xref: /expo/docs/scripts/schema-sync.cjs (revision 21d45759)
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      `public/static/schemas/unversioned/app-config-schema.json`,
30      JSON.stringify(schema.properties),
31      'utf8'
32    );
33  } else {
34    try {
35      console.log(`Fetching schema for ${version} from production...`);
36      await fetchAndWriteSchema(version, false);
37    } catch {
38      console.log(`Unable to fetch schema for ${version} from production, trying staging...`);
39      await fetchAndWriteSchema(version, true);
40    }
41  }
42}
43
44async function fetchAndWriteSchema(version, staging) {
45  const schemaPath = `public/static/schemas/v${version}.0.0/app-config-schema.json`;
46  fs.ensureDirSync(path.dirname(schemaPath));
47
48  const hostname = staging ? 'staging.exp.host' : 'exp.host';
49
50  const response = await axios.get(
51    `http://${hostname}/--/api/v2/project/configuration/schema/${version}.0.0`
52  );
53  const schema = await preprocessSchema(response.data.data.schema);
54
55  await fs.writeFile(schemaPath, JSON.stringify(schema.properties), 'utf8');
56}
57
58async function preprocessSchema(schema) {
59  // replace all $ref references with the actual definitions
60  return await parser.dereference(schema);
61}
62
63run();
64