UNPKG

1.19 kBJavaScriptView Raw
1/**
2 * Copyright 2017-present, Callstack.
3 * All rights reserved.
4 *
5 * @flow
6 */
7import type { Logger } from './types';
8
9const chalk = require('chalk');
10const stripAnsi = require('strip-ansi');
11
12let lastChar;
13
14const last = arr => arr[arr.length - 1];
15
16const section = (...args) => {
17 if (lastChar !== '\n') {
18 console.log();
19 }
20 log(...args);
21};
22
23const log = (...args) => {
24 lastChar = last(stripAnsi(last(args)));
25 console.log(...args);
26};
27
28function reset() {
29 const logger = {
30 info: (...args: any[]) => section(chalk.black.bgCyan(' INFO '), ...args),
31 warn: (...args: any[]) => section(chalk.black.bgYellow(' WARN '), ...args),
32 error: (...args: any[]) => section(chalk.black.bgRed(' ERROR '), ...args),
33 done: (...args: any[]) => section(chalk.black.bgGreen(' DONE '), ...args),
34 debug: (prefix: string, ...args: any[]) =>
35 log(
36 chalk.cyan(prefix.toUpperCase()),
37 ...args.map(str => chalk.grey(str))
38 ),
39 reset,
40 };
41
42 Object.defineProperty(logger, 'reset', {
43 ...Object.getOwnPropertyDescriptor(logger, 'reset').value,
44 writable: false,
45 });
46
47 return logger;
48}
49
50const logger: Logger = reset();
51
52module.exports = logger;