mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-23 05:57:05 +02:00
refactor: remove "error" reporting level, move reportMessage to logger (#7642)
This commit is contained in:
parent
1b9bec1042
commit
bfba6a8b02
16 changed files with 117 additions and 116 deletions
|
@ -25,6 +25,7 @@ It exports a single object as default export: `logger`. `logger` has the followi
|
|||
- `warn`: prints a warning that should be payed attention to.
|
||||
- `error`: prints an error (not necessarily halting the program) that signals significant problems.
|
||||
- `success`: prints a success message.
|
||||
- The `report` function. It takes a `ReportingSeverity` value (`ignore`, `log`, `warn`, `throw`) and reports a message according to the severity.
|
||||
|
||||
### A word on the `error` formatter
|
||||
|
||||
|
|
|
@ -124,3 +124,33 @@ describe('success', () => {
|
|||
expect(consoleMock.mock.calls).toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe('report', () => {
|
||||
beforeAll(() => jest.clearAllMocks());
|
||||
it('works with all severities', () => {
|
||||
const consoleLog = jest.spyOn(console, 'info').mockImplementation(() => {});
|
||||
const consoleWarn = jest
|
||||
.spyOn(console, 'warn')
|
||||
.mockImplementation(() => {});
|
||||
logger.report('ignore')('hey');
|
||||
logger.report('log')('hey');
|
||||
logger.report('warn')('hey');
|
||||
expect(() =>
|
||||
logger.report('throw')('hey'),
|
||||
).toThrowErrorMatchingInlineSnapshot(`"hey"`);
|
||||
expect(() =>
|
||||
// @ts-expect-error: for test
|
||||
logger.report('foo')('hey'),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Unexpected "reportingSeverity" value: foo."`,
|
||||
);
|
||||
expect(consoleLog).toBeCalledTimes(1);
|
||||
expect(consoleLog).toBeCalledWith(
|
||||
expect.stringMatching(/.*\[INFO\].* hey/),
|
||||
);
|
||||
expect(consoleWarn).toBeCalledTimes(1);
|
||||
expect(consoleWarn).toBeCalledWith(
|
||||
expect.stringMatching(/.*\[WARNING\].* hey/),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
|
||||
import chalk from 'chalk';
|
||||
import type {ReportingSeverity} from '@docusaurus/types';
|
||||
|
||||
type InterpolatableValue = string | number | (string | number)[];
|
||||
|
||||
|
@ -122,11 +123,54 @@ function success(msg: unknown, ...values: InterpolatableValue[]): void {
|
|||
}`,
|
||||
);
|
||||
}
|
||||
function throwError(msg: unknown): void;
|
||||
function throwError(
|
||||
msg: TemplateStringsArray,
|
||||
...values: [InterpolatableValue, ...InterpolatableValue[]]
|
||||
): void;
|
||||
function throwError(msg: unknown, ...values: InterpolatableValue[]): void {
|
||||
throw new Error(
|
||||
values.length === 0
|
||||
? stringify(msg)
|
||||
: interpolate(msg as TemplateStringsArray, ...values),
|
||||
);
|
||||
}
|
||||
|
||||
function newLine(): void {
|
||||
console.log();
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a message and reports it according to the severity that the user wants.
|
||||
*
|
||||
* - `ignore`: completely no-op
|
||||
* - `log`: uses the `INFO` log level
|
||||
* - `warn`: uses the `WARN` log level
|
||||
* - `throw`: aborts the process, throws the error.
|
||||
*
|
||||
* Since the logger doesn't have logging level filters yet, these severities
|
||||
* mostly just differ by their colors.
|
||||
*
|
||||
* @throws In addition to throwing when `reportingSeverity === "throw"`, this
|
||||
* function also throws if `reportingSeverity` is not one of the above.
|
||||
*/
|
||||
function report(reportingSeverity: ReportingSeverity): typeof success {
|
||||
const reportingMethods = {
|
||||
ignore: () => {},
|
||||
log: info,
|
||||
warn,
|
||||
throw: throwError,
|
||||
};
|
||||
if (
|
||||
!Object.prototype.hasOwnProperty.call(reportingMethods, reportingSeverity)
|
||||
) {
|
||||
throw new Error(
|
||||
`Unexpected "reportingSeverity" value: ${reportingSeverity}.`,
|
||||
);
|
||||
}
|
||||
return reportingMethods[reportingSeverity];
|
||||
}
|
||||
|
||||
const logger = {
|
||||
red: (msg: string | number): string => chalk.red(msg),
|
||||
yellow: (msg: string | number): string => chalk.yellow(msg),
|
||||
|
@ -144,6 +188,7 @@ const logger = {
|
|||
warn,
|
||||
error,
|
||||
success,
|
||||
report,
|
||||
newLine,
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue