UNPKG

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