-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
59 lines (42 loc) · 1.26 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
var chalk = require("chalk");
var _ = require("lodash");
var templateSettings = { interpolate: /{:([\s\S]+?):}/g };
var compile = function (template, params, prefix) {
/**
* @param string
* @returns {String}
*/
var modifier = function (string) {
var split = /{(.+?[^:]):(.+?)(?:})/.exec(string);
var color = split[1];
var content = split[2];
return chalk[color](content);
};
if (params) {
template = _.template(template, params, templateSettings);
}
if (prefix) {
template = [prefix, template].join(" ");
}
return template.replace(/({.+?[^:]:)(.+?)(?:})/g, modifier);
};
module.exports.compile = compile;
/**
* Alternative way to retrieve the compiler, this time with a prefix
* @param {String} prefix
* @returns {Function}
*/
module.exports.getCompiler = function (prefix) {
return function (template, params) {
var output = [];
if (Array.isArray(template)) {
template.forEach(function (item) {
output.push(compile(item, params, prefix));
});
return output.join("\n");
}
return compile(template, params, prefix);
};
};
module.exports.clc = chalk;
module.exports.chalk = chalk;