UNPKG

1.6 kBJavaScriptView Raw
1const path = require('path')
2const chalk = require('chalk')
3
4class Logger {
5 constructor(options) {
6 this.options = Object.assign(
7 {
8 logLevel: 3
9 },
10 options
11 )
12 }
13
14 setOptions(options) {
15 Object.assign(this.options, options)
16 }
17
18 // level: 4
19 debug(...args) {
20 if (this.options.logLevel < 4) {
21 return
22 }
23
24 this.status('magenta', 'debug', ...args)
25 }
26
27 // level: 2
28 warn(...args) {
29 if (this.options.logLevel < 2) {
30 return
31 }
32 console.warn(chalk.yellow('warning'), ...args)
33 }
34
35 // level: 1
36 error(...args) {
37 if (this.options.logLevel < 1) {
38 return
39 }
40 process.exitCode = process.exitCode || 1
41 console.error(chalk.red('error'), ...args)
42 }
43
44 // level: 3
45 success(...args) {
46 this.status('green', 'success', ...args)
47 }
48
49 // level: 3
50 tip(...args) {
51 this.status('blue', 'tip', ...args)
52 }
53
54 info(...args) {
55 this.status('cyan', 'info', ...args)
56 }
57
58 status(color, label, ...args) {
59 if (this.options.logLevel < 3) {
60 return
61 }
62 console.log(chalk[color](label), ...args)
63 }
64
65 fileAction(color, type, fp) {
66 if (this.options.logLevel < 3) {
67 return
68 }
69 this.info(
70 `${chalk[color](type)} ${chalk.green(path.relative(process.cwd(), fp))}`
71 )
72 }
73
74 fileMoveAction(from, to) {
75 if (this.options.logLevel < 3) {
76 return
77 }
78 this.info(
79 `${chalk.blue('Moved')} ${chalk.green(
80 path.relative(process.cwd(), from)
81 )} ${chalk.dim('->')} ${chalk.green(path.relative(process.cwd(), to))}`
82 )
83 }
84}
85
86module.exports = new Logger()