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