1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const localfs_1 = require("apollo-codegen-core/lib/localfs");
|
4 | const path = require("path");
|
5 | const loading_1 = require("apollo-codegen-core/lib/loading");
|
6 | const validation_1 = require("./validation");
|
7 | const compiler_1 = require("apollo-codegen-core/lib/compiler");
|
8 | const legacyIR_1 = require("apollo-codegen-core/lib/compiler/legacyIR");
|
9 | const serializeToJSON_1 = require("apollo-codegen-core/lib/serializeToJSON");
|
10 | const apollo_codegen_swift_1 = require("apollo-codegen-swift");
|
11 | const apollo_codegen_typescript_legacy_1 = require("apollo-codegen-typescript-legacy");
|
12 | const apollo_codegen_flow_legacy_1 = require("apollo-codegen-flow-legacy");
|
13 | const apollo_codegen_flow_1 = require("apollo-codegen-flow");
|
14 | const apollo_codegen_typescript_1 = require("apollo-codegen-typescript");
|
15 | const apollo_codegen_scala_1 = require("apollo-codegen-scala");
|
16 | function generate(inputPaths, schema, outputPath, only, target, tagName, nextToSources, options) {
|
17 | let writtenFiles = 0;
|
18 | const document = loading_1.loadAndMergeQueryDocuments(inputPaths, tagName);
|
19 | validation_1.validateQueryDocument(schema, document);
|
20 | if (outputPath.split(".").length <= 1 && !localfs_1.fs.existsSync(outputPath)) {
|
21 | localfs_1.fs.mkdirSync(outputPath);
|
22 | }
|
23 | if (target === "swift") {
|
24 | options.addTypename = true;
|
25 | const context = compiler_1.compileToIR(schema, document, options);
|
26 | const outputIndividualFiles = localfs_1.fs.existsSync(outputPath) && localfs_1.fs.statSync(outputPath).isDirectory();
|
27 | const generator = apollo_codegen_swift_1.generateSource(context, outputIndividualFiles, only);
|
28 | if (outputIndividualFiles) {
|
29 | writeGeneratedFiles(generator.generatedFiles, outputPath);
|
30 | writtenFiles += Object.keys(generator.generatedFiles).length;
|
31 | }
|
32 | else {
|
33 | localfs_1.fs.writeFileSync(outputPath, generator.output);
|
34 | writtenFiles += 1;
|
35 | }
|
36 | if (options.generateOperationIds) {
|
37 | writeOperationIdsMap(context);
|
38 | writtenFiles += 1;
|
39 | }
|
40 | }
|
41 | else if (target === "flow") {
|
42 | const context = compiler_1.compileToIR(schema, document, options);
|
43 | const { generatedFiles, common } = apollo_codegen_flow_1.generateSource(context);
|
44 | const outFiles = {};
|
45 | if (nextToSources) {
|
46 | generatedFiles.forEach(({ sourcePath, fileName, content }) => {
|
47 | const dir = path.join(path.dirname(sourcePath), outputPath);
|
48 | if (!localfs_1.fs.existsSync(dir)) {
|
49 | localfs_1.fs.mkdirSync(dir);
|
50 | }
|
51 | outFiles[path.join(dir, fileName)] = {
|
52 | output: content.fileContents + common
|
53 | };
|
54 | });
|
55 | writeGeneratedFiles(outFiles, path.resolve("."));
|
56 | writtenFiles += Object.keys(outFiles).length;
|
57 | }
|
58 | else if (localfs_1.fs.existsSync(outputPath) &&
|
59 | localfs_1.fs.statSync(outputPath).isDirectory()) {
|
60 | generatedFiles.forEach(({ fileName, content }) => {
|
61 | outFiles[fileName] = {
|
62 | output: content.fileContents + common
|
63 | };
|
64 | });
|
65 | writeGeneratedFiles(outFiles, outputPath);
|
66 | writtenFiles += Object.keys(outFiles).length;
|
67 | }
|
68 | else {
|
69 | localfs_1.fs.writeFileSync(outputPath, generatedFiles.map(o => o.content.fileContents).join("\n") + common);
|
70 | writtenFiles += 1;
|
71 | }
|
72 | }
|
73 | else if (target === "typescript" || target === "ts") {
|
74 | const context = compiler_1.compileToIR(schema, document, options);
|
75 | const generatedFiles = apollo_codegen_typescript_1.generateLocalSource(context);
|
76 | const generatedGlobalFile = apollo_codegen_typescript_1.generateGlobalSource(context);
|
77 | const outFiles = {};
|
78 | if (nextToSources ||
|
79 | (localfs_1.fs.existsSync(outputPath) && localfs_1.fs.statSync(outputPath).isDirectory())) {
|
80 | if (options.globalTypesFile) {
|
81 | const globalTypesDir = path.dirname(options.globalTypesFile);
|
82 | if (!localfs_1.fs.existsSync(globalTypesDir)) {
|
83 | localfs_1.fs.mkdirSync(globalTypesDir);
|
84 | }
|
85 | }
|
86 | else if (nextToSources && !localfs_1.fs.existsSync(outputPath)) {
|
87 | localfs_1.fs.mkdirSync(outputPath);
|
88 | }
|
89 | const globalSourcePath = options.globalTypesFile || path.join(outputPath, "globalTypes.ts");
|
90 | outFiles[globalSourcePath] = {
|
91 | output: generatedGlobalFile.fileContents
|
92 | };
|
93 | generatedFiles.forEach(({ sourcePath, fileName, content }) => {
|
94 | let dir = outputPath;
|
95 | if (nextToSources) {
|
96 | dir = path.join(path.dirname(sourcePath), dir);
|
97 | if (!localfs_1.fs.existsSync(dir)) {
|
98 | localfs_1.fs.mkdirSync(dir);
|
99 | }
|
100 | }
|
101 | const outFilePath = path.join(dir, fileName);
|
102 | outFiles[outFilePath] = {
|
103 | output: content({ outputPath: outFilePath, globalSourcePath })
|
104 | .fileContents
|
105 | };
|
106 | });
|
107 | writeGeneratedFiles(outFiles, path.resolve("."));
|
108 | writtenFiles += Object.keys(outFiles).length;
|
109 | }
|
110 | else {
|
111 | localfs_1.fs.writeFileSync(outputPath, generatedFiles.map(o => o.content().fileContents).join("\n") +
|
112 | "\n" +
|
113 | generatedGlobalFile.fileContents);
|
114 | writtenFiles += 1;
|
115 | }
|
116 | }
|
117 | else {
|
118 | let output;
|
119 | const context = legacyIR_1.compileToLegacyIR(schema, document, options);
|
120 | switch (target) {
|
121 | case "json":
|
122 | output = serializeToJSON_1.default(context);
|
123 | break;
|
124 | case "ts-legacy":
|
125 | case "typescript-legacy":
|
126 | output = apollo_codegen_typescript_legacy_1.generateSource(context);
|
127 | break;
|
128 | case "flow-legacy":
|
129 | output = apollo_codegen_flow_legacy_1.generateSource(context);
|
130 | break;
|
131 | case "scala":
|
132 | output = apollo_codegen_scala_1.generateSource(context);
|
133 | }
|
134 | if (outputPath) {
|
135 | localfs_1.fs.writeFileSync(outputPath, output);
|
136 | writtenFiles += 1;
|
137 | }
|
138 | else {
|
139 | console.log(output);
|
140 | }
|
141 | }
|
142 | return writtenFiles;
|
143 | }
|
144 | exports.default = generate;
|
145 | function writeGeneratedFiles(generatedFiles, outputDirectory) {
|
146 | for (const [fileName, generatedFile] of Object.entries(generatedFiles)) {
|
147 | localfs_1.fs.writeFileSync(path.join(outputDirectory, fileName), generatedFile.output);
|
148 | }
|
149 | }
|
150 | function writeOperationIdsMap(context) {
|
151 | let operationIdsMap = {};
|
152 | Object.keys(context.operations)
|
153 | .map(k => context.operations[k])
|
154 | .forEach(operation => {
|
155 | operationIdsMap[operation.operationId] = {
|
156 | name: operation.operationName,
|
157 | source: operation.source
|
158 | };
|
159 | });
|
160 | localfs_1.fs.writeFileSync(context.options.operationIdsPath, JSON.stringify(operationIdsMap, null, 2));
|
161 | }
|
162 |
|
\ | No newline at end of file |