UNPKG

8.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers");
4const graphql_codegen_core_1 = require("graphql-codegen-core");
5const Listr = require("listr");
6const helpers_1 = require("./helpers");
7const prettier_1 = require("./utils/prettier");
8const listr_renderer_1 = require("./utils/listr-renderer");
9const errors_1 = require("./errors");
10const load_1 = require("./load");
11const graphql_1 = require("graphql");
12const plugins_1 = require("./plugins");
13exports.defaultPluginLoader = (mod) => Promise.resolve().then(() => require(mod));
14async function executeCodegen(config) {
15 function wrapTask(task, source) {
16 return async () => {
17 try {
18 await task();
19 }
20 catch (error) {
21 if (source && !(error instanceof graphql_1.GraphQLError)) {
22 error.source = source;
23 }
24 throw error;
25 }
26 };
27 }
28 const result = [];
29 const commonListrOptions = {
30 exitOnError: true
31 };
32 let listr;
33 if (process.env.VERBOSE) {
34 listr = new Listr(Object.assign({}, commonListrOptions, { renderer: 'verbose', nonTTYRenderer: 'verbose' }));
35 }
36 else if (process.env.NODE_ENV === 'test') {
37 listr = new Listr(Object.assign({}, commonListrOptions, { renderer: 'silent', nonTTYRenderer: 'silent' }));
38 }
39 else {
40 listr = new Listr(Object.assign({}, commonListrOptions, { renderer: config.silent ? 'silent' : listr_renderer_1.Renderer, nonTTYRenderer: config.silent ? 'silent' : 'default', collapse: true, clearOutput: false }));
41 }
42 let rootConfig = {};
43 let rootSchemas;
44 let rootDocuments;
45 let generates = {};
46 async function normalize() {
47 /* Load Require extensions */
48 const requireExtensions = helpers_1.normalizeInstanceOrArray(config.require);
49 for (const mod of requireExtensions) {
50 await Promise.resolve().then(() => require(mod));
51 }
52 /* Root templates-config */
53 rootConfig = config.config || {};
54 /* Normalize root "schema" field */
55 rootSchemas = helpers_1.normalizeInstanceOrArray(config.schema);
56 /* Normalize root "documents" field */
57 rootDocuments = helpers_1.normalizeInstanceOrArray(config.documents);
58 /* Normalize "generators" field */
59 const generateKeys = Object.keys(config.generates);
60 if (generateKeys.length === 0) {
61 throw new errors_1.DetailedError('Invalid Codegen Configuration!', `
62 Please make sure that your codegen config file contains the "generates" field, with a specification for the plugins you need.
63
64 It should looks like that:
65
66 schema:
67 - my-schema.graphql
68 generates:
69 my-file.ts:
70 - plugin1
71 - plugin2
72 - plugin3
73 `);
74 }
75 for (const filename of generateKeys) {
76 generates[filename] = helpers_1.normalizeOutputParam(config.generates[filename]);
77 if (generates[filename].plugins.length === 0) {
78 throw new errors_1.DetailedError('Invalid Codegen Configuration!', `
79 Please make sure that your codegen config file has defined plugins list for output "${filename}".
80
81 It should looks like that:
82
83 schema:
84 - my-schema.graphql
85 generates:
86 my-file.ts:
87 - plugin1
88 - plugin2
89 - plugin3
90 `);
91 }
92 }
93 if (rootSchemas.length === 0 && Object.keys(generates).some(filename => generates[filename].schema.length === 0)) {
94 throw new errors_1.DetailedError('Invalid Codegen Configuration!', `
95 Please make sure that your codegen config file contains either the "schema" field
96 or every generated file has its own "schema" field.
97
98 It should looks like that:
99 schema:
100 - my-schema.graphql
101
102 or:
103 generates:
104 path/to/output:
105 schema: my-schema.graphql
106 `);
107 }
108 }
109 listr.add({
110 title: 'Parse configuration',
111 task: () => normalize()
112 });
113 listr.add({
114 title: 'Generate outputs',
115 task: () => {
116 return new Listr(Object.keys(generates).map((filename, i) => ({
117 title: `Generate ${filename}`,
118 task: () => {
119 const outputConfig = generates[filename];
120 const outputFileTemplateConfig = outputConfig.config || {};
121 const outputDocuments = [];
122 let outputSchema;
123 const outputSpecificSchemas = helpers_1.normalizeInstanceOrArray(outputConfig.schema);
124 const outputSpecificDocuments = helpers_1.normalizeInstanceOrArray(outputConfig.documents);
125 return new Listr([
126 {
127 title: 'Load GraphQL schemas',
128 task: wrapTask(async () => {
129 graphql_codegen_plugin_helpers_1.debugLog(`[CLI] Loading Schemas`);
130 const allSchemas = [
131 ...rootSchemas.map(pointToScehma => load_1.loadSchema(pointToScehma, config)),
132 ...outputSpecificSchemas.map(pointToScehma => load_1.loadSchema(pointToScehma, config))
133 ];
134 if (allSchemas.length > 0) {
135 outputSchema = await graphql_codegen_core_1.mergeSchemas(await Promise.all(allSchemas));
136 }
137 }, filename)
138 },
139 {
140 title: 'Load GraphQL documents',
141 task: wrapTask(async () => {
142 graphql_codegen_plugin_helpers_1.debugLog(`[CLI] Loading Documents`);
143 const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
144 for (const docDef of allDocuments) {
145 const documents = await load_1.loadDocuments(docDef, config);
146 if (documents.length > 0) {
147 outputDocuments.push(...documents);
148 }
149 }
150 }, filename)
151 },
152 {
153 title: 'Generate',
154 task: wrapTask(async () => {
155 graphql_codegen_plugin_helpers_1.debugLog(`[CLI] Generating output`);
156 const normalizedPluginsArray = helpers_1.normalizeConfig(outputConfig.plugins);
157 const pluginLoader = config.pluginLoader || exports.defaultPluginLoader;
158 const pluginPackages = await Promise.all(normalizedPluginsArray.map(plugin => plugins_1.getPluginByName(Object.keys(plugin)[0], pluginLoader)));
159 const pluginMap = {};
160 pluginPackages.forEach((pluginPackage, i) => {
161 const plugin = normalizedPluginsArray[i];
162 const name = Object.keys(plugin)[0];
163 pluginMap[name] = pluginPackage;
164 });
165 const output = await graphql_codegen_core_1.codegen({
166 filename,
167 plugins: normalizedPluginsArray,
168 schema: outputSchema,
169 documents: outputDocuments,
170 config: Object.assign({}, rootConfig, outputFileTemplateConfig),
171 pluginMap
172 });
173 result.push({
174 filename,
175 content: await prettier_1.prettify(filename, output)
176 });
177 }, filename)
178 }
179 ], {
180 // it stops when one of tasks failed
181 exitOnError: true
182 });
183 }
184 })), {
185 // it doesn't stop when one of tasks failed, to finish at least some of outputs
186 exitOnError: false,
187 // run 4 at once
188 concurrent: 4
189 });
190 }
191 });
192 await listr.run();
193 return result;
194}
195exports.executeCodegen = executeCodegen;
196//# sourceMappingURL=codegen.js.map
\No newline at end of file