forked from OKEAMAH/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.js
More file actions
109 lines (92 loc) · 2.36 KB
/
logger.js
File metadata and controls
109 lines (92 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// @flow
import chalk from 'chalk';
import type Package from '../Package';
import { toString, type Message } from './messages';
type LoggerOpts = {
prefix?: string | false,
emoji?: string
};
function fmt(str: Message | Buffer | string, opts: LoggerOpts = {}) {
let result = toString(str);
let prefix = opts.prefix || '';
if (opts.emoji) {
prefix = `${opts.emoji} ${prefix}`;
}
if (prefix) {
result = result
.trimRight()
.split('\n')
.map(line => `${prefix} ${line}`)
.join('\n');
}
return result;
}
function prompt(pkg, cmd) {
let prompt = pkg ? '(' + pkg.config.getName() + ')' : '';
if (!cmd) {
return prompt;
}
return prompt + ' $ ' + cmd;
}
function write(
message: Message | Buffer | string,
opts: LoggerOpts = {},
err: boolean = false
) {
if (err) {
console.error(fmt(message, opts));
} else {
console.log(fmt(message, opts));
}
}
export function title(
title: Message,
subtitle: Message,
opts: LoggerOpts = {}
) {
let str = chalk.bold(title);
if (subtitle) str += ' ' + chalk.dim(subtitle);
write(str, opts);
}
const INFO_PREFIX = chalk.cyan('info');
const WARN_PREFIX = chalk.yellow('warn');
const ERROR_PREFIX = chalk.red('error');
const SUCCESS_PREFIX = chalk.green('success');
export function info(message: Message, opts: LoggerOpts = {}) {
write(message, { prefix: INFO_PREFIX, ...opts }, true);
}
export function warn(message: Message, opts: LoggerOpts = {}) {
write(message, { prefix: WARN_PREFIX, ...opts }, true);
}
export function error(message: Message, opts: LoggerOpts = {}) {
write(message, { prefix: ERROR_PREFIX, ...opts }, true);
}
export function success(message: Message, opts: LoggerOpts = {}) {
write(message, { prefix: SUCCESS_PREFIX, ...opts }, true);
}
export function stdout(
cmd: string,
data: Buffer,
pkg?: Package,
opts: LoggerOpts = {}
) {
let prefix = chalk.cyan(prompt(pkg, cmd));
write(data, { prefix, ...opts }, false);
}
export function stderr(
cmd: string,
data: Buffer,
pkg?: Package,
opts: LoggerOpts = {}
) {
let prefix = chalk.red(prompt(pkg, cmd));
write(data, { prefix, ...opts }, true);
}
export function cmd(cmd: string, args: Array<string>, opts: LoggerOpts = {}) {
let msg = chalk.dim(prompt(null, cmd));
if (args.length) {
msg += ' ';
msg += chalk.magenta(args.join(' '));
}
write(msg, {}, true);
}