1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | var graphql_codegen_core_1 = require("graphql-codegen-core");
|
12 | var sanitizie_filename_1 = require("./sanitizie-filename");
|
13 | var prepare_documents_only_1 = require("./prepare-documents-only");
|
14 | var path = require("path");
|
15 | var moment = require("moment");
|
16 | var handlersMap = {
|
17 | type: handleType,
|
18 | inputType: handleInputType,
|
19 | union: handleUnion,
|
20 | enum: handleEnum,
|
21 | scalar: handleScalar,
|
22 | interface: handleInterface,
|
23 | operation: handleOperation,
|
24 | fragment: handleFragment,
|
25 | schema: handleSchema,
|
26 | documents: handleDocuments,
|
27 | all: handleAll
|
28 | };
|
29 | exports.ALLOWED_CUSTOM_TEMPLATE_EXT = ['template', 'handlebars', 'tmpl', 'gqlgen'];
|
30 | function handleSchema(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
31 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
32 | graphql_codegen_core_1.debugLog("[handleSchema] called");
|
33 | return [
|
34 | {
|
35 | filename: prefixAndPath + '.' + (fileExtension || ''),
|
36 | content: compiledTemplate(__assign({}, extraConfig, schemaContext))
|
37 | }
|
38 | ];
|
39 | }
|
40 | function handleAll(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
41 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
42 | graphql_codegen_core_1.debugLog("[handleAll] called");
|
43 | return [
|
44 | {
|
45 | filename: prefixAndPath + '.' + (fileExtension || ''),
|
46 | content: compiledTemplate(__assign({}, schemaContext, extraConfig, { operations: documents.operations, fragments: documents.fragments, hasFragments: documents.hasFragments, hasOperations: documents.hasOperations }))
|
47 | }
|
48 | ];
|
49 | }
|
50 | function handleDocuments(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
51 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
52 | graphql_codegen_core_1.debugLog("[handleDocuments] called");
|
53 | return [
|
54 | {
|
55 | filename: prefixAndPath + '.' + (fileExtension || ''),
|
56 | content: compiledTemplate(__assign({}, extraConfig, { operations: documents.operations, fragments: documents.fragments, hasFragments: documents.hasFragments, hasOperations: documents.hasOperations }))
|
57 | }
|
58 | ];
|
59 | }
|
60 | function handleType(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
61 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
62 | graphql_codegen_core_1.debugLog("[handleType] called");
|
63 | return schemaContext.types.map(function (type) { return ({
|
64 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(type.name, 'type') + '.' + (fileExtension || ''),
|
65 | content: compiledTemplate(__assign({}, type, extraConfig))
|
66 | }); });
|
67 | }
|
68 | function handleInputType(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
69 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
70 | graphql_codegen_core_1.debugLog("[handleInputType] called");
|
71 | return schemaContext.inputTypes.map(function (type) { return ({
|
72 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(type.name, 'input-type') + '.' + (fileExtension || ''),
|
73 | content: compiledTemplate(__assign({}, type, extraConfig))
|
74 | }); });
|
75 | }
|
76 | function handleUnion(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
77 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
78 | graphql_codegen_core_1.debugLog("[handleUnion] called");
|
79 | return schemaContext.unions.map(function (union) { return ({
|
80 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(union.name, 'union') + '.' + (fileExtension || ''),
|
81 | content: compiledTemplate(__assign({}, union, extraConfig))
|
82 | }); });
|
83 | }
|
84 | function handleEnum(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
85 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
86 | graphql_codegen_core_1.debugLog("[handleEnum] called");
|
87 | return schemaContext.enums.map(function (en) { return ({
|
88 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(en.name, 'enum') + '.' + (fileExtension || ''),
|
89 | content: compiledTemplate(__assign({}, en, extraConfig))
|
90 | }); });
|
91 | }
|
92 | function handleScalar(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
93 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
94 | graphql_codegen_core_1.debugLog("[handleScalar] called");
|
95 | return schemaContext.scalars.map(function (scalar) { return ({
|
96 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(scalar.name, 'scalar') + '.' + (fileExtension || ''),
|
97 | content: compiledTemplate(__assign({}, scalar, extraConfig))
|
98 | }); });
|
99 | }
|
100 | function handleInterface(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
101 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
102 | graphql_codegen_core_1.debugLog("[handleInterface] called");
|
103 | return schemaContext.interfaces.map(function (inf) { return ({
|
104 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(inf.name, 'interface') + '.' + (fileExtension || ''),
|
105 | content: compiledTemplate(__assign({}, inf, extraConfig))
|
106 | }); });
|
107 | }
|
108 | function handleOperation(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
109 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
110 | graphql_codegen_core_1.debugLog("[handleOperation] called");
|
111 | return documents.operations.map(function (operation) { return ({
|
112 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(operation.name, operation.operationType) + '.' + (fileExtension || ''),
|
113 | content: compiledTemplate(__assign({}, operation, extraConfig))
|
114 | }); });
|
115 | }
|
116 | function handleFragment(compiledTemplate, schemaContext, documents, extraConfig, fileExtension, prefixAndPath) {
|
117 | if (prefixAndPath === void 0) { prefixAndPath = ''; }
|
118 | graphql_codegen_core_1.debugLog("[handleFragment] called");
|
119 | return documents.fragments.map(function (fragment) { return ({
|
120 | filename: prefixAndPath + sanitizie_filename_1.sanitizeFilename(fragment.name, 'fragment') + '.' + (fileExtension || ''),
|
121 | content: compiledTemplate(__assign({}, fragment, extraConfig))
|
122 | }); });
|
123 | }
|
124 | function parseTemplateNameElements(templateName) {
|
125 | var basename = path.basename(templateName);
|
126 | var pathPrefix = templateName.substr(0, templateName.length - basename.length);
|
127 | var splitted = path.basename(templateName).split('.');
|
128 | var hasPrefix = true;
|
129 | if (splitted.length === 3) {
|
130 | splitted.unshift('');
|
131 | hasPrefix = false;
|
132 | }
|
133 | if (splitted.length > 4 && templateName.includes('/')) {
|
134 | splitted = [splitted.slice(0, splitted.length - 3).join('.'), splitted[2], splitted[3], splitted[4]];
|
135 | }
|
136 | var templateExtension = splitted[3];
|
137 | if (templateExtension && exports.ALLOWED_CUSTOM_TEMPLATE_EXT.includes(templateExtension)) {
|
138 | var prefix = splitted[0];
|
139 | var compilationContext = splitted[2];
|
140 | return {
|
141 | pathPrefix: pathPrefix,
|
142 | fileExtension: splitted[1],
|
143 | compilationContext: compilationContext,
|
144 | prefix: hasPrefix
|
145 | ? ['all', 'documents', 'schema'].includes(compilationContext)
|
146 | ? prefix
|
147 | : prefix + '.'
|
148 | : ['all', 'documents', 'schema'].includes(compilationContext)
|
149 | ? compilationContext
|
150 | : prefix
|
151 | };
|
152 | }
|
153 | return null;
|
154 | }
|
155 | function parseTemplateName(templateName) {
|
156 | var elements = parseTemplateNameElements(templateName);
|
157 | if (elements && handlersMap[elements.compilationContext]) {
|
158 | var compilationContext = elements.compilationContext, pathPrefix = elements.pathPrefix, prefix = elements.prefix, fileExtension = elements.fileExtension;
|
159 | return {
|
160 | handler: handlersMap[compilationContext],
|
161 | prefix: pathPrefix + prefix,
|
162 | fileExtension: fileExtension
|
163 | };
|
164 | }
|
165 | return null;
|
166 | }
|
167 | function generateMultipleFiles(templates, executionSettings, config, templateContext, documents) {
|
168 | graphql_codegen_core_1.debugLog("[generateMultipleFiles] Compiling multiple files...");
|
169 | var result = [];
|
170 | var schemaContext = !executionSettings.generateSchema
|
171 | ? prepare_documents_only_1.prepareSchemaForDocumentsOnly(templateContext)
|
172 | : templateContext;
|
173 | Object.keys(templates).forEach(function (templateName) {
|
174 | graphql_codegen_core_1.debugLog("[generateMultipleFiles] Checking template: " + templateName);
|
175 | var templateFn = templates[templateName];
|
176 | if (handlersMap[templateName]) {
|
177 | graphql_codegen_core_1.debugLog("[generateMultipleFiles] Using simple handle of type: " + templateName);
|
178 | var handler = handlersMap[templateName];
|
179 | result.push.apply(result, handler(templateFn, schemaContext, documents, {
|
180 | config: config.config,
|
181 | primitivesMap: config.primitives,
|
182 | currentTime: moment().format()
|
183 | }, config.filesExtension));
|
184 | }
|
185 | else {
|
186 | var parsedTemplateName = parseTemplateName(templateName);
|
187 | graphql_codegen_core_1.debugLog("[generateMultipleFiles] Using custom template handlers, parsed template name result: ", parsedTemplateName);
|
188 | if (parsedTemplateName !== null) {
|
189 | result.push.apply(result, parsedTemplateName.handler(templateFn, schemaContext, documents, {
|
190 | config: config.config,
|
191 | currentTime: moment().format(),
|
192 | primitivesMap: config.primitives
|
193 | }, parsedTemplateName.fileExtension, parsedTemplateName.prefix));
|
194 | }
|
195 | }
|
196 | });
|
197 | return result;
|
198 | }
|
199 | exports.generateMultipleFiles = generateMultipleFiles;
|
200 |
|
\ | No newline at end of file |