@graphql-codegen/cli 2.6.4-alpha-b6ec5b6d1.0 → 2.6.5-alpha-d6416b45e.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +187 -392
- package/index.js +206 -408
- package/index.mjs +207 -409
- package/package.json +2 -7
- package/utils/indentString.d.ts +0 -26
- package/utils/is-browser.d.ts +0 -3
- package/utils/listr-renderer.d.ts +0 -18
package/bin.js
CHANGED
|
@@ -29,12 +29,6 @@ const path__default = _interopDefault(path);
|
|
|
29
29
|
const pluginHelpers = require('@graphql-codegen/plugin-helpers');
|
|
30
30
|
const core = require('@graphql-codegen/core');
|
|
31
31
|
const utils = require('@graphql-tools/utils');
|
|
32
|
-
const chalk = _interopDefault(require('chalk'));
|
|
33
|
-
const logSymbols = _interopDefault(require('log-symbols'));
|
|
34
|
-
const ansiEscapes = _interopDefault(require('ansi-escapes'));
|
|
35
|
-
const wrapAnsi = _interopDefault(require('wrap-ansi'));
|
|
36
|
-
const commonTags = require('common-tags');
|
|
37
|
-
const UpdateRenderer = _interopDefault(require('listr-update-renderer'));
|
|
38
32
|
const graphql = require('graphql');
|
|
39
33
|
const cosmiconfig = require('cosmiconfig');
|
|
40
34
|
const stringEnvInterpolation = require('string-env-interpolation');
|
|
@@ -55,11 +49,13 @@ const fs = require('fs');
|
|
|
55
49
|
const fs__default = _interopDefault(fs);
|
|
56
50
|
const crypto = require('crypto');
|
|
57
51
|
const os = require('os');
|
|
58
|
-
const
|
|
52
|
+
const listr2 = require('listr2');
|
|
59
53
|
const isGlob = _interopDefault(require('is-glob'));
|
|
60
54
|
const debounce = _interopDefault(require('debounce'));
|
|
55
|
+
const logSymbols = _interopDefault(require('log-symbols'));
|
|
61
56
|
const mkdirp = _interopDefault(require('mkdirp'));
|
|
62
57
|
const inquirer = _interopDefault(require('inquirer'));
|
|
58
|
+
const chalk = _interopDefault(require('chalk'));
|
|
63
59
|
const detectIndent = _interopDefault(require('detect-indent'));
|
|
64
60
|
const getLatestVersion = _interopDefault(require('latest-version'));
|
|
65
61
|
|
|
@@ -75,25 +71,8 @@ function useWinstonLogger() {
|
|
|
75
71
|
logger = console;
|
|
76
72
|
}
|
|
77
73
|
|
|
78
|
-
let queue = [];
|
|
79
74
|
function debugLog(message, ...meta) {
|
|
80
|
-
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined)
|
|
81
|
-
queue.push({
|
|
82
|
-
message,
|
|
83
|
-
meta,
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
function printLogs() {
|
|
88
|
-
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
|
|
89
|
-
queue.forEach(log => {
|
|
90
|
-
getLogger().info(log.message, ...log.meta);
|
|
91
|
-
});
|
|
92
|
-
resetLogs();
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
function resetLogs() {
|
|
96
|
-
queue = [];
|
|
75
|
+
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) ;
|
|
97
76
|
}
|
|
98
77
|
|
|
99
78
|
const DEFAULT_HOOKS = {
|
|
@@ -151,7 +130,7 @@ function execShellCommand(cmd) {
|
|
|
151
130
|
});
|
|
152
131
|
}
|
|
153
132
|
async function executeHooks(hookName, scripts = [], args = []) {
|
|
154
|
-
debugLog(
|
|
133
|
+
debugLog();
|
|
155
134
|
for (const script of scripts) {
|
|
156
135
|
if (typeof script === 'string') {
|
|
157
136
|
debugLog(`Running lifecycle hook "${hookName}" script: ${script} with args: ${args.join(' ')}...`);
|
|
@@ -177,162 +156,6 @@ const lifecycleHooks = (_hooks = {}) => {
|
|
|
177
156
|
};
|
|
178
157
|
};
|
|
179
158
|
|
|
180
|
-
/**
|
|
181
|
-
Indent each line in a string.
|
|
182
|
-
@param string - The string to indent.
|
|
183
|
-
@param count - How many times you want `options.indent` repeated. Default: `1`.
|
|
184
|
-
@example
|
|
185
|
-
```
|
|
186
|
-
import indentString from 'indent-string';
|
|
187
|
-
indentString('Unicorns\nRainbows', 4);
|
|
188
|
-
//=> ' Unicorns\n Rainbows'
|
|
189
|
-
indentString('Unicorns\nRainbows', 4, {indent: '♥'});
|
|
190
|
-
//=> '♥♥♥♥Unicorns\n♥♥♥♥Rainbows'
|
|
191
|
-
```
|
|
192
|
-
*/
|
|
193
|
-
function indentString(string, count = 1, options = {}) {
|
|
194
|
-
const { indent = ' ', includeEmptyLines = false } = options;
|
|
195
|
-
if (typeof string !== 'string') {
|
|
196
|
-
throw new TypeError(`Expected \`input\` to be a \`string\`, got \`${typeof string}\``);
|
|
197
|
-
}
|
|
198
|
-
if (typeof count !== 'number') {
|
|
199
|
-
throw new TypeError(`Expected \`count\` to be a \`number\`, got \`${typeof count}\``);
|
|
200
|
-
}
|
|
201
|
-
if (count < 0) {
|
|
202
|
-
throw new RangeError(`Expected \`count\` to be at least 0, got \`${count}\``);
|
|
203
|
-
}
|
|
204
|
-
if (typeof indent !== 'string') {
|
|
205
|
-
throw new TypeError(`Expected \`options.indent\` to be a \`string\`, got \`${typeof indent}\``);
|
|
206
|
-
}
|
|
207
|
-
if (count === 0) {
|
|
208
|
-
return string;
|
|
209
|
-
}
|
|
210
|
-
const regex = includeEmptyLines ? /^/gm : /^(?!\s*$)/gm;
|
|
211
|
-
return string.replace(regex, indent.repeat(count));
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
class Renderer {
|
|
215
|
-
constructor(tasks, options) {
|
|
216
|
-
this.updateRenderer = new UpdateRenderer(tasks, options);
|
|
217
|
-
}
|
|
218
|
-
render() {
|
|
219
|
-
return this.updateRenderer.render();
|
|
220
|
-
}
|
|
221
|
-
end(err) {
|
|
222
|
-
this.updateRenderer.end(err);
|
|
223
|
-
if (typeof err === 'undefined') {
|
|
224
|
-
logUpdate.clear();
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
// persist the output
|
|
228
|
-
logUpdate.done();
|
|
229
|
-
// show errors
|
|
230
|
-
if (err) {
|
|
231
|
-
const errorCount = err.errors ? err.errors.length : 0;
|
|
232
|
-
if (errorCount > 0) {
|
|
233
|
-
const count = indentString(chalk.red.bold(`Found ${errorCount} error${errorCount > 1 ? 's' : ''}`), 1);
|
|
234
|
-
const details = err.errors
|
|
235
|
-
.map(error => {
|
|
236
|
-
debugLog(`[CLI] Exited with an error`, error);
|
|
237
|
-
return { msg: pluginHelpers.isDetailedError(error) ? error.details : null, rawError: error };
|
|
238
|
-
})
|
|
239
|
-
.map(({ msg, rawError }, i) => {
|
|
240
|
-
const source = err.errors[i].source;
|
|
241
|
-
msg = msg ? chalk.gray(indentString(commonTags.stripIndent(`${msg}`), 4)) : null;
|
|
242
|
-
const stack = rawError.stack ? chalk.gray(indentString(commonTags.stripIndent(rawError.stack), 4)) : null;
|
|
243
|
-
if (source) {
|
|
244
|
-
const sourceOfError = typeof source === 'string' ? source : source.name;
|
|
245
|
-
const title = indentString(`${logSymbols.error} ${sourceOfError}`, 2);
|
|
246
|
-
return [title, msg, stack, stack].filter(Boolean).join('\n');
|
|
247
|
-
}
|
|
248
|
-
return [msg, stack].filter(Boolean).join('\n');
|
|
249
|
-
})
|
|
250
|
-
.join('\n\n');
|
|
251
|
-
logUpdate.emit(['', count, details, ''].join('\n\n'));
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
const details = err.details ? err.details : '';
|
|
255
|
-
logUpdate.emit(`${chalk.red.bold(`${indentString(err.message, 2)}`)}\n${details}\n${chalk.grey(err.stack)}`);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
logUpdate.done();
|
|
259
|
-
printLogs();
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
const render = tasks => {
|
|
263
|
-
for (const task of tasks) {
|
|
264
|
-
task.subscribe(event => {
|
|
265
|
-
if (event.type === 'SUBTASKS') {
|
|
266
|
-
render(task.subtasks);
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
if (event.type === 'DATA') {
|
|
270
|
-
logUpdate.emit(chalk.dim(`${event.data}`));
|
|
271
|
-
}
|
|
272
|
-
logUpdate.done();
|
|
273
|
-
}, err => {
|
|
274
|
-
logUpdate.emit(err);
|
|
275
|
-
logUpdate.done();
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
class ErrorRenderer {
|
|
280
|
-
constructor(tasks, _options) {
|
|
281
|
-
this.tasks = tasks;
|
|
282
|
-
}
|
|
283
|
-
render() {
|
|
284
|
-
render(this.tasks);
|
|
285
|
-
}
|
|
286
|
-
static get nonTTY() {
|
|
287
|
-
return true;
|
|
288
|
-
}
|
|
289
|
-
end() { }
|
|
290
|
-
}
|
|
291
|
-
class LogUpdate {
|
|
292
|
-
constructor() {
|
|
293
|
-
this.stream = process.stdout;
|
|
294
|
-
// state
|
|
295
|
-
this.previousLineCount = 0;
|
|
296
|
-
this.previousOutput = '';
|
|
297
|
-
this.previousWidth = this.getWidth();
|
|
298
|
-
}
|
|
299
|
-
emit(...args) {
|
|
300
|
-
let output = args.join(' ') + '\n';
|
|
301
|
-
const width = this.getWidth();
|
|
302
|
-
if (output === this.previousOutput && this.previousWidth === width) {
|
|
303
|
-
return;
|
|
304
|
-
}
|
|
305
|
-
this.previousOutput = output;
|
|
306
|
-
this.previousWidth = width;
|
|
307
|
-
output = wrapAnsi(output, width, {
|
|
308
|
-
trim: false,
|
|
309
|
-
hard: true,
|
|
310
|
-
wordWrap: false,
|
|
311
|
-
});
|
|
312
|
-
this.stream.write(ansiEscapes.eraseLines(this.previousLineCount) + output);
|
|
313
|
-
this.previousLineCount = output.split('\n').length;
|
|
314
|
-
}
|
|
315
|
-
clear() {
|
|
316
|
-
this.stream.write(ansiEscapes.eraseLines(this.previousLineCount));
|
|
317
|
-
this.previousOutput = '';
|
|
318
|
-
this.previousWidth = this.getWidth();
|
|
319
|
-
this.previousLineCount = 0;
|
|
320
|
-
}
|
|
321
|
-
done() {
|
|
322
|
-
this.previousOutput = '';
|
|
323
|
-
this.previousWidth = this.getWidth();
|
|
324
|
-
this.previousLineCount = 0;
|
|
325
|
-
}
|
|
326
|
-
getWidth() {
|
|
327
|
-
const { columns } = this.stream;
|
|
328
|
-
if (!columns) {
|
|
329
|
-
return 80;
|
|
330
|
-
}
|
|
331
|
-
return columns;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
const logUpdate = new LogUpdate();
|
|
335
|
-
|
|
336
159
|
async function getPluginByName(name, pluginLoader) {
|
|
337
160
|
const possibleNames = [
|
|
338
161
|
`@graphql-codegen/${name}`,
|
|
@@ -830,33 +653,6 @@ function addHashToDocumentFiles(documentFilesPromise) {
|
|
|
830
653
|
}));
|
|
831
654
|
}
|
|
832
655
|
|
|
833
|
-
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
834
|
-
const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
835
|
-
|
|
836
|
-
function isListrError(err) {
|
|
837
|
-
return err.name === 'ListrError' && Array.isArray(err.errors) && err.errors.length > 0;
|
|
838
|
-
}
|
|
839
|
-
function cliError(err, exitOnError = true) {
|
|
840
|
-
let msg;
|
|
841
|
-
if (err instanceof Error) {
|
|
842
|
-
msg = err.message || err.toString();
|
|
843
|
-
}
|
|
844
|
-
else if (typeof err === 'string') {
|
|
845
|
-
msg = err;
|
|
846
|
-
}
|
|
847
|
-
else {
|
|
848
|
-
msg = JSON.stringify(err);
|
|
849
|
-
}
|
|
850
|
-
// eslint-disable-next-line no-console
|
|
851
|
-
console.error(msg);
|
|
852
|
-
if (exitOnError && isNode) {
|
|
853
|
-
process.exit(1);
|
|
854
|
-
}
|
|
855
|
-
else if (exitOnError && isBrowser) {
|
|
856
|
-
throw err;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
|
|
860
656
|
const makeDefaultLoader = (from) => {
|
|
861
657
|
if (fs__default.statSync(from).isDirectory()) {
|
|
862
658
|
from = path__default.join(from, '__fake.js');
|
|
@@ -884,39 +680,12 @@ async function executeCodegen(input) {
|
|
|
884
680
|
const config = context.getConfig();
|
|
885
681
|
const pluginContext = context.getPluginContext();
|
|
886
682
|
const result = [];
|
|
887
|
-
const commonListrOptions = {
|
|
888
|
-
exitOnError: true,
|
|
889
|
-
};
|
|
890
|
-
let listr;
|
|
891
|
-
if (process.env.VERBOSE) {
|
|
892
|
-
listr = new Listr({
|
|
893
|
-
...commonListrOptions,
|
|
894
|
-
renderer: 'verbose',
|
|
895
|
-
nonTTYRenderer: 'verbose',
|
|
896
|
-
});
|
|
897
|
-
}
|
|
898
|
-
else if (process.env.NODE_ENV === 'test') {
|
|
899
|
-
listr = new Listr({
|
|
900
|
-
...commonListrOptions,
|
|
901
|
-
renderer: 'silent',
|
|
902
|
-
nonTTYRenderer: 'silent',
|
|
903
|
-
});
|
|
904
|
-
}
|
|
905
|
-
else {
|
|
906
|
-
listr = new Listr({
|
|
907
|
-
...commonListrOptions,
|
|
908
|
-
renderer: config.silent ? 'silent' : config.errorsOnly ? ErrorRenderer : Renderer,
|
|
909
|
-
nonTTYRenderer: config.silent ? 'silent' : 'default',
|
|
910
|
-
collapse: true,
|
|
911
|
-
clearOutput: false,
|
|
912
|
-
});
|
|
913
|
-
}
|
|
914
683
|
let rootConfig = {};
|
|
915
684
|
let rootSchemas;
|
|
916
685
|
let rootDocuments;
|
|
917
686
|
const generates = {};
|
|
918
687
|
const cache = createCache();
|
|
919
|
-
function wrapTask(task, source, taskName) {
|
|
688
|
+
function wrapTask(task, source, taskName, ctx) {
|
|
920
689
|
return () => {
|
|
921
690
|
return context.profiler.run(async () => {
|
|
922
691
|
try {
|
|
@@ -926,6 +695,7 @@ async function executeCodegen(input) {
|
|
|
926
695
|
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
927
696
|
error.source = source;
|
|
928
697
|
}
|
|
698
|
+
ctx.errors.push(error);
|
|
929
699
|
throw error;
|
|
930
700
|
}
|
|
931
701
|
}, taskName);
|
|
@@ -996,119 +766,109 @@ async function executeCodegen(input) {
|
|
|
996
766
|
`);
|
|
997
767
|
}
|
|
998
768
|
}
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
const
|
|
1009
|
-
|
|
1010
|
-
|
|
769
|
+
const isTest = process.env.NODE_ENV === 'test';
|
|
770
|
+
const tasks = new listr2.Listr([
|
|
771
|
+
{
|
|
772
|
+
title: 'Parse Configuration',
|
|
773
|
+
task: () => normalize(),
|
|
774
|
+
},
|
|
775
|
+
{
|
|
776
|
+
title: 'Generate outputs',
|
|
777
|
+
task: (ctx, task) => {
|
|
778
|
+
const generateTasks = Object.keys(generates).map(filename => {
|
|
779
|
+
const outputConfig = generates[filename];
|
|
780
|
+
const hasPreset = !!outputConfig.preset;
|
|
781
|
+
const title = hasPreset
|
|
1011
782
|
? `Generate to ${filename} (using EXPERIMENTAL preset "${outputConfig.preset}")`
|
|
1012
|
-
: `Generate ${filename}
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
783
|
+
: `Generate ${filename}`;
|
|
784
|
+
return {
|
|
785
|
+
title,
|
|
786
|
+
task: (_, subTask) => {
|
|
787
|
+
let outputSchemaAst;
|
|
788
|
+
let outputSchema;
|
|
789
|
+
const outputFileTemplateConfig = outputConfig.config || {};
|
|
790
|
+
let outputDocuments = [];
|
|
791
|
+
const outputSpecificSchemas = pluginHelpers.normalizeInstanceOrArray(outputConfig.schema);
|
|
792
|
+
const outputSpecificDocuments = pluginHelpers.normalizeInstanceOrArray(outputConfig.documents);
|
|
793
|
+
return subTask.newListr([
|
|
794
|
+
{
|
|
795
|
+
title: 'Load GraphQL schemas',
|
|
796
|
+
task: wrapTask(async () => {
|
|
797
|
+
debugLog(`[CLI] Loading Schemas`);
|
|
798
|
+
const schemaPointerMap = {};
|
|
799
|
+
const allSchemaDenormalizedPointers = [...rootSchemas, ...outputSpecificSchemas];
|
|
800
|
+
for (const denormalizedPtr of allSchemaDenormalizedPointers) {
|
|
801
|
+
if (typeof denormalizedPtr === 'string') {
|
|
802
|
+
schemaPointerMap[denormalizedPtr] = {};
|
|
803
|
+
}
|
|
804
|
+
else if (typeof denormalizedPtr === 'object') {
|
|
805
|
+
Object.assign(schemaPointerMap, denormalizedPtr);
|
|
806
|
+
}
|
|
1030
807
|
}
|
|
1031
|
-
|
|
1032
|
-
|
|
808
|
+
const hash = JSON.stringify(schemaPointerMap);
|
|
809
|
+
const result = await cache('schema', hash, async () => {
|
|
810
|
+
const outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
811
|
+
const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
812
|
+
return {
|
|
813
|
+
outputSchemaAst,
|
|
814
|
+
outputSchema,
|
|
815
|
+
};
|
|
816
|
+
});
|
|
817
|
+
outputSchemaAst = result.outputSchemaAst;
|
|
818
|
+
outputSchema = result.outputSchema;
|
|
819
|
+
}, filename, `Load GraphQL schemas: ${filename}`, ctx),
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
title: 'Load GraphQL documents',
|
|
823
|
+
task: wrapTask(async () => {
|
|
824
|
+
debugLog(`[CLI] Loading Documents`);
|
|
825
|
+
const documentPointerMap = {};
|
|
826
|
+
const allDocumentsDenormalizedPointers = [...rootDocuments, ...outputSpecificDocuments];
|
|
827
|
+
for (const denormalizedPtr of allDocumentsDenormalizedPointers) {
|
|
828
|
+
if (typeof denormalizedPtr === 'string') {
|
|
829
|
+
documentPointerMap[denormalizedPtr] = {};
|
|
830
|
+
}
|
|
831
|
+
else if (typeof denormalizedPtr === 'object') {
|
|
832
|
+
Object.assign(documentPointerMap, denormalizedPtr);
|
|
833
|
+
}
|
|
1033
834
|
}
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
const outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
1038
|
-
const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
1039
|
-
return {
|
|
1040
|
-
outputSchemaAst,
|
|
1041
|
-
outputSchema,
|
|
1042
|
-
};
|
|
1043
|
-
});
|
|
1044
|
-
outputSchemaAst = await result.outputSchemaAst;
|
|
1045
|
-
outputSchema = result.outputSchema;
|
|
1046
|
-
}, filename, `Load GraphQL schemas: ${filename}`),
|
|
1047
|
-
},
|
|
1048
|
-
{
|
|
1049
|
-
title: 'Load GraphQL documents',
|
|
1050
|
-
task: wrapTask(async () => {
|
|
1051
|
-
debugLog(`[CLI] Loading Documents`);
|
|
1052
|
-
// get different cache for shared docs and output specific docs
|
|
1053
|
-
const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
|
|
1054
|
-
const hash = JSON.stringify(docs);
|
|
1055
|
-
return cache('documents', hash, async () => {
|
|
1056
|
-
const documents = await context.loadDocuments(docs);
|
|
835
|
+
const hash = JSON.stringify(documentPointerMap);
|
|
836
|
+
const result = await cache('documents', hash, async () => {
|
|
837
|
+
const documents = await context.loadDocuments(documentPointerMap);
|
|
1057
838
|
return {
|
|
1058
839
|
documents,
|
|
1059
840
|
};
|
|
1060
841
|
});
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
: outputFileTemplateConfig),
|
|
1092
|
-
};
|
|
1093
|
-
let outputs = [];
|
|
1094
|
-
if (hasPreset) {
|
|
1095
|
-
outputs = await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
1096
|
-
baseOutputDir: filename,
|
|
1097
|
-
presetConfig: outputConfig.presetConfig || {},
|
|
1098
|
-
plugins: normalizedPluginsArray,
|
|
1099
|
-
schema: outputSchema,
|
|
1100
|
-
schemaAst: outputSchemaAst,
|
|
1101
|
-
documents: outputDocuments,
|
|
1102
|
-
config: mergedConfig,
|
|
1103
|
-
pluginMap,
|
|
1104
|
-
pluginContext,
|
|
1105
|
-
profiler: context.profiler,
|
|
1106
|
-
}), `Build Generates Section: ${filename}`);
|
|
1107
|
-
}
|
|
1108
|
-
else {
|
|
1109
|
-
outputs = [
|
|
1110
|
-
{
|
|
1111
|
-
filename,
|
|
842
|
+
outputDocuments = result.documents;
|
|
843
|
+
}, filename, `Load GraphQL documents: ${filename}`, ctx),
|
|
844
|
+
},
|
|
845
|
+
{
|
|
846
|
+
title: 'Generate',
|
|
847
|
+
task: wrapTask(async () => {
|
|
848
|
+
debugLog(`[CLI] Generating output`);
|
|
849
|
+
const normalizedPluginsArray = pluginHelpers.normalizeConfig(outputConfig.plugins);
|
|
850
|
+
const pluginLoader = config.pluginLoader || makeDefaultLoader(context.cwd);
|
|
851
|
+
const pluginPackages = await Promise.all(normalizedPluginsArray.map(plugin => getPluginByName(Object.keys(plugin)[0], pluginLoader)));
|
|
852
|
+
const preset = hasPreset
|
|
853
|
+
? typeof outputConfig.preset === 'string'
|
|
854
|
+
? await getPresetByName(outputConfig.preset, makeDefaultLoader(context.cwd))
|
|
855
|
+
: outputConfig.preset
|
|
856
|
+
: null;
|
|
857
|
+
const pluginMap = Object.fromEntries(pluginPackages.map((pkg, i) => {
|
|
858
|
+
const plugin = normalizedPluginsArray[i];
|
|
859
|
+
const name = Object.keys(plugin)[0];
|
|
860
|
+
return [name, pkg];
|
|
861
|
+
}));
|
|
862
|
+
const mergedConfig = {
|
|
863
|
+
...rootConfig,
|
|
864
|
+
...(typeof outputFileTemplateConfig === 'string'
|
|
865
|
+
? { value: outputFileTemplateConfig }
|
|
866
|
+
: outputFileTemplateConfig),
|
|
867
|
+
};
|
|
868
|
+
const outputs = hasPreset
|
|
869
|
+
? await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
870
|
+
baseOutputDir: filename,
|
|
871
|
+
presetConfig: outputConfig.presetConfig || {},
|
|
1112
872
|
plugins: normalizedPluginsArray,
|
|
1113
873
|
schema: outputSchema,
|
|
1114
874
|
schemaAst: outputSchemaAst,
|
|
@@ -1117,50 +877,67 @@ async function executeCodegen(input) {
|
|
|
1117
877
|
pluginMap,
|
|
1118
878
|
pluginContext,
|
|
1119
879
|
profiler: context.profiler,
|
|
1120
|
-
},
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
880
|
+
}), `Build Generates Section: ${filename}`)
|
|
881
|
+
: [
|
|
882
|
+
{
|
|
883
|
+
filename,
|
|
884
|
+
plugins: normalizedPluginsArray,
|
|
885
|
+
schema: outputSchema,
|
|
886
|
+
schemaAst: outputSchemaAst,
|
|
887
|
+
documents: outputDocuments,
|
|
888
|
+
config: mergedConfig,
|
|
889
|
+
pluginMap,
|
|
890
|
+
pluginContext,
|
|
891
|
+
profiler: context.profiler,
|
|
892
|
+
},
|
|
893
|
+
];
|
|
894
|
+
const process = async (outputArgs) => {
|
|
895
|
+
const output = await core.codegen({
|
|
896
|
+
...outputArgs,
|
|
897
|
+
cache,
|
|
898
|
+
});
|
|
899
|
+
result.push({
|
|
900
|
+
filename: outputArgs.filename,
|
|
901
|
+
content: output,
|
|
902
|
+
hooks: outputConfig.hooks || {},
|
|
903
|
+
});
|
|
904
|
+
};
|
|
905
|
+
await context.profiler.run(() => Promise.all(outputs.map(process)), `Codegen: ${filename}`);
|
|
906
|
+
}, filename, `Generate: ${filename}`, ctx),
|
|
907
|
+
},
|
|
908
|
+
], {
|
|
909
|
+
// it stops when of the tasks failed
|
|
910
|
+
exitOnError: true,
|
|
911
|
+
});
|
|
912
|
+
},
|
|
913
|
+
// It doesn't stop when one of tasks failed, to finish at least some of outputs
|
|
914
|
+
exitOnError: false,
|
|
915
|
+
concurrent: os.cpus().length,
|
|
916
|
+
};
|
|
917
|
+
});
|
|
918
|
+
return task.newListr(generateTasks);
|
|
919
|
+
},
|
|
1148
920
|
},
|
|
921
|
+
], {
|
|
922
|
+
rendererOptions: {
|
|
923
|
+
clearOutput: false,
|
|
924
|
+
collapse: true,
|
|
925
|
+
},
|
|
926
|
+
ctx: { errors: [] },
|
|
927
|
+
rendererSilent: isTest || config.silent,
|
|
928
|
+
exitOnError: true,
|
|
1149
929
|
});
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
throw newErr;
|
|
1162
|
-
}
|
|
1163
|
-
throw err;
|
|
930
|
+
// All the errors throw in `listr2` are collected in context
|
|
931
|
+
// Running tasks doesn't throw anything
|
|
932
|
+
const executedContext = await tasks.run();
|
|
933
|
+
if (executedContext.errors.length > 0) {
|
|
934
|
+
const errors = executedContext.errors.map(subErr => pluginHelpers.isDetailedError(subErr)
|
|
935
|
+
? `${subErr.message} for "${subErr.source}"${subErr.details}`
|
|
936
|
+
: subErr.message || subErr.toString());
|
|
937
|
+
const newErr = new utils.AggregateError(executedContext.errors, `${errors.join('\n\n')}`);
|
|
938
|
+
// Best-effort to all stack traces for debugging
|
|
939
|
+
newErr.stack = `${newErr.stack}\n\n${executedContext.errors.map(subErr => subErr.stack).join('\n\n')}`;
|
|
940
|
+
throw newErr;
|
|
1164
941
|
}
|
|
1165
942
|
return result;
|
|
1166
943
|
}
|
|
@@ -1173,7 +950,7 @@ function emitWatching() {
|
|
|
1173
950
|
log(`${logSymbols.info} Watching for changes...`);
|
|
1174
951
|
}
|
|
1175
952
|
const createWatcher = (initalContext, onNext) => {
|
|
1176
|
-
debugLog(
|
|
953
|
+
debugLog();
|
|
1177
954
|
let config = initalContext.getConfig();
|
|
1178
955
|
const files = [initalContext.filepath].filter(a => a);
|
|
1179
956
|
const documents = pluginHelpers.normalizeInstanceOrArray(config.documents);
|
|
@@ -1244,10 +1021,10 @@ const createWatcher = (initalContext, onNext) => {
|
|
|
1244
1021
|
atomic: true,
|
|
1245
1022
|
ignored,
|
|
1246
1023
|
});
|
|
1247
|
-
debugLog(
|
|
1024
|
+
debugLog();
|
|
1248
1025
|
const shutdown = () => {
|
|
1249
1026
|
isShutdown = true;
|
|
1250
|
-
debugLog(
|
|
1027
|
+
debugLog();
|
|
1251
1028
|
log(`Shutting down watch...`);
|
|
1252
1029
|
watcher.close();
|
|
1253
1030
|
lifecycleHooks(config.hooks).beforeDone();
|
|
@@ -1255,7 +1032,7 @@ const createWatcher = (initalContext, onNext) => {
|
|
|
1255
1032
|
// it doesn't matter what has changed, need to run whole process anyway
|
|
1256
1033
|
watcher.on('all', async (eventName, path$1) => {
|
|
1257
1034
|
lifecycleHooks(config.hooks).onWatchTriggered(eventName, path$1);
|
|
1258
|
-
debugLog(
|
|
1035
|
+
debugLog();
|
|
1259
1036
|
const fullPath = path.join(process.cwd(), path$1);
|
|
1260
1037
|
// In ESM require is not defined
|
|
1261
1038
|
try {
|
|
@@ -1324,10 +1101,10 @@ async function generate(input, saveToFile = true) {
|
|
|
1324
1101
|
return unlinkFile(filename, err => {
|
|
1325
1102
|
const prettyFilename = filename.replace(`${input.cwd || process.cwd()}/`, '');
|
|
1326
1103
|
if (err) {
|
|
1327
|
-
debugLog(
|
|
1104
|
+
debugLog();
|
|
1328
1105
|
}
|
|
1329
1106
|
else {
|
|
1330
|
-
debugLog(
|
|
1107
|
+
debugLog();
|
|
1331
1108
|
}
|
|
1332
1109
|
});
|
|
1333
1110
|
}
|
|
@@ -1394,7 +1171,7 @@ function shouldOverwrite(config, outputPath) {
|
|
|
1394
1171
|
const globalValue = config.overwrite === undefined ? true : !!config.overwrite;
|
|
1395
1172
|
const outputConfig = config.generates[outputPath];
|
|
1396
1173
|
if (!outputConfig) {
|
|
1397
|
-
debugLog(
|
|
1174
|
+
debugLog();
|
|
1398
1175
|
return globalValue;
|
|
1399
1176
|
}
|
|
1400
1177
|
if (isConfiguredOutput(outputConfig) && typeof outputConfig.overwrite === 'boolean') {
|
|
@@ -1884,6 +1661,24 @@ async function ensureGraphQlPackage() {
|
|
|
1884
1661
|
}
|
|
1885
1662
|
}
|
|
1886
1663
|
|
|
1664
|
+
function cliError(err, exitOnError = true) {
|
|
1665
|
+
let msg;
|
|
1666
|
+
if (err instanceof Error) {
|
|
1667
|
+
msg = err.message || err.toString();
|
|
1668
|
+
}
|
|
1669
|
+
else if (typeof err === 'string') {
|
|
1670
|
+
msg = err;
|
|
1671
|
+
}
|
|
1672
|
+
else {
|
|
1673
|
+
msg = JSON.stringify(err);
|
|
1674
|
+
}
|
|
1675
|
+
// eslint-disable-next-line no-console
|
|
1676
|
+
console.error(msg);
|
|
1677
|
+
if (exitOnError) {
|
|
1678
|
+
process.exit(1);
|
|
1679
|
+
}
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1887
1682
|
const [, , cmd] = process.argv;
|
|
1888
1683
|
runCli(cmd)
|
|
1889
1684
|
.then(() => {
|