-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (59 loc) · 1.89 KB
/
index.js
File metadata and controls
64 lines (59 loc) · 1.89 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
const path = require('path')
// black console
const getZhLength = (str) => str.match(/\p{Unified_Ideograph}/gu) || ''
const getStyle = (name, text) => {
let objColors = {
green: [32, 39],
magenta: [35, 39],
gray: [90, 39]
},
color = objColors[name]
return `\u001B[${color[0]}m${text}\u001B[${color[1]}m`
}
const getText = (config) => {
let err = new Error(),
strErr = err.stack,
strLineErr = strErr.split(/\r|\n/)[3],
arrErrResult = strLineErr.match(/[^/|:|\\]{1,}/ig),
temp = {}
temp.colNum = +arrErrResult.pop()
temp.lineNum = +arrErrResult.pop()
temp.fileName = '/' + arrErrResult.slice(1).join('/')
temp.fileName = path.relative(config.context, temp.fileName)
if (/^[^\.\/]/.test(temp.fileName)) {
temp.fileName = './' + temp.fileName
}
return /\\|\//ig.test(strLineErr) ? `💡 ${getStyle('magenta', temp.fileName)}${getStyle('gray', ':')}${getStyle('green', temp.lineNum)}` : undefined
}
const _console = console.log
let printConfig = {
context: process.cwd(),
contentLength: 100
}
let prefix = getStyle('gray', '... ')
const print = (...args) => {
// 有对象的情况下, 同一行输出没法精确包装提示信息右对齐
let content = args.map(arg => {
try {
return JSON.stringify(arg)
} catch (error) {
return arg
}
}).join(' ')
let tips = getText(printConfig)
let fixLength = printConfig.contentLength - getZhLength(content).length
let gap = getStyle('gray', content.padEnd(fixLength, ' .').slice(content.length))
_console(prefix, ...args, gap, tips)
}
const println = (...args) => {
_console(getText(printConfig))
_console(prefix, ...args, '\n')
}
const setPrintConfig = (config) => {
printConfig = Object.assign(printConfig, config)
}
module.exports = {
print,
println,
setPrintConfig,
}