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