Skip to content

Commit

Permalink
feat(cli): add a dry-run mode with --check cli flag (#8149)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
charlypoly authored Jul 27, 2022
1 parent 6a2e328 commit fd6be80
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-vans-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphql-codegen/cli": minor
---

feat(cli): add a dry-run mode with `--check` cli flag
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { cliError } from './utils/cli-error.js';
const [, , cmd] = process.argv;

runCli(cmd)
.then(() => {
process.exit(0);
.then(returnCode => {
process.exit(returnCode);
})
.catch(error => {
cliError(error);
Expand Down
16 changes: 13 additions & 3 deletions packages/graphql-codegen-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@ import { createContext } from './config.js';
import { lifecycleHooks } from './hooks.js';
import { DetailedError } from '@graphql-codegen/plugin-helpers';

export async function runCli(cmd: string): Promise<any> {
export async function runCli(cmd: string): Promise<number> {
await ensureGraphQlPackage();

if (cmd === 'init') {
return init();
init();
return 0;
}

const context = await createContext();
try {
return await generate(context);
await generate(context);
if (context.checkMode && context.checkModeStaleFiles.length > 0) {
// eslint-disable-next-line no-console
console.log(
`The following stale files were detected:\n${context.checkModeStaleFiles.map(file => ` - ${file}\n`)}`
);
return 1;
}
return 0;
} catch (error) {
await lifecycleHooks(context.getConfig().hooks).onError(error.toString());
return 1;
}
}

Expand Down
15 changes: 15 additions & 0 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export type YamlCliFlags = {
silent: boolean;
errorsOnly: boolean;
profile: boolean;
check?: boolean;
verbose?: boolean;
debug?: boolean;
ignoreNoDocuments?: boolean;
Expand Down Expand Up @@ -323,6 +324,10 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
context.useProfiler();
}

if (cliFlags.check === true) {
context.enableCheckMode();
}

context.updateConfig(config);
}

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

cwd: string;
filepath: string;
profiler: Profiler;
profilerOutput?: string;
checkModeStaleFiles = [];

constructor({
config,
Expand Down Expand Up @@ -387,6 +394,14 @@ export class CodegenContext {
};
}

enableCheckMode() {
this._checkMode = true;
}

get checkMode() {
return this._checkMode;
}

useProfiler() {
this.profiler = createProfiler();

Expand Down
4 changes: 3 additions & 1 deletion packages/graphql-codegen-cli/src/generate-and-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ export async function generate(

if (previousHash && currentHash === previousHash) {
debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);

return;
} else if (context.checkMode) {
context.checkModeStaleFiles.push(result.filename);
return; // skip updating file in dry mode
}

if (content.length === 0) {
Expand Down
15 changes: 15 additions & 0 deletions website/src/pages/docs/config-reference/codegen-config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ The Codegen also supports several CLI flags that allow you to override the defau

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

- **`--check`** - Enable dry-run mode (see below)

## Dry-run mode

Codegen can be run in dry-run mode to check if some new changes are detected:

```bash
yarn run codegen --check
```

When enabled, codegen will return the following exit code:

- `0`: no changes were detected
- `1`: some changes are missing in existing files

## Debug Mode

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

1 comment on commit fd6be80

@vercel
Copy link

@vercel vercel bot commented on fd6be80 Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.