From 6a2e328e6b2e978cc304e0d54f72cb336d98bebc Mon Sep 17 00:00:00 2001 From: Charly POLY <1252066+charlypoly@users.noreply.github.com> Date: Wed, 27 Jul 2022 14:06:48 +0200 Subject: [PATCH] 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 --- .changeset/real-toes-dance.md | 6 +++++ packages/graphql-codegen-cli/src/codegen.ts | 7 ++++-- packages/graphql-codegen-cli/src/config.ts | 22 +++++++++++++++++++ .../src/utils/debugging.ts | 20 +++++++---------- packages/utils/plugins-helpers/src/types.ts | 8 +++++++ .../docs/config-reference/codegen-config.mdx | 14 ++++++++++-- 6 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 .changeset/real-toes-dance.md diff --git a/.changeset/real-toes-dance.md b/.changeset/real-toes-dance.md new file mode 100644 index 00000000000..96c02bfa17d --- /dev/null +++ b/.changeset/real-toes-dance.md @@ -0,0 +1,6 @@ +--- +"@graphql-codegen/cli": patch +"@graphql-codegen/plugin-helpers": patch +--- + +feat(cli): `--verbose` and `--debug` flags diff --git a/packages/graphql-codegen-cli/src/codegen.ts b/packages/graphql-codegen-cli/src/codegen.ts index fe57cca988e..5bf73432b6f 100644 --- a/packages/graphql-codegen-cli/src/codegen.ts +++ b/packages/graphql-codegen-cli/src/codegen.ts @@ -189,7 +189,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom const isTest = process.env.NODE_ENV === 'test'; - const tasks = new Listr( + const tasks = new Listr( [ { title: 'Parse Configuration', @@ -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, @@ -415,7 +416,9 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom throw newErr; } - printLogs(); + if (config.debug) { + printLogs(); + } return result; } diff --git a/packages/graphql-codegen-cli/src/config.ts b/packages/graphql-codegen-cli/src/config.ts index c084862c687..346cf67ce67 100644 --- a/packages/graphql-codegen-cli/src/config.ts +++ b/packages/graphql-codegen-cli/src/config.ts @@ -30,6 +30,8 @@ export type YamlCliFlags = { silent: boolean; errorsOnly: boolean; profile: boolean; + verbose?: boolean; + debug?: boolean; ignoreNoDocuments?: boolean; emitLegacyCommonJSImports?: boolean; }; @@ -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, + }, }; } @@ -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; } diff --git a/packages/graphql-codegen-cli/src/utils/debugging.ts b/packages/graphql-codegen-cli/src/utils/debugging.ts index 943ed83d6d5..93e9382a511 100644 --- a/packages/graphql-codegen-cli/src/utils/debugging.ts +++ b/packages/graphql-codegen-cli/src/utils/debugging.ts @@ -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() { diff --git a/packages/utils/plugins-helpers/src/types.ts b/packages/utils/plugins-helpers/src/types.ts index 7080db4b0bd..99f018a6bc4 100644 --- a/packages/utils/plugins-helpers/src/types.ts +++ b/packages/utils/plugins-helpers/src/types.ts @@ -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. */ diff --git a/website/src/pages/docs/config-reference/codegen-config.mdx b/website/src/pages/docs/config-reference/codegen-config.mdx index fe8d70a131d..b1c9076b626 100644 --- a/website/src/pages/docs/config-reference/codegen-config.mdx +++ b/website/src/pages/docs/config-reference/codegen-config.mdx @@ -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. @@ -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. @@ -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