UNPKG

@sanity-codegen/cli

Version:
156 lines (127 loc) 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _command = require("@oclif/command"); var _commonTags = require("common-tags"); var _path = _interopRequireDefault(require("path")); var _fs = _interopRequireDefault(require("fs")); var _schemaCodegen = require("@sanity-codegen/schema-codegen"); var _getConfig = require("../get-config"); var _getSchemaPath = require("../get-schema-path"); var _simpleLogger = require("../simple-logger"); var _createAnimatedLogger = require("../create-animated-logger"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class SchemaCodegen extends _command.Command { constructor(...args) { super(...args); _defineProperty(this, "logger", process.env.CI === 'true' || process.env.NODE_ENV === 'test' ? _simpleLogger.simpleLogger : (0, _createAnimatedLogger.createAnimatedLogger)()); } async run() { const { logger } = this; const { args, flags } = this.parse(SchemaCodegen); const { babelOptions, config, babelrcPath, root } = await (0, _getConfig.getConfig)({ flags, logger }); const normalizedSchema = config !== null && config !== void 0 && config.normalizedSchema ? config.normalizedSchema : await (0, _schemaCodegen.schemaExtractor)({ schemaPath: await (0, _getSchemaPath.getSchemaPath)({ config, args, root, logger }), babelrcPath: babelrcPath || undefined, babelOptions, cwd: root }); const result = await (0, _schemaCodegen.generateSchemaTypes)({ normalizedSchema, generateTypeName: config === null || config === void 0 ? void 0 : config.generateTypeName, prettierResolveConfigOptions: config === null || config === void 0 ? void 0 : config.prettierResolveConfigOptions, prettierResolveConfigPath: config === null || config === void 0 ? void 0 : config.prettierResolveConfigPath }); const schemaTypesOutputPath = _path.default.resolve(root, flags.schemaTypesOutputPath || (config === null || config === void 0 ? void 0 : config.schemaTypesOutputPath) || 'schema-types.d.ts'); await _fs.default.promises.writeFile(schemaTypesOutputPath, result); // TODO: would be better with a relative path logger.success(`Wrote schema types output to: ${schemaTypesOutputPath}`); const schemaJsonOutputPath = _path.default.resolve(root, flags.schemaJsonOutputPath || (config === null || config === void 0 ? void 0 : config.schemaJsonOutputPath) || 'schema-def.json'); await _fs.default.promises.writeFile(schemaJsonOutputPath, JSON.stringify(normalizedSchema, null, 2)); // TODO: would be better with a relative path logger.success(`Wrote schema JSON output to: ${schemaJsonOutputPath}`); } } exports.default = SchemaCodegen; _defineProperty(SchemaCodegen, "description", (0, _commonTags.stripIndents)` loads a sanity schema and generates TypeScript types from it `); _defineProperty(SchemaCodegen, "flags", { help: _command.flags.help({ char: 'h' }), configPath: _command.flags.string({ name: 'configPath', description: (0, _commonTags.stripIndents)` Optionally provide an exact path for the CLI to look for a sanity-codegen configuration file. If not provided, the CLI will walk up the file system checking for \`sanity-codegen.config.js\` or \`sanity-codegen.config.ts\`. Any CLI flags passed with override the config options. ` }), babelrcPath: _command.flags.string({ name: 'babelrcPath', description: (0, _commonTags.stripIndents)` Optionally provide a path to a .babelrc file. This will be passed into the babel options of the schema executor. \`babelOptions\` takes precedence over \`babelrcPath\` ` }), babelOptions: _command.flags.string({ name: 'babelOptions', description: (0, _commonTags.stripIndents)` Optionally provide babel options inline in a JSON blob. This will be passed into the babel options of the schema executor. \`babelOptions\` takes precedence over \`babelrcPath\` ` }), root: _command.flags.string({ name: 'root', description: (0, _commonTags.stripIndents)` Determines from where files are relative to. Defaults to where your sanity-codegen config was found (if any) or the current working directory. ` }), schemaTypesOutputPath: _command.flags.string({ name: 'schemaTypesOutputPath', description: (0, _commonTags.stripIndents)` Optionally provide a destination path to the resulting sanity schema types. The default value is ./schema-types.d.ts. ` }), schemaJsonOutputPath: _command.flags.string({ name: 'schemaJsonOutputPath', description: (0, _commonTags.stripIndents)` Optionally provide a destination path to the resulting sanity schema JSON. The default value is ./schema-def.json. ` }) }); _defineProperty(SchemaCodegen, "args", [{ name: 'schemaPath', description: (0, _commonTags.stripIndents)` Optionally provide the path to your sanity schema entry point. If not provided, the CLI will try to get this value from your sanity.json file. ` }]);