|
1
|
|
|
'use strict' |
|
2
|
|
|
|
|
3
|
|
|
const ora = require('ora') |
|
4
|
|
|
const chalk = require('chalk') |
|
5
|
|
|
const moment = require('moment') |
|
6
|
|
|
const format = require('util').format |
|
7
|
|
|
const eraseLine = require('ansi-escapes').eraseLine |
|
8
|
|
|
const slice = Array.prototype.slice |
|
9
|
|
|
|
|
10
|
|
|
var theme = { |
|
11
|
|
|
success: chalk.green, |
|
12
|
|
|
warn: chalk.yellow, |
|
13
|
|
|
yellow: chalk.yellow, |
|
14
|
|
|
err: chalk.red, |
|
15
|
|
|
errEmphasis: chalk.red.underline, |
|
16
|
|
|
info: chalk.underline.white, |
|
17
|
|
|
offset: chalk.italic, |
|
18
|
|
|
notice: chalk.underline |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
var themeChars = { |
|
22
|
|
|
notice: '', |
|
23
|
|
|
log: '•', |
|
24
|
|
|
err: '• ', |
|
25
|
|
|
warn: 'Warning: ', |
|
26
|
|
|
errEmphasis: '', |
|
27
|
|
|
success: '', |
|
28
|
|
|
offset: '', |
|
29
|
|
|
info: '', |
|
30
|
|
|
yellow: '' |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
var activeSpinner = null |
|
34
|
|
|
var backlog = [] |
|
35
|
|
|
|
|
36
|
|
|
function processBacklog () { |
|
37
|
|
|
for (var x in backlog) { |
|
38
|
|
|
var action = backlog[x] |
|
39
|
|
|
exports[action[0]].apply(action[1]) |
|
40
|
|
|
} |
|
41
|
|
|
backlog = [] |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
function log () { |
|
45
|
|
|
var args = slice.call(arguments) |
|
46
|
|
|
var level = args.shift() |
|
47
|
|
|
|
|
48
|
|
|
args[0] = args[0] ? themeChars[level] + args[0] : '' |
|
49
|
|
|
args[0] = args[0] ? theme[level](args[0]) : '' |
|
50
|
|
|
console.log.apply(null, args) |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
function write () { |
|
54
|
|
|
var out = process.stdout |
|
55
|
|
|
var args = slice.call(arguments) |
|
56
|
|
|
var level = args.shift() |
|
57
|
|
|
|
|
58
|
|
|
out.write(theme[level](format.apply(null, [themeChars[level]].concat(args)))) |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
exports.log = function () { |
|
62
|
|
|
if (activeSpinner) { |
|
63
|
|
|
backlog.push(['log', this.arguments]) |
|
64
|
|
|
return |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
console.log.apply(null, [themeChars.log].concat(slice.call(arguments))) |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
exports.write = function (str) { |
|
71
|
|
|
process.stdout.write(str) |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
exports.newline = function () { |
|
75
|
|
|
process.stdout.write('\n') |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
exports.wait = function (msg) { |
|
79
|
|
|
var spinner = ora({ |
|
80
|
|
|
text: chalk.gray(msg), |
|
81
|
|
|
color: 'green' |
|
82
|
|
|
}) |
|
83
|
|
|
|
|
84
|
|
|
activeSpinner = spinner |
|
85
|
|
|
|
|
86
|
|
|
spinner.start() |
|
87
|
|
|
|
|
88
|
|
|
return function (result) { |
|
89
|
|
|
if (result === undefined) { |
|
90
|
|
|
spinner.stop() |
|
91
|
|
|
process.stdout.write(eraseLine) |
|
92
|
|
|
} else if (result === true) { |
|
93
|
|
|
spinner.succeed() |
|
94
|
|
|
} else { |
|
95
|
|
|
spinner.fail(result) |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
activeSpinner = null |
|
99
|
|
|
processBacklog() |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
exports.logComponent = function (component, color, message) { |
|
104
|
|
|
message.split('\n').map(function (string) { |
|
105
|
|
|
if (string.length) { |
|
106
|
|
|
exports.logComponentLine(component, color, string) |
|
107
|
|
|
} |
|
108
|
|
|
}) |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
exports.logComponentLine = function (component, color, message) { |
|
112
|
|
|
var str = chalk.bold(chalk[color](component)) |
|
113
|
|
|
str += ' ' + message |
|
114
|
|
|
str += ' ' + chalk.dim(moment().format('hh:mm:ss')) |
|
115
|
|
|
|
|
116
|
|
|
return this.log(str) |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
exports.notice = exports.info = exports.err = exports.warn = function () { |
|
120
|
|
|
// will be overwritten later, only here for syntax completion |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
Object.keys(theme).forEach(function (level) { |
|
124
|
|
|
exports[level] = function () { |
|
125
|
|
|
log.apply(null, [level].concat(slice.call(arguments))) |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
var c = level[0].toUpperCase() |
|
129
|
|
|
exports['write' + c + level.slice(1)] = function () { |
|
130
|
|
|
write.apply(null, [level].concat(slice.call(arguments))) |
|
131
|
|
|
} |
|
132
|
|
|
}) |
|
133
|
|
|
|