UNPKG

@sanity-codegen/cli

Version:
91 lines (68 loc) 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSchemaPath = getSchemaPath; var _path = _interopRequireDefault(require("path")); var _errors = require("@oclif/errors"); var _fileWalker = require("./file-walker"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const isRecord = value => { var type = typeof value; return value != null && (type == 'object' || type == 'function'); }; async function getSchemaPath({ config, args, root, logger }) { if (args.schemaPath) { try { const resolvedSchemaPath = require.resolve(_path.default.resolve(process.cwd(), args.schemaPath)); logger.info(`Using schema at: ${resolvedSchemaPath}`); return resolvedSchemaPath; } catch { throw new _errors.CLIError(`Could not resolve \`schemaPath\` ${args.schemaPath} provided via CLI args.`); } } if (config !== null && config !== void 0 && config.schemaPath) { try { const resolvedSchemaPath = require.resolve(_path.default.resolve(process.cwd(), config.schemaPath)); logger.info(`Using schema at: ${resolvedSchemaPath}`); return resolvedSchemaPath; } catch { throw new _errors.CLIError(`Could not resolve \`schemaPath\` "${config.schemaPath}" provided via the config.`); } } const sanityJsonPath = await (0, _fileWalker.fileWalker)({ filenameIfNotFound: 'sanity.json', startingPoint: root }); logger.info(`Found sanity.json at: ${sanityJsonPath}`); if (!sanityJsonPath) { throw new _errors.CLIError(`Failed to find schemaPath. No schemaPath was provided through CLI args, ` + `configs, and no sanity.json was found.`); } try { const sanityJson = require(sanityJsonPath); if (typeof sanityJson !== 'object' || !sanityJson) { throw new _errors.CLIError(`Expected type of sanity.json to be an object but found "${!sanityJson ? 'null' : typeof sanityJson}" instead.`); } // no parts in sanity.json if (!('parts' in sanityJson)) { throw new _errors.CLIError(`sanity.json did not include a \`parts\` array.`); } const parts = sanityJson.parts; // parts is not an array if (!Array.isArray(parts)) { throw new _errors.CLIError(`Expected \`parts\` in sanity.json to be an Array but found "${typeof parts}" instead.`); } // parts doesn't contain `part:@sanity/base/schema` const schemaPart = parts.find(part => part.name === 'part:@sanity/base/schema'); if (!schemaPart) { throw new _errors.CLIError(`Could not find schema part \`part:@sanity/base/schema\` in sanity.json.`); } const resolvedSchemaPath = require.resolve(_path.default.resolve(_path.default.dirname(sanityJsonPath), schemaPart.path)); logger.info(`Using schema at ${resolvedSchemaPath} found in sanity.json`); return resolvedSchemaPath; } catch (e) { throw new _errors.CLIError('Failed to get schema path from `sanity.json`. ' + 'Please fix your sanity.json or provide the schemaPath via CLI args or the config. Error: ' + (isRecord(e) && (e === null || e === void 0 ? void 0 : e.message) || 'unknown error')); } }