1 | var Logdown = require('./base')()
|
2 | var markdown = require('./markdown/node')
|
3 | var isColorSupported = require('./util/is-color-supported/node')
|
4 | var chalk = require('chalk')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Logdown.methodEmoji = {
|
11 | warn: '⚠️',
|
12 | error: '❌',
|
13 | info: `\u{2139}\u{FE0F}`,
|
14 | debug: '🐞',
|
15 | log: ' '
|
16 | }
|
17 |
|
18 | Logdown.prefixColors = [
|
19 | 'red',
|
20 | 'green',
|
21 | 'yellow',
|
22 | 'blue',
|
23 | 'magenta',
|
24 | 'cyan'
|
25 | ]
|
26 |
|
27 | Logdown._setPrefixRegExps = function () {
|
28 |
|
29 | var envVar = null
|
30 | Logdown._prefixRegExps = []
|
31 |
|
32 | if (
|
33 | typeof process !== 'undefined' &&
|
34 | process.env !== undefined
|
35 | ) {
|
36 |
|
37 | if (
|
38 | process.env['NODE_DEBUG'] !== undefined &&
|
39 | process.env['NODE_DEBUG'] !== ''
|
40 | ) {
|
41 | envVar = 'NODE_DEBUG'
|
42 | } else if (
|
43 | process.env['DEBUG'] !== undefined &&
|
44 | process.env['DEBUG'] !== ''
|
45 | ) {
|
46 | envVar = 'DEBUG'
|
47 | }
|
48 |
|
49 | if (envVar) {
|
50 | process.env[envVar]
|
51 | .split(',')
|
52 | .forEach(function (str) {
|
53 | str = str.trim()
|
54 | var type = 'enable'
|
55 |
|
56 | if (str[0] === '-') {
|
57 | str = str.substr(1)
|
58 | type = 'disable'
|
59 | }
|
60 |
|
61 | var regExp = Logdown._prepareRegExpForPrefixSearch(str)
|
62 |
|
63 | Logdown._prefixRegExps.push({
|
64 | type: type,
|
65 | regExp: regExp
|
66 | })
|
67 | })
|
68 | }
|
69 | }
|
70 | }
|
71 |
|
72 | Logdown._getNextPrefixColor = (function () {
|
73 | var lastUsed = 0
|
74 |
|
75 | return function () {
|
76 | lastUsed += 1
|
77 | return Logdown.prefixColors[lastUsed % Logdown.prefixColors.length]
|
78 | }
|
79 | })()
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | Logdown.prototype._getDecoratedPrefix = function (method) {
|
86 | var decoratedPrefix
|
87 |
|
88 | if (isColorSupported()) {
|
89 |
|
90 | if (this.opts.prefixColor[0] === '#') {
|
91 | decoratedPrefix = chalk.bold.hex(this.opts.prefixColor)(this.opts.prefix)
|
92 | } else {
|
93 | decoratedPrefix = chalk.bold[this.opts.prefixColor](this.opts.prefix)
|
94 | }
|
95 | } else {
|
96 | decoratedPrefix = '[' + this.opts.prefix + ']'
|
97 | }
|
98 |
|
99 | if (method === 'warn') {
|
100 | decoratedPrefix = chalk.yellow(Logdown.methodEmoji.warn) + ' ' + decoratedPrefix
|
101 | } else if (method === 'error') {
|
102 | decoratedPrefix = chalk.red(Logdown.methodEmoji.error) + ' ' + decoratedPrefix
|
103 | } else if (method === 'info') {
|
104 | decoratedPrefix = chalk.blue(Logdown.methodEmoji.info) + ' ' + decoratedPrefix
|
105 | } else if (method === 'debug') {
|
106 | decoratedPrefix = chalk.gray(Logdown.methodEmoji.debug) + ' ' + decoratedPrefix
|
107 | } else if (method === 'log') {
|
108 | decoratedPrefix = chalk.white(Logdown.methodEmoji.log) + ' ' + decoratedPrefix
|
109 | }
|
110 |
|
111 | return decoratedPrefix
|
112 | }
|
113 |
|
114 | Logdown.prototype._prepareOutput = function (args, method) {
|
115 | var preparedOutput = []
|
116 |
|
117 | preparedOutput[0] = this._getDecoratedPrefix(method)
|
118 |
|
119 | args.forEach(function (arg) {
|
120 | if (typeof arg === 'string') {
|
121 | if (this.opts.markdown) {
|
122 | preparedOutput.push(markdown.parse(arg).text)
|
123 | } else {
|
124 | preparedOutput.push(arg)
|
125 | }
|
126 | } else {
|
127 | preparedOutput.push(arg)
|
128 | }
|
129 | }, this)
|
130 |
|
131 | return preparedOutput
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | Logdown._setPrefixRegExps()
|
139 |
|
140 | module.exports = Logdown
|