UNPKG

1.4 kBJavaScriptView Raw
1/*
2 Copyright © 2018 Andrew Powell
3
4 This Source Code Form is subject to the terms of the Mozilla Public
5 License, v. 2.0. If a copy of the MPL was not distributed with this
6 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8 The above copyright notice and this permission notice shall be
9 included in all copies or substantial portions of this Source Code Form.
10*/
11const chalk = require('chalk');
12const loglevel = require('loglevelnext');
13
14const symbols = { ok: '⬡', whoops: '⬢' };
15const colors = {
16 trace: 'cyan',
17 debug: 'magenta',
18 info: 'blue',
19 warn: 'yellow',
20 error: 'red'
21};
22
23/* istanbul ignore next */
24const forceError = (...args) => {
25 const { error } = console;
26 error(chalk.red(`${symbols.whoops} wps:`), ...args);
27};
28
29const getLogger = (options) => {
30 const prefix = {
31 level: ({ level }) => {
32 const color = colors[level];
33 /* istanbul ignore next */
34 const symbol = ['error', 'warn'].includes(level) ? symbols.whoops : symbols.ok;
35 return chalk[color](`${symbol} wps: `);
36 },
37 template: '{{level}}'
38 };
39
40 /* istanbul ignore if */
41 if (options.timestamp) {
42 prefix.template = `[{{time}}] ${prefix.template}`;
43 }
44
45 /* eslint-disable no-param-reassign */
46 options.prefix = prefix;
47 options.name = 'webpack-plugin-serve';
48
49 const log = loglevel.create(options);
50
51 return log;
52};
53
54module.exports = { forceError, getLogger };