@graphql-codegen/cli 2.15.1-alpha-20221207043812-945e92b01 → 2.16.1

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.
@@ -55,7 +55,7 @@ async function generate(input, saveToFile = true) {
55
55
  if (!shouldOverwrite(config, result.filename) && exists) {
56
56
  return;
57
57
  }
58
- const content = result.content || '';
58
+ let content = result.content || '';
59
59
  const currentHash = hash(content);
60
60
  if (previousHash && currentHash === previousHash) {
61
61
  (0, debugging_js_1.debugLog)(`Skipping file (${result.filename}) writing due to indentical hash...`);
@@ -69,14 +69,25 @@ async function generate(input, saveToFile = true) {
69
69
  if (content.length === 0) {
70
70
  return;
71
71
  }
72
- await (0, hooks_js_1.lifecycleHooks)(result.hooks).beforeOneFileWrite(result.filename);
73
- await (0, hooks_js_1.lifecycleHooks)(config.hooks).beforeOneFileWrite(result.filename);
74
72
  const absolutePath = (0, path_1.isAbsolute)(result.filename)
75
73
  ? result.filename
76
74
  : (0, path_1.join)(input.cwd || process.cwd(), result.filename);
77
75
  const basedir = (0, path_1.dirname)(absolutePath);
78
76
  await (0, file_system_js_1.mkdirp)(basedir);
79
- await (0, file_system_js_1.writeFile)(absolutePath, content);
77
+ content = await (0, hooks_js_1.lifecycleHooks)(result.hooks).beforeOneFileWrite(absolutePath, content);
78
+ content = await (0, hooks_js_1.lifecycleHooks)(config.hooks).beforeOneFileWrite(absolutePath, content);
79
+ if (content !== result.content) {
80
+ result.content = content;
81
+ // compare the prettified content with the previous hash
82
+ // to compare the content with an existing prettified file
83
+ if (hash(content) === previousHash) {
84
+ (0, debugging_js_1.debugLog)(`Skipping file (${result.filename}) writing due to indentical hash after prettier...`);
85
+ // the modified content is NOT stored in recentOutputHash
86
+ // so a diff can already be detected before executing the hook
87
+ return;
88
+ }
89
+ }
90
+ await (0, file_system_js_1.writeFile)(absolutePath, result.content);
80
91
  recentOutputHash.set(result.filename, currentHash);
81
92
  await (0, hooks_js_1.lifecycleHooks)(result.hooks).afterOneFileWrite(result.filename);
82
93
  await (0, hooks_js_1.lifecycleHooks)(config.hooks).afterOneFileWrite(result.filename);
package/cjs/hooks.js CHANGED
@@ -35,8 +35,9 @@ function execShellCommand(cmd) {
35
35
  });
36
36
  });
37
37
  }
38
- async function executeHooks(hookName, _scripts = [], args = []) {
38
+ async function executeHooks(hookName, _scripts = [], args = [], initialState) {
39
39
  (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" scripts...`);
40
+ let state = initialState;
40
41
  const scripts = Array.isArray(_scripts) ? _scripts : [_scripts];
41
42
  const quotedArgs = (0, shell_quote_1.quote)(args);
42
43
  for (const script of scripts) {
@@ -46,9 +47,15 @@ async function executeHooks(hookName, _scripts = [], args = []) {
46
47
  }
47
48
  else {
48
49
  (0, debugging_js_1.debugLog)(`Running lifecycle hook "${hookName}" script: ${script.name} with args: ${args.join(' ')}...`);
49
- await script(...args);
50
+ const hookArgs = state === undefined ? args : [...args, state];
51
+ const hookResult = await script(...hookArgs);
52
+ if (typeof hookResult === 'string' && typeof state === 'string') {
53
+ (0, debugging_js_1.debugLog)(`Received new content from lifecycle hook "${hookName}" script: ${script.name}`);
54
+ state = hookResult;
55
+ }
50
56
  }
51
57
  }
58
+ return state;
52
59
  }
53
60
  const lifecycleHooks = (_hooks = {}) => {
54
61
  const hooks = {
@@ -56,14 +63,31 @@ const lifecycleHooks = (_hooks = {}) => {
56
63
  ..._hooks,
57
64
  };
58
65
  return {
59
- afterStart: async () => executeHooks('afterStart', hooks.afterStart),
60
- onWatchTriggered: async (event, path) => executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]),
61
- onError: async (error) => executeHooks('onError', hooks.onError, [error]),
62
- afterOneFileWrite: async (path) => executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]),
63
- afterAllFileWrite: async (paths) => executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths),
64
- beforeOneFileWrite: async (path) => executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path]),
65
- beforeAllFileWrite: async (paths) => executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths),
66
- beforeDone: async () => executeHooks('beforeDone', hooks.beforeDone),
66
+ afterStart: async () => {
67
+ await executeHooks('afterStart', hooks.afterStart);
68
+ },
69
+ onWatchTriggered: async (event, path) => {
70
+ await executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]);
71
+ },
72
+ onError: async (error) => {
73
+ await executeHooks('onError', hooks.onError, [error]);
74
+ },
75
+ afterOneFileWrite: async (path) => {
76
+ await executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]);
77
+ },
78
+ afterAllFileWrite: async (paths) => {
79
+ await executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths);
80
+ },
81
+ beforeOneFileWrite: async (path, content) => {
82
+ const result = await executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path], content);
83
+ return typeof result === 'string' ? result : content;
84
+ },
85
+ beforeAllFileWrite: async (paths) => {
86
+ await executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths);
87
+ },
88
+ beforeDone: async () => {
89
+ await executeHooks('beforeDone', hooks.beforeDone);
90
+ },
67
91
  };
68
92
  };
69
93
  exports.lifecycleHooks = lifecycleHooks;
@@ -52,7 +52,7 @@ export async function generate(input, saveToFile = true) {
52
52
  if (!shouldOverwrite(config, result.filename) && exists) {
53
53
  return;
54
54
  }
55
- const content = result.content || '';
55
+ let content = result.content || '';
56
56
  const currentHash = hash(content);
57
57
  if (previousHash && currentHash === previousHash) {
58
58
  debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
@@ -66,14 +66,25 @@ export async function generate(input, saveToFile = true) {
66
66
  if (content.length === 0) {
67
67
  return;
68
68
  }
69
- await lifecycleHooks(result.hooks).beforeOneFileWrite(result.filename);
70
- await lifecycleHooks(config.hooks).beforeOneFileWrite(result.filename);
71
69
  const absolutePath = isAbsolute(result.filename)
72
70
  ? result.filename
73
71
  : join(input.cwd || process.cwd(), result.filename);
74
72
  const basedir = dirname(absolutePath);
75
73
  await mkdirp(basedir);
76
- await writeFile(absolutePath, content);
74
+ content = await lifecycleHooks(result.hooks).beforeOneFileWrite(absolutePath, content);
75
+ content = await lifecycleHooks(config.hooks).beforeOneFileWrite(absolutePath, content);
76
+ if (content !== result.content) {
77
+ result.content = content;
78
+ // compare the prettified content with the previous hash
79
+ // to compare the content with an existing prettified file
80
+ if (hash(content) === previousHash) {
81
+ debugLog(`Skipping file (${result.filename}) writing due to indentical hash after prettier...`);
82
+ // the modified content is NOT stored in recentOutputHash
83
+ // so a diff can already be detected before executing the hook
84
+ return;
85
+ }
86
+ }
87
+ await writeFile(absolutePath, result.content);
77
88
  recentOutputHash.set(result.filename, currentHash);
78
89
  await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
79
90
  await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
package/esm/hooks.js CHANGED
@@ -32,8 +32,9 @@ function execShellCommand(cmd) {
32
32
  });
33
33
  });
34
34
  }
35
- async function executeHooks(hookName, _scripts = [], args = []) {
35
+ async function executeHooks(hookName, _scripts = [], args = [], initialState) {
36
36
  debugLog(`Running lifecycle hook "${hookName}" scripts...`);
37
+ let state = initialState;
37
38
  const scripts = Array.isArray(_scripts) ? _scripts : [_scripts];
38
39
  const quotedArgs = quote(args);
39
40
  for (const script of scripts) {
@@ -43,9 +44,15 @@ async function executeHooks(hookName, _scripts = [], args = []) {
43
44
  }
44
45
  else {
45
46
  debugLog(`Running lifecycle hook "${hookName}" script: ${script.name} with args: ${args.join(' ')}...`);
46
- await script(...args);
47
+ const hookArgs = state === undefined ? args : [...args, state];
48
+ const hookResult = await script(...hookArgs);
49
+ if (typeof hookResult === 'string' && typeof state === 'string') {
50
+ debugLog(`Received new content from lifecycle hook "${hookName}" script: ${script.name}`);
51
+ state = hookResult;
52
+ }
47
53
  }
48
54
  }
55
+ return state;
49
56
  }
50
57
  export const lifecycleHooks = (_hooks = {}) => {
51
58
  const hooks = {
@@ -53,13 +60,30 @@ export const lifecycleHooks = (_hooks = {}) => {
53
60
  ..._hooks,
54
61
  };
55
62
  return {
56
- afterStart: async () => executeHooks('afterStart', hooks.afterStart),
57
- onWatchTriggered: async (event, path) => executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]),
58
- onError: async (error) => executeHooks('onError', hooks.onError, [error]),
59
- afterOneFileWrite: async (path) => executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]),
60
- afterAllFileWrite: async (paths) => executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths),
61
- beforeOneFileWrite: async (path) => executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path]),
62
- beforeAllFileWrite: async (paths) => executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths),
63
- beforeDone: async () => executeHooks('beforeDone', hooks.beforeDone),
63
+ afterStart: async () => {
64
+ await executeHooks('afterStart', hooks.afterStart);
65
+ },
66
+ onWatchTriggered: async (event, path) => {
67
+ await executeHooks('onWatchTriggered', hooks.onWatchTriggered, [event, path]);
68
+ },
69
+ onError: async (error) => {
70
+ await executeHooks('onError', hooks.onError, [error]);
71
+ },
72
+ afterOneFileWrite: async (path) => {
73
+ await executeHooks('afterOneFileWrite', hooks.afterOneFileWrite, [path]);
74
+ },
75
+ afterAllFileWrite: async (paths) => {
76
+ await executeHooks('afterAllFileWrite', hooks.afterAllFileWrite, paths);
77
+ },
78
+ beforeOneFileWrite: async (path, content) => {
79
+ const result = await executeHooks('beforeOneFileWrite', hooks.beforeOneFileWrite, [path], content);
80
+ return typeof result === 'string' ? result : content;
81
+ },
82
+ beforeAllFileWrite: async (paths) => {
83
+ await executeHooks('beforeAllFileWrite', hooks.beforeAllFileWrite, paths);
84
+ },
85
+ beforeDone: async () => {
86
+ await executeHooks('beforeDone', hooks.beforeDone);
87
+ },
64
88
  };
65
89
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.15.1-alpha-20221207043812-945e92b01",
3
+ "version": "2.16.1",
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,8 +8,8 @@
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": "2.6.7-alpha-20221207043812-945e92b01",
12
- "@graphql-codegen/plugin-helpers": "3.0.0-alpha-20221207043812-945e92b01",
11
+ "@graphql-codegen/core": "2.6.8",
12
+ "@graphql-codegen/plugin-helpers": "^3.1.1",
13
13
  "@graphql-tools/apollo-engine-loader": "^7.3.6",
14
14
  "@graphql-tools/code-file-loader": "^7.3.13",
15
15
  "@graphql-tools/git-loader": "^7.2.13",
@@ -20,7 +20,7 @@
20
20
  "@graphql-tools/prisma-loader": "^7.2.7",
21
21
  "@graphql-tools/url-loader": "^7.13.2",
22
22
  "@graphql-tools/utils": "^8.9.0",
23
- "@whatwg-node/fetch": "^0.3.0",
23
+ "@whatwg-node/fetch": "^0.5.0",
24
24
  "chalk": "^4.1.0",
25
25
  "chokidar": "^3.5.2",
26
26
  "cosmiconfig": "^7.0.0",
@@ -5,7 +5,7 @@ export declare const lifecycleHooks: (_hooks?: Partial<Types.LifecycleHooksDefin
5
5
  onError: (error: string) => Promise<void>;
6
6
  afterOneFileWrite: (path: string) => Promise<void>;
7
7
  afterAllFileWrite: (paths: string[]) => Promise<void>;
8
- beforeOneFileWrite: (path: string) => Promise<void>;
8
+ beforeOneFileWrite: (path: string, content: string) => Promise<string>;
9
9
  beforeAllFileWrite: (paths: string[]) => Promise<void>;
10
10
  beforeDone: () => Promise<void>;
11
11
  };
@@ -5,7 +5,7 @@ export declare const lifecycleHooks: (_hooks?: Partial<Types.LifecycleHooksDefin
5
5
  onError: (error: string) => Promise<void>;
6
6
  afterOneFileWrite: (path: string) => Promise<void>;
7
7
  afterAllFileWrite: (paths: string[]) => Promise<void>;
8
- beforeOneFileWrite: (path: string) => Promise<void>;
8
+ beforeOneFileWrite: (path: string, content: string) => Promise<string>;
9
9
  beforeAllFileWrite: (paths: string[]) => Promise<void>;
10
10
  beforeDone: () => Promise<void>;
11
11
  };