UNPKG

@sanity-codegen/cli

Version:
156 lines (127 loc) 5.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _command = require("@oclif/command"); var _errors = require("@oclif/errors"); var _commonTags = require("common-tags"); var _path = _interopRequireDefault(require("path")); var _fs = _interopRequireDefault(require("fs")); var _groqCodegen = require("@sanity-codegen/groq-codegen"); var _getConfig = require("../get-config"); var _createAnimatedLogger = require("../create-animated-logger"); var _simpleLogger = require("../simple-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; } const isRecord = obj => typeof obj === 'object' && !!obj; class GroqCodegen 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(GroqCodegen); const { config, root, babelOptions } = await (0, _getConfig.getConfig)({ flags, logger }); const normalizedSchema = config !== null && config !== void 0 && config.normalizedSchema ? config.normalizedSchema : await (async () => { const schemaJsonInputPath = _path.default.resolve(root, flags.schemaJsonInputPath || (config === null || config === void 0 ? void 0 : config.schemaJsonInputPath) || (config === null || config === void 0 ? void 0 : config.schemaJsonOutputPath) || 'schema-def.json'); if (!_fs.default.existsSync(schemaJsonInputPath)) { throw new _errors.CLIError(`Could not find \`schemaJsonInputPath\`. You may need to run \`npx sanity-codegen schema-codegen first.\``); } logger.info(`Using schemaJson at "${schemaJsonInputPath}"`); try { const buffer = await _fs.default.promises.readFile(schemaJsonInputPath); const schema = JSON.parse(buffer.toString()); return schema; } catch (e) { const errorMessage = isRecord(e) && typeof (e === null || e === void 0 ? void 0 : e.message) === 'string' ? ` Error: ${e.message}` : ''; throw new _errors.CLIError(`Failed to read schemaJson at ${schemaJsonInputPath} .${errorMessage}`); } })(); const groqCodegenInclude = args.groqCodegenInclude || (config === null || config === void 0 ? void 0 : config.groqCodegenInclude); if (!groqCodegenInclude) { throw new Error(`No \`groqCodegenInclude\` pattern was provided. Please add this to your config or provide via the CLI.`); } const result = await (0, _groqCodegen.generateGroqTypes)({ groqCodegenInclude, groqCodegenExclude: flags.groqCodegenExclude || (config === null || config === void 0 ? void 0 : config.groqCodegenExclude), babelOptions, prettierResolveConfigOptions: config === null || config === void 0 ? void 0 : config.prettierResolveConfigOptions, prettierResolveConfigPath: config === null || config === void 0 ? void 0 : config.prettierResolveConfigPath, root, normalizedSchema, logger }); const queryTypesOutputPath = _path.default.resolve(root, flags.queryTypesOutputPath || (config === null || config === void 0 ? void 0 : config.queryTypesOutputPath) || 'query-types.d.ts'); logger.verbose('Writing query types output…'); await _fs.default.promises.writeFile(queryTypesOutputPath, result); logger.success(`Wrote query types output to: ${queryTypesOutputPath}`); } } exports.default = GroqCodegen; _defineProperty(GroqCodegen, "description", (0, _commonTags.stripIndents)` parses source code files for GROQ queries and outputs TypeScript types from them `); _defineProperty(GroqCodegen, "flags", { help: _command.flags.help({ char: 'h' }), 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. ` }), 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. ` }), queryTypesOutputPath: _command.flags.string({ name: 'queryTypesOutputPath', description: (0, _commonTags.stripIndents)` Optionally provide a destination path to the resulting sanity groq types. The default value is \`query-types.d.ts\`. ` }), schemaJsonInputPath: _command.flags.string({ name: 'schemaJsonInputPath', description: (0, _commonTags.stripIndents)` Optionally provide an input \`schema-def.json\` file to be used for GROQ codegen. This is the \`schemaJsonOutputPath\` by default. ` }), groqCodegenExclude: _command.flags.string({ name: 'groqCodegenExclude', description: (0, _commonTags.stripIndents)` Specify a glob or a list of globs to specify which source files you want to exclude from type generation. ` }) }); _defineProperty(GroqCodegen, "args", [{ name: 'groqCodegenInclude', description: (0, _commonTags.stripIndents)` Provide a glob to match source files you wish to parse for GROQ queries. ` }]);