Skip to content

Commit

Permalink
Add errors-only flag to graphql-codegen-cli (#4931)
Browse files Browse the repository at this point in the history
* Add errors-only flag to graphql-codegen-cli

* Add changeset
  • Loading branch information
maurobalbi committed Oct 21, 2020
1 parent 40d850c commit 857c603
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/brown-eels-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/cli': minor
'@graphql-codegen/plugin-helpers': minor
---

Adds the --errors-only flag to the cli to print errors only.
4 changes: 2 additions & 2 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@graphql-codegen/plugin-helpers';
import { codegen } from '@graphql-codegen/core';

import { Renderer } from './utils/listr-renderer';
import { Renderer, ErrorRenderer } from './utils/listr-renderer';
import { GraphQLError, GraphQLSchema, DocumentNode, parse } from 'graphql';
import { getPluginByName } from './plugins';
import { getPresetByName } from './presets';
Expand Down Expand Up @@ -72,7 +72,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
} else {
listr = new Listr({
...commonListrOptions,
renderer: config.silent ? 'silent' : Renderer,
renderer: config.silent ? 'silent' : config.errorsOnly ? ErrorRenderer : Renderer,
nonTTYRenderer: config.silent ? 'silent' : 'default',
collapse: true,
clearOutput: false,
Expand Down
10 changes: 10 additions & 0 deletions packages/graphql-codegen-cli/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type YamlCliFlags = {
overwrite: boolean;
project: string;
silent: boolean;
errorsOnly: boolean;
};

function generateSearchPlaces(moduleName: string) {
Expand Down Expand Up @@ -151,6 +152,11 @@ export function buildOptions() {
describe: 'Suppresses printing errors',
type: 'boolean' as const,
},
e: {
alias: 'errors-only',
describe: 'Only print errors',
type: 'boolean' as const,
},
p: {
alias: 'project',
describe: 'Name of a project in GraphQL Config',
Expand Down Expand Up @@ -191,6 +197,10 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
config.silent = cliFlags.silent;
}

if (cliFlags.errorsOnly === true) {
config.errorsOnly = cliFlags.errorsOnly;
}

if (cliFlags.project) {
context.useProject(cliFlags.project);
}
Expand Down
40 changes: 40 additions & 0 deletions packages/graphql-codegen-cli/src/utils/listr-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ export class Renderer {
}
}

const render = tasks => {
for (const task of tasks) {
task.subscribe(
event => {
if (event.type === 'SUBTASKS') {
render(task.subtasks);
return;
}

if (event.type === 'DATA') {
logUpdate.emit(chalk.dim(`${event.data}`));
}
logUpdate.done();
},
err => {
logUpdate.emit(err);
logUpdate.done();
}
);
}
};

export class ErrorRenderer {
private tasks: any;

constructor(tasks, options) {
this.tasks = tasks;
}

render() {
render(this.tasks);
}

static get nonTTY() {
return true;
}

end() {}
}

class LogUpdate {
private stream = process.stdout;
// state
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/plugins-helpers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ export namespace Types {
* @description A flag to suppress printing errors when they occur.
*/
silent?: boolean;
/**
* @description A flag to print only errors.
*/
errorsOnly?: boolean;
/**
* @description If you are using the programmatic API in a browser environment, you can override this configuration to load your plugins in a way different than require.
*/
Expand Down

0 comments on commit 857c603

Please sign in to comment.