Skip to content

Commit fd6be80

Browse files
authored
feat(cli): add a dry-run mode with --check cli flag (#8149)
* feat(cli): add a dry-run mode with `--check` cli flag * fix(cli): typo for `check` typing * Create rude-vans-notice.md * fix(cli): typo
1 parent 6a2e328 commit fd6be80

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

.changeset/rude-vans-notice.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-codegen/cli": minor
3+
---
4+
5+
feat(cli): add a dry-run mode with `--check` cli flag

packages/graphql-codegen-cli/src/bin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { cliError } from './utils/cli-error.js';
55
const [, , cmd] = process.argv;
66

77
runCli(cmd)
8-
.then(() => {
9-
process.exit(0);
8+
.then(returnCode => {
9+
process.exit(returnCode);
1010
})
1111
.catch(error => {
1212
cliError(error);

packages/graphql-codegen-cli/src/cli.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ import { createContext } from './config.js';
44
import { lifecycleHooks } from './hooks.js';
55
import { DetailedError } from '@graphql-codegen/plugin-helpers';
66

7-
export async function runCli(cmd: string): Promise<any> {
7+
export async function runCli(cmd: string): Promise<number> {
88
await ensureGraphQlPackage();
99

1010
if (cmd === 'init') {
11-
return init();
11+
init();
12+
return 0;
1213
}
1314

1415
const context = await createContext();
1516
try {
16-
return await generate(context);
17+
await generate(context);
18+
if (context.checkMode && context.checkModeStaleFiles.length > 0) {
19+
// eslint-disable-next-line no-console
20+
console.log(
21+
`The following stale files were detected:\n${context.checkModeStaleFiles.map(file => ` - ${file}\n`)}`
22+
);
23+
return 1;
24+
}
25+
return 0;
1726
} catch (error) {
1827
await lifecycleHooks(context.getConfig().hooks).onError(error.toString());
28+
return 1;
1929
}
2030
}
2131

packages/graphql-codegen-cli/src/config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type YamlCliFlags = {
3030
silent: boolean;
3131
errorsOnly: boolean;
3232
profile: boolean;
33+
check?: boolean;
3334
verbose?: boolean;
3435
debug?: boolean;
3536
ignoreNoDocuments?: boolean;
@@ -323,6 +324,10 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
323324
context.useProfiler();
324325
}
325326

327+
if (cliFlags.check === true) {
328+
context.enableCheckMode();
329+
}
330+
326331
context.updateConfig(config);
327332
}
328333

@@ -331,12 +336,14 @@ export class CodegenContext {
331336
private _graphqlConfig?: GraphQLConfig;
332337
private config: Types.Config;
333338
private _project?: string;
339+
private _checkMode = false;
334340
private _pluginContext: { [key: string]: any } = {};
335341

336342
cwd: string;
337343
filepath: string;
338344
profiler: Profiler;
339345
profilerOutput?: string;
346+
checkModeStaleFiles = [];
340347

341348
constructor({
342349
config,
@@ -387,6 +394,14 @@ export class CodegenContext {
387394
};
388395
}
389396

397+
enableCheckMode() {
398+
this._checkMode = true;
399+
}
400+
401+
get checkMode() {
402+
return this._checkMode;
403+
}
404+
390405
useProfiler() {
391406
this.profiler = createProfiler();
392407

packages/graphql-codegen-cli/src/generate-and-save.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ export async function generate(
7373

7474
if (previousHash && currentHash === previousHash) {
7575
debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
76-
7776
return;
77+
} else if (context.checkMode) {
78+
context.checkModeStaleFiles.push(result.filename);
79+
return; // skip updating file in dry mode
7880
}
7981

8082
if (content.length === 0) {

website/src/pages/docs/config-reference/codegen-config.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,21 @@ The Codegen also supports several CLI flags that allow you to override the defau
143143

144144
- **`--project` (`-p`)** - To generate only one project out of a [Multi Project](multiproject-config) config file.
145145

146+
- **`--check`** - Enable dry-run mode (see below)
147+
148+
## Dry-run mode
149+
150+
Codegen can be run in dry-run mode to check if some new changes are detected:
151+
152+
```bash
153+
yarn run codegen --check
154+
```
155+
156+
When enabled, codegen will return the following exit code:
157+
158+
- `0`: no changes were detected
159+
- `1`: some changes are missing in existing files
160+
146161
## Debug Mode
147162

148163
To enable debug mode, either set the `debug: true` configuration option or use the CLI `--debug` flag.

0 commit comments

Comments
 (0)