UNPKG

3.2 kBJavaScriptView Raw
1var Logdown = require('./base')()
2var markdown = require('./markdown/node')
3var isColorSupported = require('./util/is-color-supported/node')
4var chalk = require('chalk')
5
6//
7// Static
8//
9
10Logdown.methodEmoji = {
11 warn: '⚠️',
12 error: '❌',
13 info: `\u{2139}\u{FE0F}`, // Forces emoji information instead of "i" symbol
14 debug: '🐞',
15 log: ' '
16}
17
18Logdown.prefixColors = [
19 'red',
20 'green',
21 'yellow',
22 'blue',
23 'magenta',
24 'cyan'
25]
26
27Logdown._setPrefixRegExps = function () {
28 // Parsing `NODE_DEBUG` and `DEBUG` env var
29 var envVar = null
30 Logdown._prefixRegExps = []
31
32 if (
33 typeof process !== 'undefined' &&
34 process.env !== undefined
35 ) {
36 // `NODE_DEBUG` has precedence over `DEBUG`
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
72Logdown._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// Instance
83//
84
85Logdown.prototype._getDecoratedPrefix = function (method) {
86 var decoratedPrefix
87
88 if (isColorSupported()) {
89 // If is a hex color value
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
114Logdown.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// API
136//
137
138Logdown._setPrefixRegExps()
139
140module.exports = Logdown