UNPKG

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