UNPKG

6.99 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const localfs_1 = require("apollo-codegen-core/lib/localfs");
4const path = require("path");
5const vscode_uri_1 = require("vscode-uri");
6const compiler_1 = require("apollo-codegen-core/lib/compiler");
7const legacyIR_1 = require("apollo-codegen-core/lib/compiler/legacyIR");
8const serializeToJSON_1 = require("apollo-codegen-core/lib/serializeToJSON");
9const apollo_codegen_swift_1 = require("apollo-codegen-swift");
10const apollo_codegen_flow_1 = require("apollo-codegen-flow");
11const apollo_codegen_typescript_1 = require("apollo-codegen-typescript");
12const apollo_codegen_scala_1 = require("apollo-codegen-scala");
13const validation_1 = require("apollo-language-server/lib/errors/validation");
14function toPath(uri) {
15 return vscode_uri_1.default.parse(uri).fsPath;
16}
17function 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.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}
138exports.default = generate;
139function 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}
144function 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//# sourceMappingURL=generate.js.map
\No newline at end of file