Skip to content

Commit

Permalink
feat(cli): --verbose and --debug flags (#8147)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
charlypoly committed Jul 27, 2022
1 parent 6a632e2 commit 6a2e328
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/real-toes-dance.md
@@ -0,0 +1,6 @@
---
"@graphql-codegen/cli": patch
"@graphql-codegen/plugin-helpers": patch
---

feat(cli): `--verbose` and `--debug` flags
7 changes: 5 additions & 2 deletions packages/graphql-codegen-cli/src/codegen.ts
Expand Up @@ -189,7 +189,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom

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

const tasks = new Listr<Ctx>(
const tasks = new Listr<Ctx, 'default' | 'verbose'>(
[
{
title: 'Parse Configuration',
Expand Down Expand Up @@ -393,6 +393,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
clearOutput: false,
collapse: true,
},
renderer: config.verbose ? 'verbose' : 'default',
ctx: { errors: [] },
rendererSilent: isTest || config.silent,
exitOnError: true,
Expand All @@ -415,7 +416,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
throw newErr;
}

printLogs();
if (config.debug) {
printLogs();
}

return result;
}
22 changes: 22 additions & 0 deletions packages/graphql-codegen-cli/src/config.ts
Expand Up @@ -30,6 +30,8 @@ export type YamlCliFlags = {
silent: boolean;
errorsOnly: boolean;
profile: boolean;
verbose?: boolean;
debug?: boolean;
ignoreNoDocuments?: boolean;
emitLegacyCommonJSImports?: boolean;
};
Expand Down Expand Up @@ -234,6 +236,18 @@ export function buildOptions() {
describe: 'Name of a project in GraphQL Config',
type: 'string' as const,
},
v: {
alias: 'verbose',
describe: 'output more detailed information about performed tasks',
type: 'boolean' as const,
default: false,
},
d: {
alias: 'debug',
describe: 'Print debug logs to stdout',
type: 'boolean' as const,
default: false,
},
};
}

Expand Down Expand Up @@ -279,6 +293,14 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
config.silent = cliFlags.silent;
}

if (cliFlags.verbose === true || process.env.VERBOSE) {
config.verbose = true;
}

if (cliFlags.debug === true || process.env.DEBUG) {
config.debug = true;
}

if (cliFlags.errorsOnly === true) {
config.errorsOnly = cliFlags.errorsOnly;
}
Expand Down
20 changes: 8 additions & 12 deletions packages/graphql-codegen-cli/src/utils/debugging.ts
Expand Up @@ -6,21 +6,17 @@ let queue: Array<{
}> = [];

export function debugLog(message: string, ...meta: any[]) {
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
queue.push({
message,
meta,
});
}
queue.push({
message,
meta,
});
}

export function printLogs() {
if (!process.env.GQL_CODEGEN_NODEBUG && process.env.DEBUG !== undefined) {
queue.forEach(log => {
getLogger().info(log.message, ...log.meta);
});
resetLogs();
}
queue.forEach(log => {
getLogger().info(log.message, ...log.meta);
});
resetLogs();
}

export function resetLogs() {
Expand Down
8 changes: 8 additions & 0 deletions packages/utils/plugins-helpers/src/types.ts
Expand Up @@ -445,6 +445,14 @@ export namespace Types {
* @description A flag to suppress printing errors when they occur.
*/
silent?: boolean;
/**
* @description A flag to output more detailed information about tasks
*/
verbose?: boolean;
/**
* @description A flag to output debug logs
*/
debug?: boolean;
/**
* @description A flag to print only errors.
*/
Expand Down
14 changes: 12 additions & 2 deletions website/src/pages/docs/config-reference/codegen-config.mdx
Expand Up @@ -67,6 +67,10 @@ Here are the supported options that you can define in the config file (see [sour

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

- **`debug`** - A flag to enable printing debug logs

- **`verbose`** - A flag to enable tasks verbose mode

- **`ignoreNoDocuments`** - A flag to not exit with non-zero exit code when there are no documents

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

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

- **`--verbose` (`-v`)** - Overrides the `verbose` config to true.

- **`--debug` (`-d`)** - Overrides the `debug` config to true.

- **`--errors-only` (`-e`)** - Overrides the `errorsOnly` config to true.

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

## Debug Mode

You can set the `DEBUG` environment variable to `1` to print debug information.
To enable debug mode, either set the `debug: true` configuration option or use the CLI `--debug` flag.

For more detailed output, you can also enable the `verbose: true` or `--verbose` CLI flag.

You can set the `VERBOSE` environment variable to `1` to print more information regarding the CLI output (`listr`).
> `DEBUG=1` and `VERBOSE=1` environment variables are deprecated but still supported until the next major.
## Other ways to provide configuration

Expand Down

1 comment on commit 6a2e328

@vercel
Copy link

@vercel vercel bot commented on 6a2e328 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.