@graphql-codegen/cli 2.3.0 → 2.4.0-alpha-23d229715.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 +50 -28
- package/config.d.ts +9 -1
- package/index.js +50 -28
- package/index.mjs +52 -30
- package/package.json +5 -4
package/bin.js
CHANGED
|
@@ -676,6 +676,10 @@ function buildOptions() {
|
|
|
676
676
|
describe: 'Only print errors',
|
|
677
677
|
type: 'boolean',
|
|
678
678
|
},
|
|
679
|
+
profile: {
|
|
680
|
+
describe: 'Use profiler to measure performance',
|
|
681
|
+
type: 'boolean',
|
|
682
|
+
},
|
|
679
683
|
p: {
|
|
680
684
|
alias: 'project',
|
|
681
685
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -717,6 +721,9 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
717
721
|
if (cliFlags.project) {
|
|
718
722
|
context.useProject(cliFlags.project);
|
|
719
723
|
}
|
|
724
|
+
if (cliFlags.profile === true) {
|
|
725
|
+
context.useProfiler();
|
|
726
|
+
}
|
|
720
727
|
context.updateConfig(config);
|
|
721
728
|
}
|
|
722
729
|
class CodegenContext {
|
|
@@ -726,6 +733,7 @@ class CodegenContext {
|
|
|
726
733
|
this._graphqlConfig = graphqlConfig;
|
|
727
734
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
728
735
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
736
|
+
this.profiler = pluginHelpers.createNoopProfiler();
|
|
729
737
|
}
|
|
730
738
|
useProject(name) {
|
|
731
739
|
this._project = name;
|
|
@@ -756,6 +764,13 @@ class CodegenContext {
|
|
|
756
764
|
...config,
|
|
757
765
|
};
|
|
758
766
|
}
|
|
767
|
+
useProfiler() {
|
|
768
|
+
this.profiler = pluginHelpers.createProfiler();
|
|
769
|
+
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
770
|
+
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
771
|
+
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
772
|
+
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
773
|
+
}
|
|
759
774
|
getPluginContext() {
|
|
760
775
|
return this._pluginContext;
|
|
761
776
|
}
|
|
@@ -791,19 +806,6 @@ const makeDefaultLoader = (from) => {
|
|
|
791
806
|
};
|
|
792
807
|
};
|
|
793
808
|
async function executeCodegen(input) {
|
|
794
|
-
function wrapTask(task, source) {
|
|
795
|
-
return async () => {
|
|
796
|
-
try {
|
|
797
|
-
await Promise.resolve().then(() => task());
|
|
798
|
-
}
|
|
799
|
-
catch (error) {
|
|
800
|
-
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
801
|
-
error.source = source;
|
|
802
|
-
}
|
|
803
|
-
throw error;
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
}
|
|
807
809
|
const context = ensureContext(input);
|
|
808
810
|
const config = context.getConfig();
|
|
809
811
|
const pluginContext = context.getPluginContext();
|
|
@@ -839,6 +841,21 @@ async function executeCodegen(input) {
|
|
|
839
841
|
let rootSchemas;
|
|
840
842
|
let rootDocuments;
|
|
841
843
|
const generates = {};
|
|
844
|
+
function wrapTask(task, source, taskName) {
|
|
845
|
+
return () => {
|
|
846
|
+
return context.profiler.run(async () => {
|
|
847
|
+
try {
|
|
848
|
+
await Promise.resolve().then(() => task());
|
|
849
|
+
}
|
|
850
|
+
catch (error) {
|
|
851
|
+
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
852
|
+
error.source = source;
|
|
853
|
+
}
|
|
854
|
+
throw error;
|
|
855
|
+
}
|
|
856
|
+
}, taskName);
|
|
857
|
+
};
|
|
858
|
+
}
|
|
842
859
|
async function normalize() {
|
|
843
860
|
/* Load Require extensions */
|
|
844
861
|
const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
|
|
@@ -942,7 +959,7 @@ async function executeCodegen(input) {
|
|
|
942
959
|
}
|
|
943
960
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
944
961
|
outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
945
|
-
}, filename),
|
|
962
|
+
}, filename, `Load GraphQL schemas: ${filename}`),
|
|
946
963
|
},
|
|
947
964
|
{
|
|
948
965
|
title: 'Load GraphQL documents',
|
|
@@ -953,7 +970,7 @@ async function executeCodegen(input) {
|
|
|
953
970
|
if (documents.length > 0) {
|
|
954
971
|
outputDocuments.push(...documents);
|
|
955
972
|
}
|
|
956
|
-
}, filename),
|
|
973
|
+
}, filename, `Load GraphQL documents: ${filename}`),
|
|
957
974
|
},
|
|
958
975
|
{
|
|
959
976
|
title: 'Generate',
|
|
@@ -981,7 +998,7 @@ async function executeCodegen(input) {
|
|
|
981
998
|
};
|
|
982
999
|
let outputs = [];
|
|
983
1000
|
if (hasPreset) {
|
|
984
|
-
outputs = await preset.buildGeneratesSection({
|
|
1001
|
+
outputs = await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
985
1002
|
baseOutputDir: filename,
|
|
986
1003
|
presetConfig: outputConfig.presetConfig || {},
|
|
987
1004
|
plugins: normalizedPluginsArray,
|
|
@@ -991,7 +1008,8 @@ async function executeCodegen(input) {
|
|
|
991
1008
|
config: mergedConfig,
|
|
992
1009
|
pluginMap,
|
|
993
1010
|
pluginContext,
|
|
994
|
-
|
|
1011
|
+
profiler: context.profiler,
|
|
1012
|
+
}), `Build Generates Section: ${filename}`);
|
|
995
1013
|
}
|
|
996
1014
|
else {
|
|
997
1015
|
outputs = [
|
|
@@ -1004,6 +1022,7 @@ async function executeCodegen(input) {
|
|
|
1004
1022
|
config: mergedConfig,
|
|
1005
1023
|
pluginMap,
|
|
1006
1024
|
pluginContext,
|
|
1025
|
+
profiler: context.profiler,
|
|
1007
1026
|
},
|
|
1008
1027
|
];
|
|
1009
1028
|
}
|
|
@@ -1015,8 +1034,8 @@ async function executeCodegen(input) {
|
|
|
1015
1034
|
hooks: outputConfig.hooks || {},
|
|
1016
1035
|
});
|
|
1017
1036
|
};
|
|
1018
|
-
await Promise.all(outputs.map(process));
|
|
1019
|
-
}, filename),
|
|
1037
|
+
await context.profiler.run(() => Promise.all(outputs.map(process)), `Codegen: ${filename}`);
|
|
1038
|
+
}, filename, `Generate: ${filename}`),
|
|
1020
1039
|
},
|
|
1021
1040
|
], {
|
|
1022
1041
|
// it stops when one of tasks failed
|
|
@@ -1183,7 +1202,7 @@ const hash = (content) => crypto.createHash('sha1').update(content).digest('base
|
|
|
1183
1202
|
async function generate(input, saveToFile = true) {
|
|
1184
1203
|
const context = ensureContext(input);
|
|
1185
1204
|
const config = context.getConfig();
|
|
1186
|
-
await lifecycleHooks(config.hooks).afterStart();
|
|
1205
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterStart(), 'Lifecycle: afterStart');
|
|
1187
1206
|
let previouslyGeneratedFilenames = [];
|
|
1188
1207
|
function removeStaleFiles(config, generationResult) {
|
|
1189
1208
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1212,8 +1231,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1212
1231
|
if (config.watch) {
|
|
1213
1232
|
removeStaleFiles(config, generationResult);
|
|
1214
1233
|
}
|
|
1215
|
-
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1216
|
-
await Promise.all(generationResult.map(async (result) => {
|
|
1234
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
|
|
1235
|
+
await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
|
|
1217
1236
|
const exists = fileExists(result.filename);
|
|
1218
1237
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1219
1238
|
return;
|
|
@@ -1225,7 +1244,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1225
1244
|
previousHash = hash(readSync(result.filename));
|
|
1226
1245
|
}
|
|
1227
1246
|
if (previousHash && currentHash === previousHash) {
|
|
1228
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1247
|
+
debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
|
|
1229
1248
|
return;
|
|
1230
1249
|
}
|
|
1231
1250
|
if (content.length === 0) {
|
|
@@ -1242,17 +1261,20 @@ async function generate(input, saveToFile = true) {
|
|
|
1242
1261
|
writeSync(absolutePath, result.content);
|
|
1243
1262
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1244
1263
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1245
|
-
}));
|
|
1246
|
-
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1264
|
+
})), 'Write files');
|
|
1265
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: afterAllFileWrite');
|
|
1247
1266
|
return generationResult;
|
|
1248
1267
|
}
|
|
1249
1268
|
// watch mode
|
|
1250
1269
|
if (config.watch) {
|
|
1251
1270
|
return createWatcher(context, writeOutput);
|
|
1252
1271
|
}
|
|
1253
|
-
const outputFiles = await executeCodegen(context);
|
|
1254
|
-
await writeOutput(outputFiles);
|
|
1255
|
-
lifecycleHooks(config.hooks).beforeDone();
|
|
1272
|
+
const outputFiles = await context.profiler.run(() => executeCodegen(context), 'executeCodegen');
|
|
1273
|
+
await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
|
|
1274
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
|
|
1275
|
+
if (context.profilerOutput) {
|
|
1276
|
+
writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1277
|
+
}
|
|
1256
1278
|
return outputFiles;
|
|
1257
1279
|
}
|
|
1258
1280
|
function shouldOverwrite(config, outputPath) {
|
package/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
1
|
+
import { Types, Profiler } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { GraphQLConfig } from 'graphql-config';
|
|
3
3
|
import { GraphQLSchema } from 'graphql';
|
|
4
4
|
export declare type YamlCliFlags = {
|
|
@@ -9,6 +9,7 @@ export declare type YamlCliFlags = {
|
|
|
9
9
|
project: string;
|
|
10
10
|
silent: boolean;
|
|
11
11
|
errorsOnly: boolean;
|
|
12
|
+
profile: boolean;
|
|
12
13
|
};
|
|
13
14
|
export declare function generateSearchPlaces(moduleName: string): string[];
|
|
14
15
|
export interface LoadCodegenConfigOptions {
|
|
@@ -74,6 +75,10 @@ export declare function buildOptions(): {
|
|
|
74
75
|
describe: string;
|
|
75
76
|
type: "boolean";
|
|
76
77
|
};
|
|
78
|
+
profile: {
|
|
79
|
+
describe: string;
|
|
80
|
+
type: "boolean";
|
|
81
|
+
};
|
|
77
82
|
p: {
|
|
78
83
|
alias: string;
|
|
79
84
|
describe: string;
|
|
@@ -91,6 +96,8 @@ export declare class CodegenContext {
|
|
|
91
96
|
private _pluginContext;
|
|
92
97
|
cwd: string;
|
|
93
98
|
filepath: string;
|
|
99
|
+
profiler: Profiler;
|
|
100
|
+
profilerOutput?: string;
|
|
94
101
|
constructor({ config, graphqlConfig, filepath, }: {
|
|
95
102
|
config?: Types.Config;
|
|
96
103
|
graphqlConfig?: GraphQLConfig;
|
|
@@ -99,6 +106,7 @@ export declare class CodegenContext {
|
|
|
99
106
|
useProject(name?: string): void;
|
|
100
107
|
getConfig<T>(extraConfig?: T): T & Types.Config;
|
|
101
108
|
updateConfig(config: Partial<Types.Config>): void;
|
|
109
|
+
useProfiler(): void;
|
|
102
110
|
getPluginContext(): {
|
|
103
111
|
[key: string]: any;
|
|
104
112
|
};
|
package/index.js
CHANGED
|
@@ -594,6 +594,10 @@ function buildOptions() {
|
|
|
594
594
|
describe: 'Only print errors',
|
|
595
595
|
type: 'boolean',
|
|
596
596
|
},
|
|
597
|
+
profile: {
|
|
598
|
+
describe: 'Use profiler to measure performance',
|
|
599
|
+
type: 'boolean',
|
|
600
|
+
},
|
|
597
601
|
p: {
|
|
598
602
|
alias: 'project',
|
|
599
603
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -635,6 +639,9 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
635
639
|
if (cliFlags.project) {
|
|
636
640
|
context.useProject(cliFlags.project);
|
|
637
641
|
}
|
|
642
|
+
if (cliFlags.profile === true) {
|
|
643
|
+
context.useProfiler();
|
|
644
|
+
}
|
|
638
645
|
context.updateConfig(config);
|
|
639
646
|
}
|
|
640
647
|
class CodegenContext {
|
|
@@ -644,6 +651,7 @@ class CodegenContext {
|
|
|
644
651
|
this._graphqlConfig = graphqlConfig;
|
|
645
652
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
646
653
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
654
|
+
this.profiler = pluginHelpers.createNoopProfiler();
|
|
647
655
|
}
|
|
648
656
|
useProject(name) {
|
|
649
657
|
this._project = name;
|
|
@@ -674,6 +682,13 @@ class CodegenContext {
|
|
|
674
682
|
...config,
|
|
675
683
|
};
|
|
676
684
|
}
|
|
685
|
+
useProfiler() {
|
|
686
|
+
this.profiler = pluginHelpers.createProfiler();
|
|
687
|
+
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
688
|
+
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
689
|
+
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
690
|
+
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
691
|
+
}
|
|
677
692
|
getPluginContext() {
|
|
678
693
|
return this._pluginContext;
|
|
679
694
|
}
|
|
@@ -709,19 +724,6 @@ const makeDefaultLoader = (from) => {
|
|
|
709
724
|
};
|
|
710
725
|
};
|
|
711
726
|
async function executeCodegen(input) {
|
|
712
|
-
function wrapTask(task, source) {
|
|
713
|
-
return async () => {
|
|
714
|
-
try {
|
|
715
|
-
await Promise.resolve().then(() => task());
|
|
716
|
-
}
|
|
717
|
-
catch (error) {
|
|
718
|
-
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
719
|
-
error.source = source;
|
|
720
|
-
}
|
|
721
|
-
throw error;
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
}
|
|
725
727
|
const context = ensureContext(input);
|
|
726
728
|
const config = context.getConfig();
|
|
727
729
|
const pluginContext = context.getPluginContext();
|
|
@@ -757,6 +759,21 @@ async function executeCodegen(input) {
|
|
|
757
759
|
let rootSchemas;
|
|
758
760
|
let rootDocuments;
|
|
759
761
|
const generates = {};
|
|
762
|
+
function wrapTask(task, source, taskName) {
|
|
763
|
+
return () => {
|
|
764
|
+
return context.profiler.run(async () => {
|
|
765
|
+
try {
|
|
766
|
+
await Promise.resolve().then(() => task());
|
|
767
|
+
}
|
|
768
|
+
catch (error) {
|
|
769
|
+
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
770
|
+
error.source = source;
|
|
771
|
+
}
|
|
772
|
+
throw error;
|
|
773
|
+
}
|
|
774
|
+
}, taskName);
|
|
775
|
+
};
|
|
776
|
+
}
|
|
760
777
|
async function normalize() {
|
|
761
778
|
/* Load Require extensions */
|
|
762
779
|
const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
|
|
@@ -860,7 +877,7 @@ async function executeCodegen(input) {
|
|
|
860
877
|
}
|
|
861
878
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
862
879
|
outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
863
|
-
}, filename),
|
|
880
|
+
}, filename, `Load GraphQL schemas: ${filename}`),
|
|
864
881
|
},
|
|
865
882
|
{
|
|
866
883
|
title: 'Load GraphQL documents',
|
|
@@ -871,7 +888,7 @@ async function executeCodegen(input) {
|
|
|
871
888
|
if (documents.length > 0) {
|
|
872
889
|
outputDocuments.push(...documents);
|
|
873
890
|
}
|
|
874
|
-
}, filename),
|
|
891
|
+
}, filename, `Load GraphQL documents: ${filename}`),
|
|
875
892
|
},
|
|
876
893
|
{
|
|
877
894
|
title: 'Generate',
|
|
@@ -899,7 +916,7 @@ async function executeCodegen(input) {
|
|
|
899
916
|
};
|
|
900
917
|
let outputs = [];
|
|
901
918
|
if (hasPreset) {
|
|
902
|
-
outputs = await preset.buildGeneratesSection({
|
|
919
|
+
outputs = await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
903
920
|
baseOutputDir: filename,
|
|
904
921
|
presetConfig: outputConfig.presetConfig || {},
|
|
905
922
|
plugins: normalizedPluginsArray,
|
|
@@ -909,7 +926,8 @@ async function executeCodegen(input) {
|
|
|
909
926
|
config: mergedConfig,
|
|
910
927
|
pluginMap,
|
|
911
928
|
pluginContext,
|
|
912
|
-
|
|
929
|
+
profiler: context.profiler,
|
|
930
|
+
}), `Build Generates Section: ${filename}`);
|
|
913
931
|
}
|
|
914
932
|
else {
|
|
915
933
|
outputs = [
|
|
@@ -922,6 +940,7 @@ async function executeCodegen(input) {
|
|
|
922
940
|
config: mergedConfig,
|
|
923
941
|
pluginMap,
|
|
924
942
|
pluginContext,
|
|
943
|
+
profiler: context.profiler,
|
|
925
944
|
},
|
|
926
945
|
];
|
|
927
946
|
}
|
|
@@ -933,8 +952,8 @@ async function executeCodegen(input) {
|
|
|
933
952
|
hooks: outputConfig.hooks || {},
|
|
934
953
|
});
|
|
935
954
|
};
|
|
936
|
-
await Promise.all(outputs.map(process));
|
|
937
|
-
}, filename),
|
|
955
|
+
await context.profiler.run(() => Promise.all(outputs.map(process)), `Codegen: ${filename}`);
|
|
956
|
+
}, filename, `Generate: ${filename}`),
|
|
938
957
|
},
|
|
939
958
|
], {
|
|
940
959
|
// it stops when one of tasks failed
|
|
@@ -1184,7 +1203,7 @@ const hash = (content) => crypto.createHash('sha1').update(content).digest('base
|
|
|
1184
1203
|
async function generate(input, saveToFile = true) {
|
|
1185
1204
|
const context = ensureContext(input);
|
|
1186
1205
|
const config = context.getConfig();
|
|
1187
|
-
await lifecycleHooks(config.hooks).afterStart();
|
|
1206
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterStart(), 'Lifecycle: afterStart');
|
|
1188
1207
|
let previouslyGeneratedFilenames = [];
|
|
1189
1208
|
function removeStaleFiles(config, generationResult) {
|
|
1190
1209
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1213,8 +1232,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1213
1232
|
if (config.watch) {
|
|
1214
1233
|
removeStaleFiles(config, generationResult);
|
|
1215
1234
|
}
|
|
1216
|
-
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1217
|
-
await Promise.all(generationResult.map(async (result) => {
|
|
1235
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
|
|
1236
|
+
await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
|
|
1218
1237
|
const exists = fileExists(result.filename);
|
|
1219
1238
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1220
1239
|
return;
|
|
@@ -1226,7 +1245,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1226
1245
|
previousHash = hash(readSync(result.filename));
|
|
1227
1246
|
}
|
|
1228
1247
|
if (previousHash && currentHash === previousHash) {
|
|
1229
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1248
|
+
debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
|
|
1230
1249
|
return;
|
|
1231
1250
|
}
|
|
1232
1251
|
if (content.length === 0) {
|
|
@@ -1243,17 +1262,20 @@ async function generate(input, saveToFile = true) {
|
|
|
1243
1262
|
writeSync(absolutePath, result.content);
|
|
1244
1263
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1245
1264
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1246
|
-
}));
|
|
1247
|
-
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1265
|
+
})), 'Write files');
|
|
1266
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: afterAllFileWrite');
|
|
1248
1267
|
return generationResult;
|
|
1249
1268
|
}
|
|
1250
1269
|
// watch mode
|
|
1251
1270
|
if (config.watch) {
|
|
1252
1271
|
return createWatcher(context, writeOutput);
|
|
1253
1272
|
}
|
|
1254
|
-
const outputFiles = await executeCodegen(context);
|
|
1255
|
-
await writeOutput(outputFiles);
|
|
1256
|
-
lifecycleHooks(config.hooks).beforeDone();
|
|
1273
|
+
const outputFiles = await context.profiler.run(() => executeCodegen(context), 'executeCodegen');
|
|
1274
|
+
await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
|
|
1275
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
|
|
1276
|
+
if (context.profilerOutput) {
|
|
1277
|
+
writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1278
|
+
}
|
|
1257
1279
|
return outputFiles;
|
|
1258
1280
|
}
|
|
1259
1281
|
function shouldOverwrite(config, outputPath) {
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isDetailedError, DetailedError, normalizeInstanceOrArray, normalizeConfig, normalizeOutputParam, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
|
|
1
|
+
import { isDetailedError, DetailedError, createNoopProfiler, createProfiler, normalizeInstanceOrArray, normalizeConfig, normalizeOutputParam, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { codegen } from '@graphql-codegen/core';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import logSymbols from 'log-symbols';
|
|
@@ -24,7 +24,7 @@ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
|
|
|
24
24
|
import { UrlLoader } from '@graphql-tools/url-loader';
|
|
25
25
|
import yaml from 'yaml';
|
|
26
26
|
import { createRequire } from 'module';
|
|
27
|
-
import fs, { promises,
|
|
27
|
+
import fs, { promises, writeFileSync, readFileSync, statSync, unlink } from 'fs';
|
|
28
28
|
import Listr from 'listr';
|
|
29
29
|
import { exec } from 'child_process';
|
|
30
30
|
import isGlob from 'is-glob';
|
|
@@ -567,6 +567,10 @@ function buildOptions() {
|
|
|
567
567
|
describe: 'Only print errors',
|
|
568
568
|
type: 'boolean',
|
|
569
569
|
},
|
|
570
|
+
profile: {
|
|
571
|
+
describe: 'Use profiler to measure performance',
|
|
572
|
+
type: 'boolean',
|
|
573
|
+
},
|
|
570
574
|
p: {
|
|
571
575
|
alias: 'project',
|
|
572
576
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -608,6 +612,9 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
608
612
|
if (cliFlags.project) {
|
|
609
613
|
context.useProject(cliFlags.project);
|
|
610
614
|
}
|
|
615
|
+
if (cliFlags.profile === true) {
|
|
616
|
+
context.useProfiler();
|
|
617
|
+
}
|
|
611
618
|
context.updateConfig(config);
|
|
612
619
|
}
|
|
613
620
|
class CodegenContext {
|
|
@@ -617,6 +624,7 @@ class CodegenContext {
|
|
|
617
624
|
this._graphqlConfig = graphqlConfig;
|
|
618
625
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
619
626
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
627
|
+
this.profiler = createNoopProfiler();
|
|
620
628
|
}
|
|
621
629
|
useProject(name) {
|
|
622
630
|
this._project = name;
|
|
@@ -647,6 +655,13 @@ class CodegenContext {
|
|
|
647
655
|
...config,
|
|
648
656
|
};
|
|
649
657
|
}
|
|
658
|
+
useProfiler() {
|
|
659
|
+
this.profiler = createProfiler();
|
|
660
|
+
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
661
|
+
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
662
|
+
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
663
|
+
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
664
|
+
}
|
|
650
665
|
getPluginContext() {
|
|
651
666
|
return this._pluginContext;
|
|
652
667
|
}
|
|
@@ -682,19 +697,6 @@ const makeDefaultLoader = (from) => {
|
|
|
682
697
|
};
|
|
683
698
|
};
|
|
684
699
|
async function executeCodegen(input) {
|
|
685
|
-
function wrapTask(task, source) {
|
|
686
|
-
return async () => {
|
|
687
|
-
try {
|
|
688
|
-
await Promise.resolve().then(() => task());
|
|
689
|
-
}
|
|
690
|
-
catch (error) {
|
|
691
|
-
if (source && !(error instanceof GraphQLError)) {
|
|
692
|
-
error.source = source;
|
|
693
|
-
}
|
|
694
|
-
throw error;
|
|
695
|
-
}
|
|
696
|
-
};
|
|
697
|
-
}
|
|
698
700
|
const context = ensureContext(input);
|
|
699
701
|
const config = context.getConfig();
|
|
700
702
|
const pluginContext = context.getPluginContext();
|
|
@@ -730,6 +732,21 @@ async function executeCodegen(input) {
|
|
|
730
732
|
let rootSchemas;
|
|
731
733
|
let rootDocuments;
|
|
732
734
|
const generates = {};
|
|
735
|
+
function wrapTask(task, source, taskName) {
|
|
736
|
+
return () => {
|
|
737
|
+
return context.profiler.run(async () => {
|
|
738
|
+
try {
|
|
739
|
+
await Promise.resolve().then(() => task());
|
|
740
|
+
}
|
|
741
|
+
catch (error) {
|
|
742
|
+
if (source && !(error instanceof GraphQLError)) {
|
|
743
|
+
error.source = source;
|
|
744
|
+
}
|
|
745
|
+
throw error;
|
|
746
|
+
}
|
|
747
|
+
}, taskName);
|
|
748
|
+
};
|
|
749
|
+
}
|
|
733
750
|
async function normalize() {
|
|
734
751
|
/* Load Require extensions */
|
|
735
752
|
const requireExtensions = normalizeInstanceOrArray(config.require);
|
|
@@ -833,7 +850,7 @@ async function executeCodegen(input) {
|
|
|
833
850
|
}
|
|
834
851
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
835
852
|
outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
836
|
-
}, filename),
|
|
853
|
+
}, filename, `Load GraphQL schemas: ${filename}`),
|
|
837
854
|
},
|
|
838
855
|
{
|
|
839
856
|
title: 'Load GraphQL documents',
|
|
@@ -844,7 +861,7 @@ async function executeCodegen(input) {
|
|
|
844
861
|
if (documents.length > 0) {
|
|
845
862
|
outputDocuments.push(...documents);
|
|
846
863
|
}
|
|
847
|
-
}, filename),
|
|
864
|
+
}, filename, `Load GraphQL documents: ${filename}`),
|
|
848
865
|
},
|
|
849
866
|
{
|
|
850
867
|
title: 'Generate',
|
|
@@ -872,7 +889,7 @@ async function executeCodegen(input) {
|
|
|
872
889
|
};
|
|
873
890
|
let outputs = [];
|
|
874
891
|
if (hasPreset) {
|
|
875
|
-
outputs = await preset.buildGeneratesSection({
|
|
892
|
+
outputs = await context.profiler.run(async () => preset.buildGeneratesSection({
|
|
876
893
|
baseOutputDir: filename,
|
|
877
894
|
presetConfig: outputConfig.presetConfig || {},
|
|
878
895
|
plugins: normalizedPluginsArray,
|
|
@@ -882,7 +899,8 @@ async function executeCodegen(input) {
|
|
|
882
899
|
config: mergedConfig,
|
|
883
900
|
pluginMap,
|
|
884
901
|
pluginContext,
|
|
885
|
-
|
|
902
|
+
profiler: context.profiler,
|
|
903
|
+
}), `Build Generates Section: ${filename}`);
|
|
886
904
|
}
|
|
887
905
|
else {
|
|
888
906
|
outputs = [
|
|
@@ -895,6 +913,7 @@ async function executeCodegen(input) {
|
|
|
895
913
|
config: mergedConfig,
|
|
896
914
|
pluginMap,
|
|
897
915
|
pluginContext,
|
|
916
|
+
profiler: context.profiler,
|
|
898
917
|
},
|
|
899
918
|
];
|
|
900
919
|
}
|
|
@@ -906,8 +925,8 @@ async function executeCodegen(input) {
|
|
|
906
925
|
hooks: outputConfig.hooks || {},
|
|
907
926
|
});
|
|
908
927
|
};
|
|
909
|
-
await Promise.all(outputs.map(process));
|
|
910
|
-
}, filename),
|
|
928
|
+
await context.profiler.run(() => Promise.all(outputs.map(process)), `Codegen: ${filename}`);
|
|
929
|
+
}, filename, `Generate: ${filename}`),
|
|
911
930
|
},
|
|
912
931
|
], {
|
|
913
932
|
// it stops when one of tasks failed
|
|
@@ -1157,7 +1176,7 @@ const hash = (content) => createHash('sha1').update(content).digest('base64');
|
|
|
1157
1176
|
async function generate(input, saveToFile = true) {
|
|
1158
1177
|
const context = ensureContext(input);
|
|
1159
1178
|
const config = context.getConfig();
|
|
1160
|
-
await lifecycleHooks(config.hooks).afterStart();
|
|
1179
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterStart(), 'Lifecycle: afterStart');
|
|
1161
1180
|
let previouslyGeneratedFilenames = [];
|
|
1162
1181
|
function removeStaleFiles(config, generationResult) {
|
|
1163
1182
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1186,8 +1205,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1186
1205
|
if (config.watch) {
|
|
1187
1206
|
removeStaleFiles(config, generationResult);
|
|
1188
1207
|
}
|
|
1189
|
-
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1190
|
-
await Promise.all(generationResult.map(async (result) => {
|
|
1208
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: beforeAllFileWrite');
|
|
1209
|
+
await context.profiler.run(() => Promise.all(generationResult.map(async (result) => {
|
|
1191
1210
|
const exists = fileExists(result.filename);
|
|
1192
1211
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1193
1212
|
return;
|
|
@@ -1199,7 +1218,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1199
1218
|
previousHash = hash(readSync(result.filename));
|
|
1200
1219
|
}
|
|
1201
1220
|
if (previousHash && currentHash === previousHash) {
|
|
1202
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1221
|
+
debugLog(`Skipping file (${result.filename}) writing due to identical hash...`);
|
|
1203
1222
|
return;
|
|
1204
1223
|
}
|
|
1205
1224
|
if (content.length === 0) {
|
|
@@ -1216,17 +1235,20 @@ async function generate(input, saveToFile = true) {
|
|
|
1216
1235
|
writeSync(absolutePath, result.content);
|
|
1217
1236
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1218
1237
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1219
|
-
}));
|
|
1220
|
-
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1238
|
+
})), 'Write files');
|
|
1239
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename)), 'Lifecycle: afterAllFileWrite');
|
|
1221
1240
|
return generationResult;
|
|
1222
1241
|
}
|
|
1223
1242
|
// watch mode
|
|
1224
1243
|
if (config.watch) {
|
|
1225
1244
|
return createWatcher(context, writeOutput);
|
|
1226
1245
|
}
|
|
1227
|
-
const outputFiles = await executeCodegen(context);
|
|
1228
|
-
await writeOutput(outputFiles);
|
|
1229
|
-
lifecycleHooks(config.hooks).beforeDone();
|
|
1246
|
+
const outputFiles = await context.profiler.run(() => executeCodegen(context), 'executeCodegen');
|
|
1247
|
+
await context.profiler.run(() => writeOutput(outputFiles), 'writeOutput');
|
|
1248
|
+
await context.profiler.run(() => lifecycleHooks(config.hooks).beforeDone(), 'Lifecycle: beforeDone');
|
|
1249
|
+
if (context.profilerOutput) {
|
|
1250
|
+
writeSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1251
|
+
}
|
|
1230
1252
|
return outputFiles;
|
|
1231
1253
|
}
|
|
1232
1254
|
function shouldOverwrite(config, outputPath) {
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/cli",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0-alpha-23d229715.0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@graphql-codegen/core": "2.
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "
|
|
8
|
+
"@graphql-codegen/core": "2.4.0-alpha-23d229715.0",
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "2.4.0-alpha-23d229715.0",
|
|
10
10
|
"@graphql-tools/apollo-engine-loader": "^7.0.5",
|
|
11
11
|
"@graphql-tools/code-file-loader": "^7.0.6",
|
|
12
12
|
"@graphql-tools/git-loader": "^7.0.5",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"glob": "^7.1.6",
|
|
30
30
|
"globby": "^11.0.4",
|
|
31
31
|
"graphql-config": "^4.1.0",
|
|
32
|
-
"inquirer": "^
|
|
32
|
+
"inquirer": "^8.0.0",
|
|
33
33
|
"is-glob": "^4.0.1",
|
|
34
34
|
"json-to-pretty-yaml": "^1.2.2",
|
|
35
35
|
"latest-version": "5.1.0",
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"definition": "index.d.ts"
|
|
79
79
|
},
|
|
80
80
|
"exports": {
|
|
81
|
+
"./package.json": "./package.json",
|
|
81
82
|
".": {
|
|
82
83
|
"require": "./index.js",
|
|
83
84
|
"import": "./index.mjs"
|