@graphql-codegen/cli 3.1.1-alpha-20230217094942-ea4838cdc → 3.2.0-alpha-20230222014932-bada44650

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/codegen.js CHANGED
@@ -15,6 +15,7 @@ const config_js_1 = require("./config.js");
15
15
  const plugins_js_1 = require("./plugins.js");
16
16
  const presets_js_1 = require("./presets.js");
17
17
  const debugging_js_1 = require("./utils/debugging.js");
18
+ const documentTransforms_js_1 = require("./documentTransforms.js");
18
19
  /**
19
20
  * Poor mans ESM detection.
20
21
  * Looking at this and you have a better method?
@@ -241,6 +242,11 @@ async function executeCodegen(input) {
241
242
  : outputFileTemplateConfig),
242
243
  emitLegacyCommonJSImports: (0, config_js_1.shouldEmitLegacyCommonJSImports)(config),
243
244
  };
245
+ const documentTransforms = Array.isArray(outputConfig.documentTransforms)
246
+ ? await Promise.all(outputConfig.documentTransforms.map(async (config, index) => {
247
+ return await (0, documentTransforms_js_1.getDocumentTransform)(config, makeDefaultLoader(context.cwd), `the element at index ${index} of the documentTransforms`);
248
+ }))
249
+ : [];
244
250
  const outputs = preset
245
251
  ? await context.profiler.run(async () => preset.buildGeneratesSection({
246
252
  baseOutputDir: filename,
@@ -253,6 +259,7 @@ async function executeCodegen(input) {
253
259
  pluginMap,
254
260
  pluginContext,
255
261
  profiler: context.profiler,
262
+ documentTransforms,
256
263
  }), `Build Generates Section: ${filename}`)
257
264
  : [
258
265
  {
@@ -265,6 +272,7 @@ async function executeCodegen(input) {
265
272
  pluginMap,
266
273
  pluginContext,
267
274
  profiler: context.profiler,
275
+ documentTransforms,
268
276
  },
269
277
  ];
270
278
  const process = async (outputArgs) => {
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDocumentTransformByName = exports.getDocumentTransform = void 0;
4
+ const path_1 = require("path");
5
+ async function getDocumentTransform(documentTransform, loader, defaultName) {
6
+ if (typeof documentTransform === 'string') {
7
+ const transformObject = await getDocumentTransformByName(documentTransform, loader);
8
+ return { name: documentTransform, transformObject };
9
+ }
10
+ if (isTransformObject(documentTransform)) {
11
+ return { name: defaultName, transformObject: documentTransform };
12
+ }
13
+ if (isTransformFileConfig(documentTransform)) {
14
+ const name = Object.keys(documentTransform)[0];
15
+ const transformObject = await getDocumentTransformByName(name, loader);
16
+ return { name, transformObject, config: Object.values(documentTransform)[0] };
17
+ }
18
+ throw new Error(`
19
+ An unknown format document transform: '${defaultName}'.
20
+ `);
21
+ }
22
+ exports.getDocumentTransform = getDocumentTransform;
23
+ function isTransformObject(config) {
24
+ return typeof config === 'object' && config.transform && typeof config.transform === 'function';
25
+ }
26
+ function isTransformFileConfig(config) {
27
+ const keys = Object.keys(config);
28
+ return keys.length === 1 && typeof keys[0] === 'string';
29
+ }
30
+ async function getDocumentTransformByName(name, loader) {
31
+ const possibleNames = [
32
+ `@graphql-codegen/${name}`,
33
+ `@graphql-codegen/${name}-document-transform`,
34
+ name,
35
+ (0, path_1.resolve)(process.cwd(), name),
36
+ ];
37
+ const possibleModules = possibleNames.concat((0, path_1.resolve)(process.cwd(), name));
38
+ for (const moduleName of possibleModules) {
39
+ try {
40
+ return await loader(moduleName);
41
+ }
42
+ catch (err) {
43
+ if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') {
44
+ throw new Error(`
45
+ Unable to load document transform matching '${name}'.
46
+ Reason:
47
+ ${err.message}
48
+ `);
49
+ }
50
+ }
51
+ }
52
+ const possibleNamesMsg = possibleNames
53
+ .map(name => `
54
+ - ${name}
55
+ `.trimEnd())
56
+ .join('');
57
+ throw new Error(`
58
+ Unable to find document transform matching '${name}'
59
+ Install one of the following packages:
60
+
61
+ ${possibleNamesMsg}
62
+ `);
63
+ }
64
+ exports.getDocumentTransformByName = getDocumentTransformByName;
@@ -51,10 +51,10 @@ const createWatcher = (initalContext, onNext) => {
51
51
  if (typeof config.watch !== 'boolean') {
52
52
  files.push(...(0, plugin_helpers_1.normalizeInstanceOrArray)(config.watch));
53
53
  }
54
- let watcher;
54
+ let watcherSubscription;
55
55
  const runWatcher = async () => {
56
- var _a, _b;
57
- const chokidar = await Promise.resolve().then(() => tslib_1.__importStar(require('chokidar')));
56
+ const parcelWatcher = await Promise.resolve().then(() => tslib_1.__importStar(require('@parcel/watcher')));
57
+ (0, debugging_js_1.debugLog)(`[Watcher] Parcel watcher loaded...`);
58
58
  let isShutdown = false;
59
59
  const debouncedExec = (0, debounce_1.default)(() => {
60
60
  if (!isShutdown) {
@@ -79,51 +79,39 @@ const createWatcher = (initalContext, onNext) => {
79
79
  ignored.push(entry.filename);
80
80
  }
81
81
  });
82
- watcher = chokidar.watch(files, {
83
- persistent: true,
84
- ignoreInitial: true,
85
- followSymlinks: true,
86
- cwd: process.cwd(),
87
- disableGlobbing: false,
88
- usePolling: (_a = config.watchConfig) === null || _a === void 0 ? void 0 : _a.usePolling,
89
- interval: (_b = config.watchConfig) === null || _b === void 0 ? void 0 : _b.interval,
90
- depth: 99,
91
- awaitWriteFinish: true,
92
- ignorePermissionErrors: false,
93
- atomic: true,
94
- ignored,
95
- });
82
+ watcherSubscription = await parcelWatcher.subscribe(process.cwd(), async (_, events) => {
83
+ // it doesn't matter what has changed, need to run whole process anyway
84
+ await Promise.all(events.map(async ({ type: eventName, path }) => {
85
+ (0, hooks_js_1.lifecycleHooks)(config.hooks).onWatchTriggered(eventName, path);
86
+ (0, debugging_js_1.debugLog)(`[Watcher] triggered due to a file ${eventName} event: ${path}`);
87
+ const fullPath = (0, path_1.join)(process.cwd(), path);
88
+ // In ESM require is not defined
89
+ try {
90
+ delete require.cache[fullPath];
91
+ }
92
+ catch (err) { }
93
+ if (eventName === 'update' && config.configFilePath && fullPath === config.configFilePath) {
94
+ log(`${log_symbols_1.default.info} Config file has changed, reloading...`);
95
+ const context = await (0, config_js_1.loadContext)(config.configFilePath);
96
+ const newParsedConfig = context.getConfig();
97
+ newParsedConfig.watch = config.watch;
98
+ newParsedConfig.silent = config.silent;
99
+ newParsedConfig.overwrite = config.overwrite;
100
+ newParsedConfig.configFilePath = config.configFilePath;
101
+ config = newParsedConfig;
102
+ initalContext.updateConfig(config);
103
+ }
104
+ debouncedExec();
105
+ }));
106
+ }, { ignore: ignored });
96
107
  (0, debugging_js_1.debugLog)(`[Watcher] Started`);
97
108
  const shutdown = () => {
98
109
  isShutdown = true;
99
110
  (0, debugging_js_1.debugLog)(`[Watcher] Shutting down`);
100
111
  log(`Shutting down watch...`);
101
- watcher.close();
112
+ watcherSubscription.unsubscribe();
102
113
  (0, hooks_js_1.lifecycleHooks)(config.hooks).beforeDone();
103
114
  };
104
- // it doesn't matter what has changed, need to run whole process anyway
105
- watcher.on('all', async (eventName, path) => {
106
- (0, hooks_js_1.lifecycleHooks)(config.hooks).onWatchTriggered(eventName, path);
107
- (0, debugging_js_1.debugLog)(`[Watcher] triggered due to a file ${eventName} event: ${path}`);
108
- const fullPath = (0, path_1.join)(process.cwd(), path);
109
- // In ESM require is not defined
110
- try {
111
- delete require.cache[fullPath];
112
- }
113
- catch (err) { }
114
- if (eventName === 'change' && config.configFilePath && fullPath === config.configFilePath) {
115
- log(`${log_symbols_1.default.info} Config file has changed, reloading...`);
116
- const context = await (0, config_js_1.loadContext)(config.configFilePath);
117
- const newParsedConfig = context.getConfig();
118
- newParsedConfig.watch = config.watch;
119
- newParsedConfig.silent = config.silent;
120
- newParsedConfig.overwrite = config.overwrite;
121
- newParsedConfig.configFilePath = config.configFilePath;
122
- config = newParsedConfig;
123
- initalContext.updateConfig(config);
124
- }
125
- debouncedExec();
126
- });
127
115
  process.once('SIGINT', shutdown);
128
116
  process.once('SIGTERM', shutdown);
129
117
  };
@@ -133,7 +121,7 @@ const createWatcher = (initalContext, onNext) => {
133
121
  .then(onNext, () => Promise.resolve())
134
122
  .then(runWatcher)
135
123
  .catch(err => {
136
- watcher.close();
124
+ watcherSubscription.unsubscribe();
137
125
  reject(err);
138
126
  });
139
127
  });
package/esm/codegen.js CHANGED
@@ -11,6 +11,7 @@ import { ensureContext, shouldEmitLegacyCommonJSImports } from './config.js';
11
11
  import { getPluginByName } from './plugins.js';
12
12
  import { getPresetByName } from './presets.js';
13
13
  import { debugLog, printLogs } from './utils/debugging.js';
14
+ import { getDocumentTransform } from './documentTransforms.js';
14
15
  /**
15
16
  * Poor mans ESM detection.
16
17
  * Looking at this and you have a better method?
@@ -237,6 +238,11 @@ export async function executeCodegen(input) {
237
238
  : outputFileTemplateConfig),
238
239
  emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config),
239
240
  };
241
+ const documentTransforms = Array.isArray(outputConfig.documentTransforms)
242
+ ? await Promise.all(outputConfig.documentTransforms.map(async (config, index) => {
243
+ return await getDocumentTransform(config, makeDefaultLoader(context.cwd), `the element at index ${index} of the documentTransforms`);
244
+ }))
245
+ : [];
240
246
  const outputs = preset
241
247
  ? await context.profiler.run(async () => preset.buildGeneratesSection({
242
248
  baseOutputDir: filename,
@@ -249,6 +255,7 @@ export async function executeCodegen(input) {
249
255
  pluginMap,
250
256
  pluginContext,
251
257
  profiler: context.profiler,
258
+ documentTransforms,
252
259
  }), `Build Generates Section: ${filename}`)
253
260
  : [
254
261
  {
@@ -261,6 +268,7 @@ export async function executeCodegen(input) {
261
268
  pluginMap,
262
269
  pluginContext,
263
270
  profiler: context.profiler,
271
+ documentTransforms,
264
272
  },
265
273
  ];
266
274
  const process = async (outputArgs) => {
@@ -0,0 +1,59 @@
1
+ import { resolve } from 'path';
2
+ export async function getDocumentTransform(documentTransform, loader, defaultName) {
3
+ if (typeof documentTransform === 'string') {
4
+ const transformObject = await getDocumentTransformByName(documentTransform, loader);
5
+ return { name: documentTransform, transformObject };
6
+ }
7
+ if (isTransformObject(documentTransform)) {
8
+ return { name: defaultName, transformObject: documentTransform };
9
+ }
10
+ if (isTransformFileConfig(documentTransform)) {
11
+ const name = Object.keys(documentTransform)[0];
12
+ const transformObject = await getDocumentTransformByName(name, loader);
13
+ return { name, transformObject, config: Object.values(documentTransform)[0] };
14
+ }
15
+ throw new Error(`
16
+ An unknown format document transform: '${defaultName}'.
17
+ `);
18
+ }
19
+ function isTransformObject(config) {
20
+ return typeof config === 'object' && config.transform && typeof config.transform === 'function';
21
+ }
22
+ function isTransformFileConfig(config) {
23
+ const keys = Object.keys(config);
24
+ return keys.length === 1 && typeof keys[0] === 'string';
25
+ }
26
+ export async function getDocumentTransformByName(name, loader) {
27
+ const possibleNames = [
28
+ `@graphql-codegen/${name}`,
29
+ `@graphql-codegen/${name}-document-transform`,
30
+ name,
31
+ resolve(process.cwd(), name),
32
+ ];
33
+ const possibleModules = possibleNames.concat(resolve(process.cwd(), name));
34
+ for (const moduleName of possibleModules) {
35
+ try {
36
+ return await loader(moduleName);
37
+ }
38
+ catch (err) {
39
+ if (err.code !== 'MODULE_NOT_FOUND' && err.code !== 'ERR_MODULE_NOT_FOUND') {
40
+ throw new Error(`
41
+ Unable to load document transform matching '${name}'.
42
+ Reason:
43
+ ${err.message}
44
+ `);
45
+ }
46
+ }
47
+ }
48
+ const possibleNamesMsg = possibleNames
49
+ .map(name => `
50
+ - ${name}
51
+ `.trimEnd())
52
+ .join('');
53
+ throw new Error(`
54
+ Unable to find document transform matching '${name}'
55
+ Install one of the following packages:
56
+
57
+ ${possibleNamesMsg}
58
+ `);
59
+ }
@@ -47,10 +47,10 @@ export const createWatcher = (initalContext, onNext) => {
47
47
  if (typeof config.watch !== 'boolean') {
48
48
  files.push(...normalizeInstanceOrArray(config.watch));
49
49
  }
50
- let watcher;
50
+ let watcherSubscription;
51
51
  const runWatcher = async () => {
52
- var _a, _b;
53
- const chokidar = await import('chokidar');
52
+ const parcelWatcher = await import('@parcel/watcher');
53
+ debugLog(`[Watcher] Parcel watcher loaded...`);
54
54
  let isShutdown = false;
55
55
  const debouncedExec = debounce(() => {
56
56
  if (!isShutdown) {
@@ -75,51 +75,39 @@ export const createWatcher = (initalContext, onNext) => {
75
75
  ignored.push(entry.filename);
76
76
  }
77
77
  });
78
- watcher = chokidar.watch(files, {
79
- persistent: true,
80
- ignoreInitial: true,
81
- followSymlinks: true,
82
- cwd: process.cwd(),
83
- disableGlobbing: false,
84
- usePolling: (_a = config.watchConfig) === null || _a === void 0 ? void 0 : _a.usePolling,
85
- interval: (_b = config.watchConfig) === null || _b === void 0 ? void 0 : _b.interval,
86
- depth: 99,
87
- awaitWriteFinish: true,
88
- ignorePermissionErrors: false,
89
- atomic: true,
90
- ignored,
91
- });
78
+ watcherSubscription = await parcelWatcher.subscribe(process.cwd(), async (_, events) => {
79
+ // it doesn't matter what has changed, need to run whole process anyway
80
+ await Promise.all(events.map(async ({ type: eventName, path }) => {
81
+ lifecycleHooks(config.hooks).onWatchTriggered(eventName, path);
82
+ debugLog(`[Watcher] triggered due to a file ${eventName} event: ${path}`);
83
+ const fullPath = join(process.cwd(), path);
84
+ // In ESM require is not defined
85
+ try {
86
+ delete require.cache[fullPath];
87
+ }
88
+ catch (err) { }
89
+ if (eventName === 'update' && config.configFilePath && fullPath === config.configFilePath) {
90
+ log(`${logSymbols.info} Config file has changed, reloading...`);
91
+ const context = await loadContext(config.configFilePath);
92
+ const newParsedConfig = context.getConfig();
93
+ newParsedConfig.watch = config.watch;
94
+ newParsedConfig.silent = config.silent;
95
+ newParsedConfig.overwrite = config.overwrite;
96
+ newParsedConfig.configFilePath = config.configFilePath;
97
+ config = newParsedConfig;
98
+ initalContext.updateConfig(config);
99
+ }
100
+ debouncedExec();
101
+ }));
102
+ }, { ignore: ignored });
92
103
  debugLog(`[Watcher] Started`);
93
104
  const shutdown = () => {
94
105
  isShutdown = true;
95
106
  debugLog(`[Watcher] Shutting down`);
96
107
  log(`Shutting down watch...`);
97
- watcher.close();
108
+ watcherSubscription.unsubscribe();
98
109
  lifecycleHooks(config.hooks).beforeDone();
99
110
  };
100
- // it doesn't matter what has changed, need to run whole process anyway
101
- watcher.on('all', async (eventName, path) => {
102
- lifecycleHooks(config.hooks).onWatchTriggered(eventName, path);
103
- debugLog(`[Watcher] triggered due to a file ${eventName} event: ${path}`);
104
- const fullPath = join(process.cwd(), path);
105
- // In ESM require is not defined
106
- try {
107
- delete require.cache[fullPath];
108
- }
109
- catch (err) { }
110
- if (eventName === 'change' && config.configFilePath && fullPath === config.configFilePath) {
111
- log(`${logSymbols.info} Config file has changed, reloading...`);
112
- const context = await loadContext(config.configFilePath);
113
- const newParsedConfig = context.getConfig();
114
- newParsedConfig.watch = config.watch;
115
- newParsedConfig.silent = config.silent;
116
- newParsedConfig.overwrite = config.overwrite;
117
- newParsedConfig.configFilePath = config.configFilePath;
118
- config = newParsedConfig;
119
- initalContext.updateConfig(config);
120
- }
121
- debouncedExec();
122
- });
123
111
  process.once('SIGINT', shutdown);
124
112
  process.once('SIGTERM', shutdown);
125
113
  };
@@ -129,7 +117,7 @@ export const createWatcher = (initalContext, onNext) => {
129
117
  .then(onNext, () => Promise.resolve())
130
118
  .then(runWatcher)
131
119
  .catch(err => {
132
- watcher.close();
120
+ watcherSubscription.unsubscribe();
133
121
  reject(err);
134
122
  });
135
123
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "3.1.1-alpha-20230217094942-ea4838cdc",
3
+ "version": "3.2.0-alpha-20230222014932-bada44650",
4
4
  "peerDependencies": {
5
5
  "graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
6
6
  },
@@ -8,7 +8,7 @@
8
8
  "@babel/generator": "^7.18.13",
9
9
  "@babel/template": "^7.18.10",
10
10
  "@babel/types": "^7.18.13",
11
- "@graphql-codegen/core": "3.0.2-alpha-20230217094942-ea4838cdc",
11
+ "@graphql-codegen/core": "^3.1.0",
12
12
  "@graphql-codegen/plugin-helpers": "^4.1.0",
13
13
  "@graphql-tools/apollo-engine-loader": "^7.3.6",
14
14
  "@graphql-tools/code-file-loader": "^7.3.17",
@@ -20,9 +20,9 @@
20
20
  "@graphql-tools/prisma-loader": "^7.2.49",
21
21
  "@graphql-tools/url-loader": "^7.13.2",
22
22
  "@graphql-tools/utils": "^9.0.0",
23
- "@whatwg-node/fetch": "^0.6.0",
23
+ "@parcel/watcher": "^2.1.0",
24
+ "@whatwg-node/fetch": "^0.8.0",
24
25
  "chalk": "^4.1.0",
25
- "chokidar": "^3.5.2",
26
26
  "cosmiconfig": "^7.0.0",
27
27
  "cosmiconfig-typescript-loader": "^4.3.0",
28
28
  "debounce": "^1.2.0",
@@ -0,0 +1,3 @@
1
+ import { Types } from '@graphql-codegen/plugin-helpers';
2
+ export declare function getDocumentTransform(documentTransform: Types.OutputDocumentTransform, loader: Types.PackageLoaderFn<Types.DocumentTransformObject>, defaultName: string): Promise<Types.ConfiguredDocumentTransform>;
3
+ export declare function getDocumentTransformByName(name: string, loader: Types.PackageLoaderFn<Types.DocumentTransformObject>): Promise<Types.DocumentTransformObject>;
@@ -0,0 +1,3 @@
1
+ import { Types } from '@graphql-codegen/plugin-helpers';
2
+ export declare function getDocumentTransform(documentTransform: Types.OutputDocumentTransform, loader: Types.PackageLoaderFn<Types.DocumentTransformObject>, defaultName: string): Promise<Types.ConfiguredDocumentTransform>;
3
+ export declare function getDocumentTransformByName(name: string, loader: Types.PackageLoaderFn<Types.DocumentTransformObject>): Promise<Types.DocumentTransformObject>;