1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const graphql_codegen_plugin_helpers_1 = require("graphql-codegen-plugin-helpers");
|
4 | const codegen_1 = require("./codegen");
|
5 | const watcher_1 = require("./utils/watcher");
|
6 | const file_system_1 = require("./utils/file-system");
|
7 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
8 | async function generate(config, saveToFile = true) {
|
9 | async function writeOutput(generationResult) {
|
10 | if (!saveToFile) {
|
11 | return generationResult;
|
12 | }
|
13 | await Promise.all(generationResult.map(async (result) => {
|
14 | if (!shouldOverwrite(config, result.filename) && file_system_1.fileExists(result.filename)) {
|
15 | return;
|
16 | }
|
17 | const content = result.content || '';
|
18 | if (content.length === 0) {
|
19 | return;
|
20 | }
|
21 | file_system_1.writeSync(result.filename, result.content);
|
22 | }));
|
23 | return generationResult;
|
24 | }
|
25 |
|
26 | if (config.watch) {
|
27 | return watcher_1.createWatcher(config, writeOutput);
|
28 | }
|
29 | const outputFiles = await codegen_1.executeCodegen(config);
|
30 | await writeOutput(outputFiles);
|
31 | return outputFiles;
|
32 | }
|
33 | exports.generate = generate;
|
34 | function shouldOverwrite(config, outputPath) {
|
35 | const globalValue = !!config.overwrite;
|
36 | const outputConfig = config.generates[outputPath];
|
37 | if (!outputConfig) {
|
38 | graphql_codegen_plugin_helpers_1.debugLog(`Couldn't find a config of ${outputPath}`);
|
39 | return globalValue;
|
40 | }
|
41 | if (isConfiguredOutput(outputConfig) && typeof outputConfig.overwrite === 'boolean') {
|
42 | return outputConfig.overwrite;
|
43 | }
|
44 | return globalValue;
|
45 | }
|
46 | function isConfiguredOutput(output) {
|
47 | return typeof output.plugins !== 'undefined';
|
48 | }
|
49 |
|
\ | No newline at end of file |