xref: /expo/docs/scripts/schema-sync.cjs (revision dfd15ebd)
1/*
2This script updates the necessary schema for the passed-in version.
3
4yarn run schema-sync 38 -> updates the schema that versions/v38.0.0/sdk/app-config.md uses
5yarn run schema-sync unversioned -> updates the schema that versions/unversioned/sdk/app-config.md uses
6*/
7
8const parser = require('@apidevtools/json-schema-ref-parser');
9const axios = require('axios');
10const fs = require('fs-extra');
11const path = require('path');
12
13const version = process.argv[2];
14
15async function run() {
16  if (!version) {
17    console.log('Please enter a version number\n');
18    console.log('E.g., "yarn run schema-sync 38" \nor, "yarn run schema-sync unversioned"');
19    return;
20  }
21
22  if (version === 'unversioned') {
23    const response = await axios.get(
24      `http://exp.host/--/api/v2/project/configuration/schema/UNVERSIONED`
25    );
26    const schema = await preprocessSchema(response.data.data.schema);
27
28    await fs.writeFile(
29      `scripts/schemas/unversioned/app-config-schema.js`,
30      'export default ',
31      'utf8'
32    );
33    await fs.appendFile(
34      `scripts/schemas/unversioned/app-config-schema.js`,
35      JSON.stringify(schema.properties),
36      'utf8'
37    );
38  } else {
39    try {
40      console.log(`Fetching schema for ${version} from production...`);
41      await fetchAndWriteSchema(version, false);
42    } catch {
43      console.log(`Unable to fetch schema for ${version} from production, trying staging...`);
44      await fetchAndWriteSchema(version, true);
45    }
46  }
47}
48
49async function fetchAndWriteSchema(version, staging) {
50  const schemaPath = `scripts/schemas/v${version}.0.0/app-config-schema.js`;
51  fs.ensureDirSync(path.dirname(schemaPath));
52
53  const hostname = staging ? 'staging.exp.host' : 'exp.host';
54
55  const response = await axios.get(
56    `http://${hostname}/--/api/v2/project/configuration/schema/${version}.0.0`
57  );
58  const schema = await preprocessSchema(response.data.data.schema);
59
60  await fs.writeFile(schemaPath, 'export default ', 'utf8');
61  await fs.appendFile(schemaPath, JSON.stringify(schema.properties), 'utf8');
62}
63
64async function preprocessSchema(schema) {
65  // replace all $ref references with the actual definitions
66  return await parser.dereference(schema);
67}
68
69run();
70