UNPKG

graphql-typescript-definitions

Version:
115 lines (109 loc) 3.96 kB
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } /* eslint no-console: off */ import chalk from 'chalk'; import yargs from 'yargs'; import { EnumFormat, ExportFormat } from "./types.mjs"; import { Builder } from "./index.mjs"; var argv = yargs.usage('Usage: $0 [options]').option('watch', { required: false, "default": false, type: 'boolean', describe: 'Watch the GraphQL files for changes and re-run the generation' }).option('cwd', { required: false, "default": process.cwd(), normalize: true, type: 'string', describe: 'Working directory to use' }).option('schema-types-path', { required: true, normalize: true, type: 'string', describe: 'The path to output concrete schema types' }).option('add-typename', { required: false, "default": true, type: 'boolean', describe: 'Add a __typename field to every object type' }).option('export-format', { required: false, describe: 'The format to use for values exported from GraphQL documents', choices: [ExportFormat.Document, ExportFormat.Simple] }).option('enum-format', { required: false, describe: 'The format in which to generate case names for enum types in the schema', choices: [EnumFormat.CamelCase, EnumFormat.PascalCase, EnumFormat.SnakeCase, EnumFormat.ScreamingSnakeCase] }).option('custom-scalars', { required: false, "default": '{}', type: 'string', describe: 'A JSON-serialized object where the key is the name of a custom scalar, and the value is an object with the name and package from which to import the type to use for that scalar' }).help().argv; var builder = new Builder({ cwd: argv.cwd, schemaTypesPath: argv['schema-types-path'], addTypename: argv['add-typename'], enumFormat: argv['enum-format'], customScalars: normalizeCustomScalars(argv['custom-scalars']) }); function normalizeCustomScalars(customScalarOption) { try { var result = JSON.parse(customScalarOption); return _typeof(result) === 'object' ? result : undefined; } catch (error) { return undefined; } } var schemas = []; var docs = []; var BUILT = chalk.inverse.bold.green(' BUILT '); var ERROR = chalk.inverse.bold.red(' ERROR '); builder.on('start:docs', function () { docs.length = 0; console.log(); }); builder.on('start:schema', function () { schemas.length = 0; console.log(); }); builder.on('build:docs', function (doc) { docs.push(doc); }); builder.on('build:schema', function (schema) { schemas.push(schema); }); builder.on('end:docs', function () { docs.sort(function (_ref, _ref2) { var documentPathA = _ref.documentPath; var documentPathB = _ref2.documentPath; return documentPathA.localeCompare(documentPathB); }).forEach(function (_ref3) { var documentPath = _ref3.documentPath, definitionPath = _ref3.definitionPath; console.log("".concat(BUILT, " ").concat(chalk.dim(documentPath), " \u2192 ").concat(definitionPath)); }); }); builder.on('end:schema', function () { schemas.sort(function (_ref4, _ref5) { var schemaPathA = _ref4.schemaPath; var schemaPathB = _ref5.schemaPath; return schemaPathA.localeCompare(schemaPathB); }).forEach(function (_ref6) { var schemaPath = _ref6.schemaPath, schemaTypesPath = _ref6.schemaTypesPath; console.log("".concat(BUILT, " ").concat(chalk.dim(schemaPath), " \u2192 ").concat(schemaTypesPath)); }); }); builder.on('error', function (error) { console.log("".concat(ERROR, " ").concat(error.message)); if (error.stack) { console.log(chalk.dim(error.stack)); } console.log(); if (!argv.watch) { process.exit(1); } }); builder.run({ watch: argv.watch });