Skip to content

Commit 857c603

Browse files
authoredOct 21, 2020
Add errors-only flag to graphql-codegen-cli (#4931)
* Add errors-only flag to graphql-codegen-cli * Add changeset
1 parent 40d850c commit 857c603

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed
 

‎.changeset/brown-eels-chew.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/cli': minor
3+
'@graphql-codegen/plugin-helpers': minor
4+
---
5+
6+
Adds the --errors-only flag to the cli to print errors only.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '@graphql-codegen/plugin-helpers';
99
import { codegen } from '@graphql-codegen/core';
1010

11-
import { Renderer } from './utils/listr-renderer';
11+
import { Renderer, ErrorRenderer } from './utils/listr-renderer';
1212
import { GraphQLError, GraphQLSchema, DocumentNode, parse } from 'graphql';
1313
import { getPluginByName } from './plugins';
1414
import { getPresetByName } from './presets';
@@ -72,7 +72,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
7272
} else {
7373
listr = new Listr({
7474
...commonListrOptions,
75-
renderer: config.silent ? 'silent' : Renderer,
75+
renderer: config.silent ? 'silent' : config.errorsOnly ? ErrorRenderer : Renderer,
7676
nonTTYRenderer: config.silent ? 'silent' : 'default',
7777
collapse: true,
7878
clearOutput: false,

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

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type YamlCliFlags = {
1414
overwrite: boolean;
1515
project: string;
1616
silent: boolean;
17+
errorsOnly: boolean;
1718
};
1819

1920
function generateSearchPlaces(moduleName: string) {
@@ -151,6 +152,11 @@ export function buildOptions() {
151152
describe: 'Suppresses printing errors',
152153
type: 'boolean' as const,
153154
},
155+
e: {
156+
alias: 'errors-only',
157+
describe: 'Only print errors',
158+
type: 'boolean' as const,
159+
},
154160
p: {
155161
alias: 'project',
156162
describe: 'Name of a project in GraphQL Config',
@@ -191,6 +197,10 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
191197
config.silent = cliFlags.silent;
192198
}
193199

200+
if (cliFlags.errorsOnly === true) {
201+
config.errorsOnly = cliFlags.errorsOnly;
202+
}
203+
194204
if (cliFlags.project) {
195205
context.useProject(cliFlags.project);
196206
}

‎packages/graphql-codegen-cli/src/utils/listr-renderer.ts

+40
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ export class Renderer {
7979
}
8080
}
8181

82+
const render = tasks => {
83+
for (const task of tasks) {
84+
task.subscribe(
85+
event => {
86+
if (event.type === 'SUBTASKS') {
87+
render(task.subtasks);
88+
return;
89+
}
90+
91+
if (event.type === 'DATA') {
92+
logUpdate.emit(chalk.dim(`${event.data}`));
93+
}
94+
logUpdate.done();
95+
},
96+
err => {
97+
logUpdate.emit(err);
98+
logUpdate.done();
99+
}
100+
);
101+
}
102+
};
103+
104+
export class ErrorRenderer {
105+
private tasks: any;
106+
107+
constructor(tasks, options) {
108+
this.tasks = tasks;
109+
}
110+
111+
render() {
112+
render(this.tasks);
113+
}
114+
115+
static get nonTTY() {
116+
return true;
117+
}
118+
119+
end() {}
120+
}
121+
82122
class LogUpdate {
83123
private stream = process.stdout;
84124
// state

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

+4
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,10 @@ export namespace Types {
429429
* @description A flag to suppress printing errors when they occur.
430430
*/
431431
silent?: boolean;
432+
/**
433+
* @description A flag to print only errors.
434+
*/
435+
errorsOnly?: boolean;
432436
/**
433437
* @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.
434438
*/

0 commit comments

Comments
 (0)
Please sign in to comment.