1 | import chalk from 'chalk';
|
2 | import logUpdate from 'log-update';
|
3 | import indentString from 'indent-string';
|
4 | import logSymbol from 'log-symbols';
|
5 | import UpdateRenderer from 'listr-update-renderer';
|
6 | import { stripIndent } from 'common-tags';
|
7 | import { isDetailedError } from '../errors';
|
8 | import { debugLog, printLogs } from 'graphql-codegen-core';
|
9 | export class Renderer {
|
10 | constructor(tasks, options) {
|
11 | this.updateRenderer = new UpdateRenderer(tasks, options);
|
12 | }
|
13 | render() {
|
14 | return this.updateRenderer.render();
|
15 | }
|
16 | end(err) {
|
17 | this.updateRenderer.end(err);
|
18 | if (typeof err === undefined) {
|
19 | logUpdate.clear();
|
20 | return;
|
21 | }
|
22 |
|
23 | logUpdate.done();
|
24 |
|
25 | if (err) {
|
26 | const errorCount = err.errors ? err.errors.length : 0;
|
27 | if (errorCount > 0) {
|
28 | const count = indentString(chalk.red.bold(`Found ${errorCount} error${errorCount > 1 ? 's' : ''}`), 1);
|
29 | const details = err.errors
|
30 | .map(error => {
|
31 | debugLog(`[CLI] Exited with an error`, error);
|
32 | return isDetailedError(error) ? error.details : error;
|
33 | })
|
34 | .map((msg, i) => {
|
35 | const source = err.errors[i].source;
|
36 | msg = chalk.gray(indentString(stripIndent(`${msg}`), 4));
|
37 | if (source) {
|
38 | const sourceOfError = typeof source === 'string' ? source : source.name;
|
39 | const title = indentString(`${logSymbol.error} ${sourceOfError}`, 2);
|
40 | return [title, msg].join('\n');
|
41 | }
|
42 | return msg;
|
43 | })
|
44 | .join('\n\n');
|
45 | logUpdate(['', count, details, ''].join('\n\n'));
|
46 | }
|
47 | else {
|
48 | logUpdate(chalk.red.bold(err.message));
|
49 | }
|
50 | }
|
51 | logUpdate.done();
|
52 | printLogs();
|
53 | }
|
54 | }
|
55 |
|
\ | No newline at end of file |