UNPKG

3.78 kBJavaScriptView Raw
1"use strict";
2/* eslint no-console: off */
3var __importStar = (this && this.__importStar) || function (mod) {
4 if (mod && mod.__esModule) return mod;
5 var result = {};
6 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
7 result["default"] = mod;
8 return result;
9};
10var __importDefault = (this && this.__importDefault) || function (mod) {
11 return (mod && mod.__esModule) ? mod : { "default": mod };
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14const yargs = __importStar(require("yargs"));
15const chalk_1 = __importDefault(require("chalk"));
16const types_1 = require("./types");
17const _1 = require(".");
18const argv = yargs
19 .usage('Usage: $0 [options]')
20 .option('watch', {
21 required: false,
22 default: false,
23 type: 'boolean',
24 describe: 'Watch the GraphQL files for changes and re-run the generation',
25})
26 .option('cwd', {
27 required: false,
28 default: process.cwd(),
29 normalize: true,
30 type: 'string',
31 describe: 'Working directory to use',
32})
33 .option('schema-types-path', {
34 required: true,
35 normalize: true,
36 type: 'string',
37 describe: 'The path to output concrete schema types',
38})
39 .option('add-typename', {
40 required: false,
41 default: true,
42 type: 'boolean',
43 describe: 'Add a __typename field to every object type',
44})
45 .option('enum-format', {
46 required: false,
47 type: 'string',
48 describe: 'The format in which to generate case names for enum types in the schema',
49 choices: [
50 types_1.EnumFormat.CamelCase,
51 types_1.EnumFormat.PascalCase,
52 types_1.EnumFormat.SnakeCase,
53 types_1.EnumFormat.ScreamingSnakeCase,
54 ],
55})
56 .option('custom-scalars', {
57 required: false,
58 default: '{}',
59 type: 'string',
60 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',
61})
62 .help().argv;
63const builder = new _1.Builder({
64 cwd: argv.cwd,
65 schemaTypesPath: argv.schemaTypesPath,
66 addTypename: argv.addTypename,
67 enumFormat: argv.enumFormat,
68 customScalars: normalizeCustomScalars(argv.customScalars),
69});
70function normalizeCustomScalars(customScalarOption) {
71 try {
72 const result = JSON.parse(customScalarOption);
73 return typeof result === 'object' ? result : undefined;
74 }
75 catch (error) {
76 return undefined;
77 }
78}
79const schemas = [];
80const docs = [];
81const BUILT = chalk_1.default.inverse.bold.green(' BUILT ');
82const ERROR = chalk_1.default.inverse.bold.red(' ERROR ');
83builder.on('start:docs', () => {
84 docs.length = 0;
85 console.log();
86});
87builder.on('start:schema', () => {
88 schemas.length = 0;
89 console.log();
90});
91builder.on('build:docs', (doc) => {
92 docs.push(doc);
93});
94builder.on('build:schema', (schema) => {
95 schemas.push(schema);
96});
97builder.on('end:docs', () => {
98 docs
99 .sort(({ documentPath: a }, { documentPath: b }) => a.localeCompare(b))
100 .forEach(({ documentPath, definitionPath }) => {
101 console.log(`${BUILT} ${chalk_1.default.dim(documentPath)}${definitionPath}`);
102 });
103});
104builder.on('end:schema', () => {
105 schemas
106 .sort(({ schemaPath: a }, { schemaPath: b }) => a.localeCompare(b))
107 .forEach(({ schemaPath, schemaTypesPath }) => {
108 console.log(`${BUILT} ${chalk_1.default.dim(schemaPath)}${schemaTypesPath}`);
109 });
110});
111builder.on('error', (error) => {
112 console.log(`${ERROR} ${error.message}`);
113 if (error.stack) {
114 console.log(chalk_1.default.dim(error.stack));
115 }
116 console.log();
117 if (!argv.watch) {
118 process.exit(1);
119 }
120});
121builder.run({ watch: argv.watch });