-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
190 lines (162 loc) · 4.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
var destroy = require('demolish')
, kuler = require('kuler');
var toString = Object.prototype.toString;
/**
* Warnings: Output warnings in the terminal when users mess things up.
*
* @constructor
* @param {String} namespace The namespace to prefix warnings.
* @param {Object} options Additional and optional configuration.
* @api public
*/
function Warnings(namespace, options) {
if (!this) return new Warnings(namespace, options);
options = options || {};
this.disabled = [];
this.namespace = namespace;
this.warnings = Object.create(null);
this.stream = options.stream || process.stderr;
this.colors = options.colors || {
prefix: options.prefix || '#EF7D43',
line: options.line || '#FFFFFF'
};
this.atty = 'atty' in options ? options.atty : (this.stream.fd
? require('tty').isatty(this.stream.fd)
: true
);
}
/**
* Read out all the warnings from an local JSON or JS file. This makes it the
* API a bit easier to work with and creates a more maintainable warning
* structure.
*
* @param {String} path The location of a JSON file which contains your warnings.
* @returns {Warnings}
* @api public
*/
Warnings.prototype.read = function read(path) {
var warnings = require(path);
if (Array.isArray(warnings)) {
warnings = warnings.reduce(function reduce(memo, warning) {
memo[warning.name] = warning;
return memo;
}, {});
}
Object.keys(warnings).forEach(function each(warning) {
this.set(warning, warnings[warning]);
}, this);
return this;
};
/**
* Add a new warning.
*
* @param {String} name The name/key of the warning.
* @param {Mixed} spec Details about the warning.
* @returns {Warnings}
* @api public
*/
Warnings.prototype.set = function set(name, spec) {
if ('string' === typeof spec || Array.isArray(spec)) spec = { message: spec };
if (spec.when) spec.conditional = spec.when;
if (!spec.name) spec.name = name;
this.warnings[name] = spec;
return this;
};
/**
* Give out a warning about a certain topic. If the topic has already been
* warned about it will not be executed again.
*
* @param {String} key The key on which the information is stored.
* @param {Mixed} what An optional value that we need to check against.
* @returns {Boolean}
* @api public
*/
Warnings.prototype.about = function about(key, what) {
if (~this.disabled.indexOf(key) || !(key in this.warnings)) return false;
var warning = this.warnings[key]
, passed = false;
//
// Fast case, this warning should always be shown, it doesn't require any
// conditional information.
//
if (!('conditional' in warning)) {
delete this.warnings[key];
return this.write(warning.message);
}
switch (toString.call(warning.conditional).slice(8, -1).toLowerCase()) {
case 'function':
passed = warning.conditional(what);
break;
case 'regexp':
passed = warning.conditional.test(what);
break;
default:
passed = what === warning.conditional;
}
//
// It doesn't match the conditional check, so we shouldn't output this
// warning.
//
if (!passed) return false;
delete this.warnings[key];
return this.write(warning.message);
};
/**
* Write a message to the supplied stream.
*
* @param {String|Array} msg Output a message to the CLI.
* @returns {Boolean}
* @api private
*/
Warnings.prototype.write = function write(msg) {
if (!Array.isArray(msg)) msg = msg.split('\n');
//
// Add some extra white space around the message so it's easier to spot in the
// terminal as these error messages should be considered quite important.
//
msg.push('');
msg.unshift('');
return this.stream.write(msg.map(function map(line) {
var prefix = this.namespace +': ';
//
// Color the prefix orange if the terminal allows colors.
//
if (this.atty) {
prefix = kuler(prefix, this.colors.prefix);
line = kuler(line, this.colors.line);
}
return prefix + line;
}, this).join('\n') + '\n');
};
/**
* Disable certain warnings.
*
* @param {Mixed} names The warnings that should be disabled.
* @returns {Warnings}
* @api public
*/
Warnings.prototype.disable = function disable(names) {
if (Array.isArray(names)) {
Array.prototype.push.apply(this.disabled, names);
} else if (arguments.length > 1) {
Array.prototype.push.apply(this.disabled, arguments);
} else if ('object' === typeof names) {
return this.disable(Object.keys(names));
} else {
this.disabled.push(names);
}
return this;
};
/**
* Destroy the warning instance, nuke all the things.
*
* @type {Function}
* @returns {Boolean}
* @api public
*/
Warnings.prototype.destroy = destroy('disabled, namespace, warnings, stream, colors, atty');
//
// Expose the interface.
//
module.exports = Warnings;