UNPKG

7.94 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 (0, 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 = (0, 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 = (0, 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 = (0, compiler_1.compileToIR)(schema, document, options);
49 const { generatedFiles, common } = (0, 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 = (0, compiler_1.compileToIR)(schema, document, options);
81 const generatedFiles = (0, apollo_codegen_typescript_1.generateLocalSource)(context);
82 const generatedGlobalFile = (0, 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 || helpers_1.DEFAULT_FILE_EXTENSION}`);
97 outFiles[globalSourcePath] = {
98 output: generatedGlobalFile.fileContents,
99 };
100 generatedFiles.forEach(({ sourcePath, fileName, content }) => {
101 let dir = outputPath;
102 if (nextToSources) {
103 dir = path_1.default.join(path_1.default.dirname(path_1.default.relative(rootPath, toPath(sourcePath))), dir);
104 if (!localfs_1.fs.existsSync(dir)) {
105 localfs_1.fs.mkdirSync(dir);
106 }
107 }
108 const outFilePath = path_1.default.join(dir, fileName);
109 outFiles[outFilePath] = {
110 output: content({ outputPath: outFilePath, globalSourcePath })
111 .fileContents,
112 };
113 });
114 writeGeneratedFiles(outFiles, path_1.default.resolve("."));
115 writtenFiles += Object.keys(outFiles).length;
116 }
117 else {
118 localfs_1.fs.writeFileSync(outputPath, generatedFiles.map((o) => o.content().fileContents).join("\n") +
119 "\n" +
120 generatedGlobalFile.fileContents);
121 writtenFiles += 1;
122 }
123 }
124 else {
125 let output;
126 const context = (0, legacyIR_1.compileToLegacyIR)(schema, document, Object.assign(Object.assign({}, options), { exposeTypeNodes: target === "json-modern" }));
127 switch (target) {
128 case "json-modern":
129 case "json":
130 output = (0, serializeToJSON_1.default)(context, {
131 exposeTypeNodes: Boolean(options.exposeTypeNodes),
132 });
133 break;
134 case "scala":
135 output = (0, apollo_codegen_scala_1.generateSource)(context);
136 }
137 if (outputPath) {
138 localfs_1.fs.writeFileSync(outputPath, output);
139 writtenFiles += 1;
140 }
141 else {
142 console.log(output);
143 }
144 }
145 return writtenFiles;
146}
147exports.default = generate;
148function writeGeneratedFiles(generatedFiles, outputDirectory, terminator = "") {
149 for (const [fileName, generatedFile] of Object.entries(generatedFiles)) {
150 localfs_1.fs.writeFileSync(path_1.default.join(outputDirectory, fileName), generatedFile.output.concat(terminator));
151 }
152}
153function writeOperationIdsMap(context) {
154 let operationIdsMap = {};
155 Object.keys(context.operations)
156 .map((k) => context.operations[k])
157 .forEach((operation) => {
158 operationIdsMap[operation.operationId] = {
159 name: operation.operationName,
160 source: operation.sourceWithFragments,
161 };
162 });
163 localfs_1.fs.writeFileSync(context.options.operationIdsPath, JSON.stringify(operationIdsMap, null, 2));
164}
165//# sourceMappingURL=generate.js.map
\No newline at end of file