1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
14 | return new (P || (P = Promise))(function (resolve, reject) {
|
15 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
16 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
17 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
18 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
19 | });
|
20 | };
|
21 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
22 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
23 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
24 | function verb(n) { return function (v) { return step([n, v]); }; }
|
25 | function step(op) {
|
26 | if (f) throw new TypeError("Generator is already executing.");
|
27 | while (_) try {
|
28 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
29 | if (y = 0, t) op = [op[0] & 2, t.value];
|
30 | switch (op[0]) {
|
31 | case 0: case 1: t = op; break;
|
32 | case 4: _.label++; return { value: op[1], done: false };
|
33 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
34 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
35 | default:
|
36 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
37 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
38 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
39 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
40 | if (t[2]) _.ops.pop();
|
41 | _.trys.pop(); continue;
|
42 | }
|
43 | op = body.call(thisArg, _);
|
44 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
45 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
46 | }
|
47 | };
|
48 | Object.defineProperty(exports, "__esModule", { value: true });
|
49 | var graphql_tools_1 = require("graphql-tools");
|
50 | var Listr = require("listr");
|
51 | var helpers_1 = require("./helpers");
|
52 | var documents_glob_1 = require("./utils/documents-glob");
|
53 | var document_loader_1 = require("./loaders/documents/document-loader");
|
54 | var validate_documents_1 = require("./loaders/documents/validate-documents");
|
55 | var prettier_1 = require("./utils/prettier");
|
56 | var listr_renderer_1 = require("./utils/listr-renderer");
|
57 | var load_1 = require("./load");
|
58 | var errors_1 = require("./errors");
|
59 | function mergeSchemas(schemas) {
|
60 | return __awaiter(this, void 0, void 0, function () {
|
61 | return __generator(this, function (_a) {
|
62 | if (schemas.length === 0) {
|
63 | return [2 , null];
|
64 | }
|
65 | else if (schemas.length === 1) {
|
66 | return [2 , schemas[0]];
|
67 | }
|
68 | else {
|
69 | return [2 , graphql_tools_1.mergeSchemas({ schemas: schemas.filter(function (s) { return s; }) })];
|
70 | }
|
71 | return [2 ];
|
72 | });
|
73 | });
|
74 | }
|
75 | function executeCodegen(config) {
|
76 | return __awaiter(this, void 0, void 0, function () {
|
77 | function wrapTask(task, source) {
|
78 | var _this = this;
|
79 | return function () { return __awaiter(_this, void 0, void 0, function () {
|
80 | var error_1;
|
81 | return __generator(this, function (_a) {
|
82 | switch (_a.label) {
|
83 | case 0:
|
84 | _a.trys.push([0, 2, , 3]);
|
85 | return [4 , task()];
|
86 | case 1:
|
87 | _a.sent();
|
88 | return [3 , 3];
|
89 | case 2:
|
90 | error_1 = _a.sent();
|
91 | if (source) {
|
92 | error_1.source = source;
|
93 | }
|
94 | throw error_1;
|
95 | case 3: return [2 ];
|
96 | }
|
97 | });
|
98 | }); };
|
99 | }
|
100 | function normalize() {
|
101 |
|
102 | var requireExtensions = helpers_1.normalizeInstanceOrArray(config.require);
|
103 | requireExtensions.forEach(function (mod) { return require(mod); });
|
104 |
|
105 | rootConfig = config.config || {};
|
106 |
|
107 | schemas = helpers_1.normalizeInstanceOrArray(config.schema);
|
108 |
|
109 | documents = helpers_1.normalizeInstanceOrArray(config.documents);
|
110 |
|
111 | for (var _i = 0, _a = Object.keys(config.generates); _i < _a.length; _i++) {
|
112 | var filename = _a[_i];
|
113 | generates[filename] = helpers_1.normalizeOutputParam(config.generates[filename]);
|
114 | }
|
115 | }
|
116 | function loadRootSchema() {
|
117 | return __awaiter(this, void 0, void 0, function () {
|
118 | var _a;
|
119 | return __generator(this, function (_b) {
|
120 | switch (_b.label) {
|
121 | case 0:
|
122 | _a = mergeSchemas;
|
123 | return [4 , Promise.all(schemas.map(function (pointToScehma) { return load_1.loadSchema(pointToScehma, config); }))];
|
124 | case 1: return [4 , _a.apply(void 0, [_b.sent()])];
|
125 | case 2:
|
126 |
|
127 | rootSchema = _b.sent();
|
128 | return [2 ];
|
129 | }
|
130 | });
|
131 | });
|
132 | }
|
133 | function loadRootDocuments() {
|
134 | return __awaiter(this, void 0, void 0, function () {
|
135 | var foundDocumentsPaths, errors;
|
136 | return __generator(this, function (_a) {
|
137 | switch (_a.label) {
|
138 | case 0:
|
139 | if (!(documents.length > 0)) return [3 , 3];
|
140 | return [4 , documents_glob_1.documentsFromGlobs(documents)];
|
141 | case 1:
|
142 | foundDocumentsPaths = _a.sent();
|
143 | return [4 , document_loader_1.loadDocumentsSources(foundDocumentsPaths)];
|
144 | case 2:
|
145 | rootDocuments = _a.sent();
|
146 | if (rootSchema) {
|
147 | errors = validate_documents_1.validateGraphQlDocuments(rootSchema, rootDocuments);
|
148 | validate_documents_1.checkValidationErrors(errors);
|
149 | }
|
150 | _a.label = 3;
|
151 | case 3: return [2 ];
|
152 | }
|
153 | });
|
154 | });
|
155 | }
|
156 | var result, commonListrOptions, verboseOptions, listrOptions, listr, rootConfig, schemas, documents, generates, rootSchema, rootDocuments;
|
157 | return __generator(this, function (_a) {
|
158 | switch (_a.label) {
|
159 | case 0:
|
160 | result = [];
|
161 | commonListrOptions = {
|
162 | exitOnError: true
|
163 | };
|
164 | verboseOptions = __assign({}, commonListrOptions, { renderer: 'verbose', nonTTYRenderer: 'verbose' });
|
165 | listrOptions = __assign({}, commonListrOptions, { renderer: config.silent ? 'silent' : listr_renderer_1.Renderer, nonTTYRenderer: config.silent ? 'silent' : 'default', collapse: true, clearOutput: false });
|
166 | listr = new Listr(process.env.VERBOSE || process.env.NODE_ENV === 'test' ? verboseOptions : listrOptions);
|
167 | rootConfig = {};
|
168 | generates = {};
|
169 | rootDocuments = [];
|
170 | listr.add({
|
171 | title: 'Parse configuration',
|
172 | task: function (ctx) {
|
173 | normalize();
|
174 | ctx.hasSchemas = schemas.length > 0;
|
175 | ctx.hasDocuments = documents.length > 0;
|
176 | }
|
177 | });
|
178 | listr.add({
|
179 | title: 'Load schema',
|
180 | enabled: function (ctx) { return ctx.hasSchemas; },
|
181 | task: wrapTask(loadRootSchema)
|
182 | });
|
183 | listr.add({
|
184 | title: 'Load documents',
|
185 | enabled: function (ctx) { return ctx.hasDocuments; },
|
186 | task: wrapTask(loadRootDocuments)
|
187 | });
|
188 | listr.add({
|
189 | title: 'Generate outputs',
|
190 | task: function () {
|
191 | return new Listr(Object.keys(generates).map(function (filename, i) { return ({
|
192 | title: "Generate " + filename,
|
193 | task: function () {
|
194 | var outputConfig = generates[filename];
|
195 | var outputFileTemplateConfig = outputConfig.config || {};
|
196 | var outputSchema = rootSchema;
|
197 | var outputDocuments = rootDocuments;
|
198 | var outputSpecificSchemas = helpers_1.normalizeInstanceOrArray(outputConfig.schema);
|
199 | var outputSpecificDocuments = helpers_1.normalizeInstanceOrArray(outputConfig.documents);
|
200 | function addSchema() {
|
201 | return __awaiter(this, void 0, void 0, function () {
|
202 | var _a, _b, _c;
|
203 | return __generator(this, function (_d) {
|
204 | switch (_d.label) {
|
205 | case 0:
|
206 | _a = mergeSchemas;
|
207 | _c = (_b = [
|
208 | rootSchema
|
209 | ]).concat;
|
210 | return [4 , Promise.all(outputSpecificSchemas.map(function (pointToScehma) { return load_1.loadSchema(pointToScehma, config); }))];
|
211 | case 1: return [4 , _a.apply(void 0, [_c.apply(_b, [(_d.sent())])])];
|
212 | case 2:
|
213 | outputSchema = _d.sent();
|
214 | return [2 ];
|
215 | }
|
216 | });
|
217 | });
|
218 | }
|
219 | function addDocuments() {
|
220 | return __awaiter(this, void 0, void 0, function () {
|
221 | var foundDocumentsPaths, additionalDocs, errors;
|
222 | return __generator(this, function (_a) {
|
223 | switch (_a.label) {
|
224 | case 0: return [4 , documents_glob_1.documentsFromGlobs(outputSpecificDocuments)];
|
225 | case 1:
|
226 | foundDocumentsPaths = _a.sent();
|
227 | return [4 , document_loader_1.loadDocumentsSources(foundDocumentsPaths)];
|
228 | case 2:
|
229 | additionalDocs = _a.sent();
|
230 | if (outputSchema) {
|
231 | errors = validate_documents_1.validateGraphQlDocuments(outputSchema, additionalDocs);
|
232 | validate_documents_1.checkValidationErrors(errors);
|
233 | }
|
234 | outputDocuments = rootDocuments.concat(additionalDocs);
|
235 | return [2 ];
|
236 | }
|
237 | });
|
238 | });
|
239 | }
|
240 | function doGenerateOutput() {
|
241 | return __awaiter(this, void 0, void 0, function () {
|
242 | var normalizedPluginsArray, output;
|
243 | return __generator(this, function (_a) {
|
244 | switch (_a.label) {
|
245 | case 0:
|
246 | normalizedPluginsArray = helpers_1.normalizeConfig(outputConfig.plugins);
|
247 | return [4 , generateOutput({
|
248 | filename: filename,
|
249 | plugins: normalizedPluginsArray,
|
250 | schema: outputSchema,
|
251 | documents: outputDocuments,
|
252 | inheritedConfig: __assign({}, rootConfig, outputFileTemplateConfig)
|
253 | })];
|
254 | case 1:
|
255 | output = _a.sent();
|
256 | result.push(output);
|
257 | return [2 ];
|
258 | }
|
259 | });
|
260 | });
|
261 | }
|
262 | return new Listr([
|
263 | {
|
264 | title: 'Add related schemas',
|
265 | enabled: function () { return outputSpecificSchemas.length > 0; },
|
266 | task: wrapTask(addSchema, filename)
|
267 | },
|
268 | {
|
269 | title: 'Add related documents',
|
270 | enabled: function () { return outputSpecificDocuments.length > 0; },
|
271 | task: wrapTask(addDocuments, filename)
|
272 | },
|
273 | {
|
274 | title: 'Generate',
|
275 | task: wrapTask(doGenerateOutput, filename)
|
276 | }
|
277 | ], {
|
278 |
|
279 | exitOnError: true
|
280 | });
|
281 | }
|
282 | }); }), {
|
283 |
|
284 | exitOnError: false,
|
285 |
|
286 | concurrent: 4
|
287 | });
|
288 | }
|
289 | });
|
290 | return [4 , listr.run()];
|
291 | case 1:
|
292 | _a.sent();
|
293 | return [2 , result];
|
294 | }
|
295 | });
|
296 | });
|
297 | }
|
298 | exports.executeCodegen = executeCodegen;
|
299 | function generateOutput(options) {
|
300 | return __awaiter(this, void 0, void 0, function () {
|
301 | var output, _i, _a, plugin, name_1, pluginConfig, result, _b;
|
302 | return __generator(this, function (_c) {
|
303 | switch (_c.label) {
|
304 | case 0:
|
305 | output = '';
|
306 | _i = 0, _a = options.plugins;
|
307 | _c.label = 1;
|
308 | case 1:
|
309 | if (!(_i < _a.length)) return [3 , 4];
|
310 | plugin = _a[_i];
|
311 | name_1 = Object.keys(plugin)[0];
|
312 | pluginConfig = plugin[name_1];
|
313 | return [4 , executePlugin({
|
314 | name: name_1,
|
315 | config: typeof pluginConfig !== 'object'
|
316 | ? pluginConfig
|
317 | : __assign({}, options.inheritedConfig, pluginConfig),
|
318 | schema: options.schema,
|
319 | documents: options.documents,
|
320 | outputFilename: options.filename,
|
321 | allPlugins: options.plugins
|
322 | })];
|
323 | case 2:
|
324 | result = _c.sent();
|
325 | output += result;
|
326 | _c.label = 3;
|
327 | case 3:
|
328 | _i++;
|
329 | return [3 , 1];
|
330 | case 4:
|
331 | _b = { filename: options.filename };
|
332 | return [4 , prettier_1.prettify(options.filename, output)];
|
333 | case 5: return [2 , (_b.content = _c.sent(), _b)];
|
334 | }
|
335 | });
|
336 | });
|
337 | }
|
338 | exports.generateOutput = generateOutput;
|
339 | function getPluginByName(name) {
|
340 | return __awaiter(this, void 0, void 0, function () {
|
341 | var possibleNames, _i, possibleNames_1, packageName, possibleNamesMsg;
|
342 | return __generator(this, function (_a) {
|
343 | possibleNames = [
|
344 | "graphql-codegen-" + name,
|
345 | "graphql-codegen-" + name + "-template",
|
346 | "codegen-" + name,
|
347 | "codegen-" + name + "-template",
|
348 | name
|
349 | ];
|
350 | for (_i = 0, possibleNames_1 = possibleNames; _i < possibleNames_1.length; _i++) {
|
351 | packageName = possibleNames_1[_i];
|
352 | try {
|
353 | return [2 , require(packageName)];
|
354 | }
|
355 | catch (err) {
|
356 | if (err.message.indexOf("Cannot find module '" + packageName + "'") === -1) {
|
357 | throw new errors_1.DetailedError("Unable to load template plugin matching " + name, "\n Unable to load template plugin matching '" + name + "'.\n Reason: \n " + err.message + "\n ");
|
358 | }
|
359 | }
|
360 | }
|
361 | possibleNamesMsg = possibleNames
|
362 | .map(function (name) {
|
363 | return ("\n - " + name + "\n ").trimRight();
|
364 | })
|
365 | .join('');
|
366 | throw new errors_1.DetailedError("Unable to find template plugin matching " + name, "\n Unable to find template plugin matching '" + name + "'\n Install one of the following packages:\n \n " + possibleNamesMsg + "\n ");
|
367 | });
|
368 | });
|
369 | }
|
370 | exports.getPluginByName = getPluginByName;
|
371 | function executePlugin(options) {
|
372 | return __awaiter(this, void 0, void 0, function () {
|
373 | var pluginPackage, schema, _a, e_1;
|
374 | return __generator(this, function (_b) {
|
375 | switch (_b.label) {
|
376 | case 0: return [4 , getPluginByName(options.name)];
|
377 | case 1:
|
378 | pluginPackage = _b.sent();
|
379 | if (!!pluginPackage.addToSchema) return [3 , 2];
|
380 | _a = options.schema;
|
381 | return [3 , 4];
|
382 | case 2: return [4 , mergeSchemas([
|
383 | options.schema,
|
384 | graphql_tools_1.makeExecutableSchema({
|
385 | typeDefs: pluginPackage.addToSchema,
|
386 | allowUndefinedInResolve: true,
|
387 | resolverValidationOptions: {
|
388 | requireResolversForResolveType: false,
|
389 | requireResolversForAllFields: false,
|
390 | requireResolversForNonScalar: false,
|
391 | requireResolversForArgs: false
|
392 | }
|
393 | })
|
394 | ])];
|
395 | case 3:
|
396 | _a = _b.sent();
|
397 | _b.label = 4;
|
398 | case 4:
|
399 | schema = _a;
|
400 | if (!(pluginPackage.validate && typeof pluginPackage.validate === 'function')) return [3 , 8];
|
401 | _b.label = 5;
|
402 | case 5:
|
403 | _b.trys.push([5, 7, , 8]);
|
404 | return [4 , pluginPackage.validate(schema, options.documents, options.config, options.outputFilename, options.allPlugins)];
|
405 | case 6:
|
406 | _b.sent();
|
407 | return [3 , 8];
|
408 | case 7:
|
409 | e_1 = _b.sent();
|
410 | return [3 , 8];
|
411 | case 8: return [2 , pluginPackage.plugin(schema, options.documents, options.config)];
|
412 | }
|
413 | });
|
414 | });
|
415 | }
|
416 | exports.executePlugin = executePlugin;
|
417 |
|
\ | No newline at end of file |