1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
11 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
12 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
13 | function verb(n) { return function (v) { return step([n, v]); }; }
|
14 | function step(op) {
|
15 | if (f) throw new TypeError("Generator is already executing.");
|
16 | while (_) try {
|
17 | if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
|
18 | if (y = 0, t) op = [0, t.value];
|
19 | switch (op[0]) {
|
20 | case 0: case 1: t = op; break;
|
21 | case 4: _.label++; return { value: op[1], done: false };
|
22 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
23 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
24 | default:
|
25 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
26 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
27 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
28 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
29 | if (t[2]) _.ops.pop();
|
30 | _.trys.pop(); continue;
|
31 | }
|
32 | op = body.call(thisArg, _);
|
33 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
34 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
35 | }
|
36 | };
|
37 | var _this = this;
|
38 | Object.defineProperty(exports, "__esModule", { value: true });
|
39 | var commander = require("commander");
|
40 | var introspection_from_file_1 = require("./loaders/introspection-from-file");
|
41 | var introspection_from_url_1 = require("./loaders/introspection-from-url");
|
42 | var schema_from_export_1 = require("./loaders/schema-from-export");
|
43 | var documents_glob_1 = require("./utils/documents-glob");
|
44 | var graphql_codegen_compiler_1 = require("graphql-codegen-compiler");
|
45 | var graphql_codegen_core_1 = require("graphql-codegen-core");
|
46 | var document_loader_1 = require("./loaders/document-loader");
|
47 | var path = require("path");
|
48 | var fs = require("fs");
|
49 | var templates_scanner_1 = require("./loaders/templates-scanner");
|
50 | var graphql_codegen_generators_1 = require("graphql-codegen-generators");
|
51 | var mkdirp = require("mkdirp");
|
52 | function collect(val, memo) {
|
53 | memo.push(val);
|
54 | return memo;
|
55 | }
|
56 | exports.initCLI = function (args) {
|
57 | commander
|
58 | .usage('gql-gen [options]')
|
59 | .option('-f, --file <filePath>', 'Parse local GraphQL introspection JSON file')
|
60 | .option('-u, --url <graphql-endpoint>', 'Parse remote GraphQL endpoint as introspection file')
|
61 | .option('-u, --export <export-file>', 'Path to a JavaScript (es5/6) file that exports (as default export) your `GraphQLSchema` object')
|
62 | .option('-h, --header [header]', 'Header to add to the introspection HTTP request when using --url', collect, [])
|
63 | .option('-t, --template <template-name>', 'Language/platform name templates')
|
64 | .option('-p, --project <project-path>', 'Project path(s) to scan for custom template files')
|
65 | .option('--project-config <json-file>', 'Project configuration file')
|
66 | .option('-m, --no-schema', 'Generates only client side documents, without server side schema types')
|
67 | .option('-c, --no-documents', 'Generates only server side schema types, without client side documents')
|
68 | .option('-o, --out <path>', 'Output file(s) path', String, './')
|
69 | .arguments('<options> [documents...]')
|
70 | .parse(args);
|
71 | return commander;
|
72 | };
|
73 | exports.cliError = function (err) {
|
74 | if (typeof err === 'object') {
|
75 | console.log(err);
|
76 | }
|
77 | console.error('Error: ' + err);
|
78 | process.exit(1);
|
79 | return;
|
80 | };
|
81 | exports.validateCliOptions = function (options) {
|
82 | var file = options.file;
|
83 | var url = options.url;
|
84 | var fsExport = options.export;
|
85 | var template = options.template;
|
86 | var project = options.project;
|
87 | if (!file && !url && !fsExport) {
|
88 | exports.cliError('Please specify one of --file, --url or --export flags!');
|
89 | }
|
90 | if (!template && !project) {
|
91 | exports.cliError('Please specify language/platform, using --template flag, or specify --project to generate with custom project!');
|
92 | }
|
93 | };
|
94 | exports.executeWithOptions = function (options) { return __awaiter(_this, void 0, void 0, function () {
|
95 | var file, url, fsExport, documents, template, project, projectConfig, out, headers, generateSchema, generateDocuments, schemaExportPromise, graphQlSchema, context, transformedDocuments, _a, _b, _c, templateConfig, configPath, config, templates;
|
96 | return __generator(this, function (_d) {
|
97 | switch (_d.label) {
|
98 | case 0:
|
99 | exports.validateCliOptions(options);
|
100 | file = options.file;
|
101 | url = options.url;
|
102 | fsExport = options.export;
|
103 | documents = options.args || [];
|
104 | template = options.template;
|
105 | project = options.project;
|
106 | projectConfig = options.projectConfig || './gql-gen.json';
|
107 | out = options.out || './';
|
108 | headers = options.headers;
|
109 | generateSchema = options.schema;
|
110 | generateDocuments = options.documents;
|
111 | if (file) {
|
112 | schemaExportPromise = introspection_from_file_1.introspectionFromFile(file).then(graphql_codegen_core_1.introspectionToGraphQLSchema);
|
113 | }
|
114 | else if (url) {
|
115 | schemaExportPromise = introspection_from_url_1.introspectionFromUrl(url, headers).then(graphql_codegen_core_1.introspectionToGraphQLSchema);
|
116 | }
|
117 | else if (fsExport) {
|
118 | schemaExportPromise = schema_from_export_1.schemaFromExport(fsExport);
|
119 | }
|
120 | return [4 , schemaExportPromise];
|
121 | case 1:
|
122 | graphQlSchema = _d.sent();
|
123 | if (process.env.VERBOSE !== undefined) {
|
124 | console.log("GraphQL Schema is: ", graphQlSchema);
|
125 | }
|
126 | context = graphql_codegen_core_1.schemaToTemplateContext(graphQlSchema);
|
127 | graphql_codegen_core_1.debugLog("[executeWithOptions] Schema template context build, the result is: ");
|
128 | Object.keys(context).forEach(function (key) {
|
129 | if (Array.isArray(context[key])) {
|
130 | graphql_codegen_core_1.debugLog("Total of " + key + ": " + context[key].length);
|
131 | }
|
132 | });
|
133 | _a = graphql_codegen_core_1.transformDocument;
|
134 | _b = [graphQlSchema];
|
135 | _c = document_loader_1.loadDocumentsSources;
|
136 | return [4 , documents_glob_1.documentsFromGlobs(documents)];
|
137 | case 2:
|
138 | transformedDocuments = _a.apply(void 0, _b.concat([_c.apply(void 0, [_d.sent()])]));
|
139 | templateConfig = null;
|
140 | if (template && template !== '') {
|
141 | graphql_codegen_core_1.debugLog("[executeWithOptions] using template: " + template);
|
142 | templateConfig = graphql_codegen_generators_1.getGeneratorConfig(template);
|
143 | if (!templateConfig) {
|
144 | throw new Error("Unknown template: " + template + "!");
|
145 | }
|
146 | }
|
147 | if (project && project !== '') {
|
148 | graphql_codegen_core_1.debugLog("[executeWithOptions] using project: " + project);
|
149 | configPath = path.resolve(process.cwd(), projectConfig);
|
150 | if (fs.existsSync(configPath)) {
|
151 | config = JSON.parse(fs.readFileSync(configPath).toString());
|
152 | graphql_codegen_core_1.debugLog("[executeWithOptions] Got project config JSON: ", config);
|
153 | templates = templates_scanner_1.scanForTemplatesInPath(project, graphql_codegen_compiler_1.ALLOWED_CUSTOM_TEMPLATE_EXT);
|
154 | templateConfig = {
|
155 | inputType: graphql_codegen_generators_1.EInputType.PROJECT,
|
156 | templates: templates,
|
157 | flattenTypes: config.flattenTypes,
|
158 | primitives: config.primitives,
|
159 | };
|
160 | }
|
161 | else {
|
162 | throw new Error("Please specify --projectConfig path or create gql-gen.json in your project root!");
|
163 | }
|
164 | }
|
165 | return [2 , graphql_codegen_compiler_1.compileTemplate(templateConfig, context, [transformedDocuments], {
|
166 | generateSchema: generateSchema,
|
167 | generateDocuments: generateDocuments,
|
168 | }).map(function (item) {
|
169 | var resultName = item.filename;
|
170 | if (!path.isAbsolute(resultName)) {
|
171 | var resolved = path.resolve(process.cwd(), out);
|
172 | if (fs.existsSync(resolved)) {
|
173 | var stats = fs.lstatSync(resolved);
|
174 | if (stats.isDirectory()) {
|
175 | resultName = path.resolve(resolved, item.filename);
|
176 | }
|
177 | else if (stats.isFile()) {
|
178 | resultName = resolved;
|
179 | }
|
180 | }
|
181 | else {
|
182 | if (out.endsWith('/')) {
|
183 | resultName = path.resolve(resolved, item.filename);
|
184 | }
|
185 | else {
|
186 | resultName = resolved;
|
187 | }
|
188 | }
|
189 | }
|
190 | var resultDir = path.dirname(resultName);
|
191 | mkdirp.sync(resultDir);
|
192 | return {
|
193 | content: item.content,
|
194 | filename: resultName,
|
195 | };
|
196 | })];
|
197 | }
|
198 | });
|
199 | }); };
|
200 |
|
\ | No newline at end of file |