"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAssetSchemasAsync = getAssetSchemasAsync; exports.getSchemaAsync = getSchemaAsync; exports.validateAsync = validateAsync; exports.validatorFromProjectRoot = validatorFromProjectRoot; function _config() { const data = require("@expo/config"); _config = function () { return data; }; return data; } function _schemer() { const data = _interopRequireDefault(require("@expo/schemer")); _schemer = function () { return data; }; return data; } function _fs() { const data = _interopRequireDefault(require("fs")); _fs = function () { return data; }; return data; } function _getenv() { const data = require("getenv"); _getenv = function () { return data; }; return data; } function _jsonSchemaDerefSync() { const data = _interopRequireDefault(require("json-schema-deref-sync")); _jsonSchemaDerefSync = function () { return data; }; return data; } function _path() { const data = _interopRequireDefault(require("path")); _path = function () { return data; }; return data; } function _internal() { const data = require("../internal"); _internal = function () { return data; }; return data; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const _xdlSchemaJson = {}; const _schemaCaches = {}; async function validatorFromProjectRoot(projectRoot) { const { exp } = (0, _config().getConfig)(projectRoot); if (!exp.sdkVersion) throw new Error(`Couldn't read local manifest`); const schema = await getSchemaAsync(exp.sdkVersion); const validator = new (_schemer().default)(schema); return validator; } async function validateAsync(projectRoot) { const { exp } = (0, _config().getConfig)(projectRoot); if (!exp.sdkVersion) throw new Error(`Couldn't read local manifest`); const schema = await getSchemaAsync(exp.sdkVersion); const validator = new (_schemer().default)(schema); await validator.validateAll(exp); } async function getSchemaAsync(sdkVersion) { const json = await _getSchemaJSONAsync(sdkVersion); const schema = (0, _jsonSchemaDerefSync().default)(json.schema); return schema; } /** * Array of schema nodes that refer to assets along with their field path (eg. 'notification.icon') * * @param sdkVersion */ async function getAssetSchemasAsync(sdkVersion) { // If no SDK version is available then fall back to unversioned const schema = await getSchemaAsync(sdkVersion !== null && sdkVersion !== void 0 ? sdkVersion : 'UNVERSIONED'); const assetSchemas = []; const visit = (node, fieldPath) => { if (node.meta && node.meta.asset) { assetSchemas.push(fieldPath); } const properties = node.properties; if (properties) { Object.keys(properties).forEach(property => visit(properties[property], `${fieldPath}${fieldPath.length > 0 ? '.' : ''}${property}`)); } }; visit(schema, ''); return assetSchemas; } async function _getSchemaJSONAsync(sdkVersion) { if ((0, _getenv().boolish)('LOCAL_XDL_SCHEMA', false)) { if (process.env.EXPONENT_UNIVERSE_DIR) { return JSON.parse(_fs().default.readFileSync(_path().default.join(process.env.EXPONENT_UNIVERSE_DIR, 'server', 'www', 'xdl-schemas', 'UNVERSIONED-schema.json')).toString()); } else { throw new Error(`LOCAL_XDL_SCHEMA is set but EXPONENT_UNIVERSE_DIR is not.`); } } if (!_xdlSchemaJson[sdkVersion]) { try { _xdlSchemaJson[sdkVersion] = await getConfigurationSchemaAsync(sdkVersion); } catch (e) { if (e.code && e.code === 'INVALID_JSON') { throw new Error(`Couldn't read schema from server`); } else { throw e; } } } return _xdlSchemaJson[sdkVersion]; } async function getConfigurationSchemaAsync(sdkVersion) { if (!_schemaCaches.hasOwnProperty(sdkVersion)) { _schemaCaches[sdkVersion] = new (_internal().FsCache.Cacher)(async () => { return await new (_internal().ApiV2)().getAsync(`project/configuration/schema/${sdkVersion}`); }, `schema-${sdkVersion}.json`, 0, _path().default.join(__dirname, `../caches/schema-${sdkVersion}.json`)); } return await _schemaCaches[sdkVersion].getAsync(); } //# sourceMappingURL=ExpSchema.js.map