UNPKG

4.29 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getAssetSchemasAsync = getAssetSchemasAsync;
7exports.getSchemaAsync = getSchemaAsync;
8exports.validateAsync = validateAsync;
9exports.validatorFromProjectRoot = validatorFromProjectRoot;
10function _config() {
11 const data = require("@expo/config");
12 _config = function () {
13 return data;
14 };
15 return data;
16}
17function _schemer() {
18 const data = _interopRequireDefault(require("@expo/schemer"));
19 _schemer = function () {
20 return data;
21 };
22 return data;
23}
24function _fs() {
25 const data = _interopRequireDefault(require("fs"));
26 _fs = function () {
27 return data;
28 };
29 return data;
30}
31function _getenv() {
32 const data = require("getenv");
33 _getenv = function () {
34 return data;
35 };
36 return data;
37}
38function _jsonSchemaDerefSync() {
39 const data = _interopRequireDefault(require("json-schema-deref-sync"));
40 _jsonSchemaDerefSync = function () {
41 return data;
42 };
43 return data;
44}
45function _path() {
46 const data = _interopRequireDefault(require("path"));
47 _path = function () {
48 return data;
49 };
50 return data;
51}
52function _internal() {
53 const data = require("../internal");
54 _internal = function () {
55 return data;
56 };
57 return data;
58}
59function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
60const _xdlSchemaJson = {};
61const _schemaCaches = {};
62async function validatorFromProjectRoot(projectRoot) {
63 const {
64 exp
65 } = (0, _config().getConfig)(projectRoot);
66 if (!exp.sdkVersion) throw new Error(`Couldn't read local manifest`);
67 const schema = await getSchemaAsync(exp.sdkVersion);
68 const validator = new (_schemer().default)(schema);
69 return validator;
70}
71async function validateAsync(projectRoot) {
72 const {
73 exp
74 } = (0, _config().getConfig)(projectRoot);
75 if (!exp.sdkVersion) throw new Error(`Couldn't read local manifest`);
76 const schema = await getSchemaAsync(exp.sdkVersion);
77 const validator = new (_schemer().default)(schema);
78 await validator.validateAll(exp);
79}
80async function getSchemaAsync(sdkVersion) {
81 const json = await _getSchemaJSONAsync(sdkVersion);
82 const schema = (0, _jsonSchemaDerefSync().default)(json.schema);
83 return schema;
84}
85
86/**
87 * Array of schema nodes that refer to assets along with their field path (eg. 'notification.icon')
88 *
89 * @param sdkVersion
90 */
91async function getAssetSchemasAsync(sdkVersion) {
92 // If no SDK version is available then fall back to unversioned
93 const schema = await getSchemaAsync(sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : 'UNVERSIONED');
94 const assetSchemas = [];
95 const visit = (node, fieldPath) => {
96 if (node.meta && node.meta.asset) {
97 assetSchemas.push(fieldPath);
98 }
99 const properties = node.properties;
100 if (properties) {
101 Object.keys(properties).forEach(property => visit(properties[property], `${fieldPath}${fieldPath.length > 0 ? '.' : ''}${property}`));
102 }
103 };
104 visit(schema, '');
105 return assetSchemas;
106}
107async function _getSchemaJSONAsync(sdkVersion) {
108 if ((0, _getenv().boolish)('LOCAL_XDL_SCHEMA', false)) {
109 if (process.env.EXPONENT_UNIVERSE_DIR) {
110 return JSON.parse(_fs().default.readFileSync(_path().default.join(process.env.EXPONENT_UNIVERSE_DIR, 'server', 'www', 'xdl-schemas', 'UNVERSIONED-schema.json')).toString());
111 } else {
112 throw new Error(`LOCAL_XDL_SCHEMA is set but EXPONENT_UNIVERSE_DIR is not.`);
113 }
114 }
115 if (!_xdlSchemaJson[sdkVersion]) {
116 try {
117 _xdlSchemaJson[sdkVersion] = await getConfigurationSchemaAsync(sdkVersion);
118 } catch (e) {
119 if (e.code && e.code === 'INVALID_JSON') {
120 throw new Error(`Couldn't read schema from server`);
121 } else {
122 throw e;
123 }
124 }
125 }
126 return _xdlSchemaJson[sdkVersion];
127}
128async function getConfigurationSchemaAsync(sdkVersion) {
129 if (!_schemaCaches.hasOwnProperty(sdkVersion)) {
130 _schemaCaches[sdkVersion] = new (_internal().FsCache.Cacher)(async () => {
131 return await new (_internal().ApiV2)().getAsync(`project/configuration/schema/${sdkVersion}`);
132 }, `schema-${sdkVersion}.json`, 0, _path().default.join(__dirname, `../caches/schema-${sdkVersion}.json`));
133 }
134 return await _schemaCaches[sdkVersion].getAsync();
135}
136//# sourceMappingURL=ExpSchema.js.map
\No newline at end of file