1 | var chalk = require('chalk');
|
2 | var util = require('util');
|
3 | var _ = require('lodash');
|
4 | var printf = require('printf');
|
5 |
|
6 | var log = module.exports = function(options) {
|
7 | _.defaults(options, {
|
8 | process: '4front',
|
9 | status: 'info',
|
10 | color: 'green'
|
11 | });
|
12 |
|
13 | if (_.isString(options.message))
|
14 | options.message = options.message.split('\n');
|
15 |
|
16 | options.message.forEach(function(line) {
|
17 | module.exports.write(printf("%-8s", options.process));
|
18 |
|
19 |
|
20 | var padding = _.map(_.range(6 - options.status.toString().length),
|
21 | function() {
|
22 | return ' '
|
23 | }).join('');
|
24 |
|
25 | module.exports.write(chalk[options.color](options.status) + padding);
|
26 |
|
27 | if (options.messageColor)
|
28 | module.exports.write(chalk[options.messageColor](line));
|
29 | else
|
30 | module.exports.write(line);
|
31 |
|
32 | writeln('');
|
33 | });
|
34 | };
|
35 |
|
36 | module.exports.debug = function() {
|
37 | if (process.env.DEBUG === '1')
|
38 | log({
|
39 | message: util.format.apply(this, arguments),
|
40 | status: 'debug',
|
41 | color: 'magenta'
|
42 | });
|
43 | };
|
44 |
|
45 | module.exports.error = function() {
|
46 | log({
|
47 | message: util.format.apply(this, arguments),
|
48 | status: 'ERR!',
|
49 | color: 'bgRed',
|
50 | messageColor: 'red'
|
51 | });
|
52 | };
|
53 |
|
54 | module.exports.warn = function() {
|
55 | log({
|
56 | message: util.format.apply(this, arguments),
|
57 | status: 'WARN',
|
58 | color: 'yellow'
|
59 | });
|
60 | }
|
61 |
|
62 | module.exports.info = function() {
|
63 | log({
|
64 | message: util.format.apply(this, arguments),
|
65 | status: 'info',
|
66 | color: 'green'
|
67 | });
|
68 | };
|
69 |
|
70 | module.exports.success = function() {
|
71 | log({
|
72 | message: chalk.green.bold(util.format.apply(this, arguments)),
|
73 | status: 'OK!',
|
74 | color: 'bgGreen'
|
75 | });
|
76 | };
|
77 |
|
78 | module.exports.http = function(statusCode, urlPath) {
|
79 | writeln("4front " + chalk.green(statusCode) + " " + urlPath);
|
80 | };
|
81 |
|
82 | module.exports.blankLine = function() {
|
83 | writeln('');
|
84 | };
|
85 |
|
86 | module.exports.messageBox = function(message, options) {
|
87 | options = _.defaults(options || {}, {
|
88 | width: 60
|
89 | });
|
90 |
|
91 | if (_.isString(message))
|
92 | message = message.split('\n');
|
93 |
|
94 | var top = chalk.yellow('┌' + fill('─', options.width) + '┐');
|
95 | var bottom = chalk.yellow('└' + fill('─', options.width) + '┘');
|
96 | var side = chalk.yellow('│');
|
97 |
|
98 | writeln('\n' + top);
|
99 | message.forEach(function(line) {
|
100 | module.exports.writeln(side + ' ' + line + fill(' ', options.width - line.length -
|
101 | 1) + side);
|
102 | });
|
103 | writeln(bottom);
|
104 | };
|
105 |
|
106 | module.exports.writeln = writeln = function(line) {
|
107 | module.exports.write(line + '\n');
|
108 | };
|
109 |
|
110 | module.exports.write = function(msg) {
|
111 | process.stdout.write(msg);
|
112 | };
|
113 |
|
114 | function fill(str, count) {
|
115 | if (count < 0)
|
116 | return;
|
117 |
|
118 | return Array(count + 1).join(str);
|
119 | }
|