1 | #!/usr/bin/env node
|
2 | "use strict";
|
3 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
4 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
5 | };
|
6 | var _a, _b;
|
7 | Object.defineProperty(exports, "__esModule", { value: true });
|
8 | exports.printHelp = void 0;
|
9 | const parse_1 = require("./helpers/parse");
|
10 | const main_1 = require("./main");
|
11 | const validateConfigs_1 = require("./tasks/validateConfigs");
|
12 | const version_1 = require("./version");
|
13 | const colors_1 = __importDefault(require("colors"));
|
14 | const fs_1 = require("fs");
|
15 | const yargs_1 = __importDefault(require("yargs"));
|
16 | const program = (0, yargs_1.default)(process.argv.slice(2))
|
17 | .option('output', {
|
18 | alias: 'o',
|
19 | description: 'Output directory',
|
20 | demandOption: true,
|
21 | type: 'string',
|
22 | })
|
23 | .option('endpoint', {
|
24 | alias: 'e',
|
25 | description: 'Graphql endpoint',
|
26 | type: 'string',
|
27 | })
|
28 | .option('get', {
|
29 | alias: 'g',
|
30 | description: 'use GET for introspection query',
|
31 | type: 'boolean',
|
32 | })
|
33 | .option('schema', {
|
34 | alias: 's',
|
35 | type: 'string',
|
36 | description: 'path to GraphQL schema definition file',
|
37 | })
|
38 |
|
39 | .option('header', {
|
40 | alias: 'H',
|
41 | type: 'array',
|
42 | string: true,
|
43 | description: 'header to use in introspection query',
|
44 | })
|
45 | .option('scalar', {
|
46 | alias: 'S',
|
47 | type: 'array',
|
48 | string: true,
|
49 | description: 'map a scalar to a type, for example `-S DateTime:string` ',
|
50 | })
|
51 | .option('methodPrefix', {
|
52 | alias: 'mp',
|
53 | type: 'string',
|
54 | default: '',
|
55 | string: true,
|
56 | description: 'prefix for generated methods',
|
57 | })
|
58 | .option('methodSuffix', {
|
59 | alias: 'ms',
|
60 | type: 'string',
|
61 | default: '',
|
62 | string: true,
|
63 | description: 'suffix for generated methods',
|
64 | })
|
65 | .option('esm', {
|
66 | type: 'boolean',
|
67 | default: false,
|
68 | description: 'generate only ES modules code, ./generated/index.js will use esm exports and imports',
|
69 | })
|
70 | .option('standalone-name', {
|
71 | type: 'string',
|
72 | default: '',
|
73 | description: 'generate only standalone bundle code, ./generated/standalone.js',
|
74 | })
|
75 | .option('standalone-compress', {
|
76 | type: 'boolean',
|
77 | default: true,
|
78 | description: 'compress standalone bundle code, ./generated/standalone.js',
|
79 | })
|
80 | .option('esm-and-cjs', {
|
81 | type: 'boolean',
|
82 | default: false,
|
83 | description: 'generate both ES modules code and CJS code, useful when publishing a package, ./generated/index.js will use CJS require',
|
84 | })
|
85 | .option('sort', {
|
86 | type: 'boolean',
|
87 | default: false,
|
88 | description: 'sort object properties to not create diffs after generations',
|
89 | })
|
90 | .option('verbose', { alias: 'v', type: 'boolean', default: false })
|
91 | .example('$0 --output ./generated --endpoint http://localhost:3000 -H "Authorization: Bearer xxx"', 'generate the client from an endpoint')
|
92 | .example('$0 --output ./generated --schema ./schema.graphql', 'generate the client from a schema')
|
93 | .help('help')
|
94 | .help('h')
|
95 | .parseSync();
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | const config = {
|
111 | endpoint: program.endpoint,
|
112 | useGet: program.get,
|
113 | schema: program.schema && readFile(program.schema),
|
114 | output: program.output,
|
115 | methodPrefix: (_a = program.methodPrefix) !== null && _a !== void 0 ? _a : '',
|
116 | methodSuffix: (_b = program.methodSuffix) !== null && _b !== void 0 ? _b : '',
|
117 | headers: (0, parse_1.parseColonSeparatedStrings)(program.header || []),
|
118 | scalarTypes: (0, parse_1.parseColonSeparatedStrings)(program.scalar || []),
|
119 | onlyEsModules: program.esm,
|
120 | onlyCJSModules: !program['esm-and-cjs'] && !program.esm,
|
121 | 'standalone-name': program['standalone-name'],
|
122 | 'standalone-compress': program['standalone-compress'],
|
123 | verbose: program.verbose,
|
124 | sortProperties: program.sort,
|
125 | };
|
126 | if (!(0, validateConfigs_1.validateConfigs)([config])) {
|
127 | process.exit(1);
|
128 | }
|
129 | (0, main_1.generate)(config)
|
130 | .catch((e) => {
|
131 | console.error(colors_1.default.red('Cannot generate, got an error:'));
|
132 | console.error(e);
|
133 | process.exit(1);
|
134 | })
|
135 | .then(() => {
|
136 | printHelp({
|
137 | dirPath: program.output,
|
138 | useYarn: false,
|
139 | dependencies: [`@gqlts/runtime@${version_1.version}`, 'graphql'],
|
140 | });
|
141 | });
|
142 | function printHelp({ useYarn, dirPath, dependencies }) {
|
143 | console.info();
|
144 | console.info(`${colors_1.default.green('Success!')} Generated client code inside '${dirPath}'`);
|
145 | console.info();
|
146 | console.info(colors_1.default.bold('Remember to install the necessary runtime package with:'));
|
147 | console.info();
|
148 | console.info(` ${colors_1.default.cyan(`${useYarn ? 'yarn add' : 'npm install'} ${dependencies.join(' ')}`)}`);
|
149 | console.info();
|
150 | console.info('PS: `@gqlts/runtime` should always have the same version as the cli!');
|
151 | console.info();
|
152 | }
|
153 | exports.printHelp = printHelp;
|
154 | function readFile(p) {
|
155 | if (!(0, fs_1.existsSync)(p)) {
|
156 | console.log(`file '${p}' does not exist`);
|
157 | process.exit(1);
|
158 | }
|
159 | return (0, fs_1.readFileSync)(p).toString();
|
160 | }
|
161 |
|
\ | No newline at end of file |