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 | const mkdirp_1 = require("mkdirp");
|
8 | const path_1 = require("path");
|
9 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
10 | async function generate(config, saveToFile = true) {
|
11 | async function writeOutput(generationResult) {
|
12 | if (!saveToFile) {
|
13 | return generationResult;
|
14 | }
|
15 | await Promise.all(generationResult.map(async (result) => {
|
16 | if (!shouldOverwrite(config, result.filename) && file_system_1.fileExists(result.filename)) {
|
17 | return;
|
18 | }
|
19 | const content = result.content || '';
|
20 | if (content.length === 0) {
|
21 | return;
|
22 | }
|
23 | const basedir = path_1.dirname(result.filename);
|
24 | mkdirp_1.sync(basedir);
|
25 | file_system_1.writeSync(result.filename, result.content);
|
26 | }));
|
27 | return generationResult;
|
28 | }
|
29 |
|
30 | if (config.watch) {
|
31 | return watcher_1.createWatcher(config, writeOutput);
|
32 | }
|
33 | const outputFiles = await codegen_1.executeCodegen(config);
|
34 | await writeOutput(outputFiles);
|
35 | return outputFiles;
|
36 | }
|
37 | exports.generate = generate;
|
38 | function shouldOverwrite(config, outputPath) {
|
39 | const globalValue = !!config.overwrite;
|
40 | const outputConfig = config.generates[outputPath];
|
41 | if (!outputConfig) {
|
42 | graphql_codegen_plugin_helpers_1.debugLog(`Couldn't find a config of ${outputPath}`);
|
43 | return globalValue;
|
44 | }
|
45 | if (isConfiguredOutput(outputConfig) && typeof outputConfig.overwrite === 'boolean') {
|
46 | return outputConfig.overwrite;
|
47 | }
|
48 | return globalValue;
|
49 | }
|
50 | function isConfiguredOutput(output) {
|
51 | return typeof output.plugins !== 'undefined';
|
52 | }
|
53 |
|
\ | No newline at end of file |