@graphql-codegen/cli 2.15.1-alpha-20221207042805-858684199 → 2.16.0
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/generate-and-save.js +15 -4
- package/cjs/hooks.js +34 -10
- package/esm/generate-and-save.js +15 -4
- package/esm/hooks.js +34 -10
- package/package.json +3 -3
- package/typings/hooks.d.cts +1 -1
- package/typings/hooks.d.ts +1 -1
package/cjs/generate-and-save.js
CHANGED
|
@@ -55,7 +55,7 @@ async function generate(input, saveToFile = true) {
|
|
|
55
55
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
|
-
|
|
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,
|
|
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
|
-
|
|
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 () =>
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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;
|
package/esm/generate-and-save.js
CHANGED
|
@@ -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
|
-
|
|
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
|
|
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
|
-
|
|
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 () =>
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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.
|
|
3
|
+
"version": "2.16.0",
|
|
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
|
|
12
|
-
"@graphql-codegen/plugin-helpers": "3.
|
|
11
|
+
"@graphql-codegen/core": "2.6.7",
|
|
12
|
+
"@graphql-codegen/plugin-helpers": "^3.1.0",
|
|
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",
|
package/typings/hooks.d.cts
CHANGED
|
@@ -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<
|
|
8
|
+
beforeOneFileWrite: (path: string, content: string) => Promise<string>;
|
|
9
9
|
beforeAllFileWrite: (paths: string[]) => Promise<void>;
|
|
10
10
|
beforeDone: () => Promise<void>;
|
|
11
11
|
};
|
package/typings/hooks.d.ts
CHANGED
|
@@ -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<
|
|
8
|
+
beforeOneFileWrite: (path: string, content: string) => Promise<string>;
|
|
9
9
|
beforeAllFileWrite: (paths: string[]) => Promise<void>;
|
|
10
10
|
beforeDone: () => Promise<void>;
|
|
11
11
|
};
|