Skip to content

Commit

Permalink
Enhance: Output streams for error and warning messages are separated
Browse files Browse the repository at this point in the history
- [enhance] Separate output streams for error and warning messages
  - warn messages are printed to stdout and error messages are printed according to the `reasonerStream` setting.
  • Loading branch information
imjuni committed Nov 4, 2023
1 parent 64d966c commit d11de2c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 54 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ module.exports = {
'no-console': ['off'],
},
},
{
files: ['src/cli/ux/Reasoner.ts'],
rules: {
'no-console': ['off'],
},
},
],
settings: {
'import/resolver': {
Expand Down
104 changes: 50 additions & 54 deletions src/cli/ux/Reasoner.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable no-console */
import type { IReason } from '#/compilers/interfaces/IReason';
import type { TStreamType } from '#/configs/interfaces/TStreamType';
import chalk from 'chalk';
import { sleep as sleepMs } from 'my-easy-fp';
import * as path from 'path';

export class Reasoner {
Expand Down Expand Up @@ -34,21 +32,24 @@ export class Reasoner {

#stream: TStreamType;

#func: typeof console.log | typeof console.error;
#logger: typeof console.log;

#streamFunc: typeof console.log | typeof console.error;

constructor(
func: typeof console.log | typeof console.error,
stream: TStreamType,
enable: boolean,
) {
this.#func = func;
this.#streamFunc = func;
this.#stream = stream;
this.#enable = enable;
this.#logger = console.log;
}

set stream(value: TStreamType) {
if (value !== this.#stream) {
this.#func = value === 'stderr' ? console.error : console.log;
this.#streamFunc = value === 'stderr' ? console.error : console.log;
this.#stream = value;
}
}
Expand All @@ -61,68 +62,63 @@ export class Reasoner {
this.#enable = value;
}

async sleep(ms: number): Promise<void> {
if (this.#enable) {
await sleepMs(ms);
}
}
static messaging(reason: IReason): string {
const messageBlock = [''];

space(): void {
if (this.#enable === false) {
return;
}
const typeMessage =
reason.type === 'error'
? chalk.bgRed(` ${reason.type.toUpperCase()} `)
: chalk.bgYellow(` ${chalk.black(reason.type.toUpperCase())} `);

this.#func('');
}

start(reasons: IReason[]): void {
if (this.#enable === false) {
return;
}
const { filePath } = reason;

const messages = reasons.map((reason) => {
const messageBlock = [''];
const filename =
reason.lineAndCharacter == null
? `${path.basename(filePath)}`
: `${path.basename(filePath)}:${reason.lineAndCharacter.line}:${
reason.lineAndCharacter.character
}`;

const typeMessage =
reason.type === 'error'
? chalk.bgRed(` ${reason.type.toUpperCase()} `)
: chalk.bgYellow(` ${chalk.black(reason.type.toUpperCase())} `);
const chevronRight = reason.type === 'error' ? chalk.red('>') : chalk.yellow('>');

const { filePath } = reason;
messageBlock.push(`${typeMessage} ${filename}`);

const filename =
reason.lineAndCharacter == null
? `${path.basename(filePath)}`
: `${path.basename(filePath)}:${reason.lineAndCharacter.line}:${
reason.lineAndCharacter.character
}`;
if (reason.lineAndCharacter == null) {
messageBlock.push(` ${chevronRight} ${chalk.gray(`${filePath}`)}`);
} else {
messageBlock.push(
` ${chevronRight} ${chalk.gray(
`${filePath}:${reason.lineAndCharacter.line}:${reason.lineAndCharacter.character}`,
)}`,
);
}

const chevronRight = reason.type === 'error' ? chalk.red('>') : chalk.yellow('>');
messageBlock.push(
...reason.message.split('\n').map((splittedMessage) => {
return ` ${chevronRight} ${chalk.gray(splittedMessage.trim())}`;
}),
);

messageBlock.push(`${typeMessage} ${filename}`);
messageBlock.push('');

if (reason.lineAndCharacter == null) {
messageBlock.push(` ${chevronRight} ${chalk.gray(`${filePath}`)}`);
} else {
messageBlock.push(
` ${chevronRight} ${chalk.gray(
`${filePath}:${reason.lineAndCharacter.line}:${reason.lineAndCharacter.character}`,
)}`,
);
}
return messageBlock.join('\n');
}

messageBlock.push(
...reason.message.split('\n').map((splittedMessage) => {
return ` ${chevronRight} ${chalk.gray(splittedMessage.trim())}`;
}),
);
start(reasons: IReason[]): void {
if (this.#enable === false) {
return;
}

messageBlock.push('');
const errors = reasons
.filter((reason) => reason.type === 'error')
.map((reason) => Reasoner.messaging(reason));

return messageBlock.join('\n');
});
const warns = reasons
.filter((reason) => reason.type === 'warn')
.map((reason) => Reasoner.messaging(reason));

this.#func(messages.join(''));
this.#logger(warns.join(''));
this.#streamFunc(errors.join(''));
}
}

Expand Down

0 comments on commit d11de2c

Please sign in to comment.