Skip to content

Commit 6a2e328

Browse files
authored
feat(cli): --verbose and --debug flags (#8147)
* feat(cli): `--verbose` and `--debug` flags * Create real-toes-dance.md * fix(cli): `--verbose` and `--debug` cli flags forcing * doc(core): update documentation for debug mode * style: prettier * feat(cli): bring back support of `DEBUG=1` and `VERBOSE=1` environment variables * style: prettier
1 parent 6a632e2 commit 6a2e328

File tree

6 files changed

+61
-16
lines changed

6 files changed

+61
-16
lines changed

.changeset/real-toes-dance.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@graphql-codegen/cli": patch
3+
"@graphql-codegen/plugin-helpers": patch
4+
---
5+
6+
feat(cli): `--verbose` and `--debug` flags

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
189189

190190
const isTest = process.env.NODE_ENV === 'test';
191191

192-
const tasks = new Listr<Ctx>(
192+
const tasks = new Listr<Ctx, 'default' | 'verbose'>(
193193
[
194194
{
195195
title: 'Parse Configuration',
@@ -393,6 +393,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
393393
clearOutput: false,
394394
collapse: true,
395395
},
396+
renderer: config.verbose ? 'verbose' : 'default',
396397
ctx: { errors: [] },
397398
rendererSilent: isTest || config.silent,
398399
exitOnError: true,
@@ -415,7 +416,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
415416
throw newErr;
416417
}
417418

418-
printLogs();
419+
if (config.debug) {
420+
printLogs();
421+
}
419422

420423
return result;
421424
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export type YamlCliFlags = {
3030
silent: boolean;
3131
errorsOnly: boolean;
3232
profile: boolean;
33+
verbose?: boolean;
34+
debug?: boolean;
3335
ignoreNoDocuments?: boolean;
3436
emitLegacyCommonJSImports?: boolean;
3537
};
@@ -234,6 +236,18 @@ export function buildOptions() {
234236
describe: 'Name of a project in GraphQL Config',
235237
type: 'string' as const,
236238
},
239+
v: {
240+
alias: 'verbose',
241+
describe: 'output more detailed information about performed tasks',
242+
type: 'boolean' as const,
243+
default: false,
244+
},
245+
d: {
246+
alias: 'debug',
247+
describe: 'Print debug logs to stdout',
248+
type: 'boolean' as const,
249+
default: false,
250+
},
237251
};
238252
}
239253

@@ -279,6 +293,14 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
279293
config.silent = cliFlags.silent;
280294
}
281295

296+
if (cliFlags.verbose === true || process.env.VERBOSE) {
297+
config.verbose = true;
298+
}
299+
300+
if (cliFlags.debug === true || process.env.DEBUG) {
301+
config.debug = true;
302+
}
303+
282304
if (cliFlags.errorsOnly === true) {
283305
config.errorsOnly = cliFlags.errorsOnly;
284306
}

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ let queue: Array<{
66
}> = [];
77

88
export function debugLog(message: string, ...meta: any[]) {
9-
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
10-
queue.push({
11-
message,
12-
meta,
13-
});
14-
}
9+
queue.push({
10+
message,
11+
meta,
12+
});
1513
}
1614

1715
export function printLogs() {
18-
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
19-
queue.forEach(log => {
20-
getLogger().info(log.message, ...log.meta);
21-
});
22-
resetLogs();
23-
}
16+
queue.forEach(log => {
17+
getLogger().info(log.message, ...log.meta);
18+
});
19+
resetLogs();
2420
}
2521

2622
export function resetLogs() {

packages/utils/plugins-helpers/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ export namespace Types {
445445
* @description A flag to suppress printing errors when they occur.
446446
*/
447447
silent?: boolean;
448+
/**
449+
* @description A flag to output more detailed information about tasks
450+
*/
451+
verbose?: boolean;
452+
/**
453+
* @description A flag to output debug logs
454+
*/
455+
debug?: boolean;
448456
/**
449457
* @description A flag to print only errors.
450458
*/

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Here are the supported options that you can define in the config file (see [sour
6767

6868
- **`silent`** - A flag to suppress printing errors when they occur
6969

70+
- **`debug`** - A flag to enable printing debug logs
71+
72+
- **`verbose`** - A flag to enable tasks verbose mode
73+
7074
- **`ignoreNoDocuments`** - A flag to not exit with non-zero exit code when there are no documents
7175

7276
- **`emitLegacyCommonJSImports`** - A flag to emit imports without `.js` extension. Enabled by default.
@@ -125,6 +129,10 @@ The Codegen also supports several CLI flags that allow you to override the defau
125129

126130
- **`--silent` (`-s`)** - Overrides the `silent` config to true.
127131

132+
- **`--verbose` (`-v`)** - Overrides the `verbose` config to true.
133+
134+
- **`--debug` (`-d`)** - Overrides the `debug` config to true.
135+
128136
- **`--errors-only` (`-e`)** - Overrides the `errorsOnly` config to true.
129137

130138
- **`--require` (`-r`)** - Specifies `require.extensions` before loading the `.yml` file.
@@ -137,9 +145,11 @@ The Codegen also supports several CLI flags that allow you to override the defau
137145

138146
## Debug Mode
139147

140-
You can set the `DEBUG` environment variable to `1` to print debug information.
148+
To enable debug mode, either set the `debug: true` configuration option or use the CLI `--debug` flag.
149+
150+
For more detailed output, you can also enable the `verbose: true` or `--verbose` CLI flag.
141151

142-
You can set the `VERBOSE` environment variable to `1` to print more information regarding the CLI output (`listr`).
152+
> `DEBUG=1` and `VERBOSE=1` environment variables are deprecated but still supported until the next major.
143153

144154
## Other ways to provide configuration
145155

0 commit comments

Comments
 (0)