index.js
3.5 KB
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
"use strict";
var chalk = require("chalk");
var parser = require("./lib/parser");
var objectPath = require("object-path");
/**
* Stateless compiler.
* @param {String} string
* @param {Object} [custom] - Any custom methods
* @param {Object} [opts] - Options
* @returns {String}
*/
function compile(string, custom, opts) {
opts = opts || {};
return parseAst(createAst(parser, string), custom, function (err) {
if (err) {
if (opts.logErrors) {
console.log(err.msg);
}
if (opts.failOnError) {
throw Error(err.msg);
}
}
});
}
/**
* @param parser
* @param string
* @returns {*}
*/
function createAst(parser, string) {
return parser.parse(string);
}
/**
* @param ast
* @param custom
* @param {Function} cb
*/
function parseAst(ast, custom, cb) {
var colors = [];
return ast.reduce(function (joined, item) {
var fn;
if (item.color) {
if (item.text) {
if (fn = resolveFun(item.color, custom)) {
colors.push(fn);
return joined + fn(item.text);
} else {
cb({
msg: "Method does not exist: " + item.color
});
return joined + item.text;
}
}
}
if (item.buffer) {
return colors.length
? joined + colors[colors.length-1](item.buffer)
: joined + item.buffer;
}
if (item.reset) {
colors.pop();
if (item.text) {
return colors.length
? joined + colors[colors.length-1](item.text)
: joined + item.text;
}
}
return joined;
}, "");
}
/**
* @param path
* @param custom
* @returns {*}
*/
function resolveFun(path, custom) {
var fn;
if (fn = getFun(custom, path)) {
return fn.bind({compile:compile});
}
return getFun(chalk, path);
}
/**
* Get a function from an object
*/
function getFun(obj, path) {
if (!obj) {
return false;
}
return objectPath.get(obj, path);
}
/**
* @param {Object} [opts]
* @param {Object} custom
* @returns {Compiler}
*/
function Compiler(custom, opts) {
opts = opts || {};
custom = custom || {};
this.prefix = "";
if (typeof opts.prefix === "string") {
this.prefix = compile(opts.prefix, custom, opts);
}
if (typeof opts.prefix === "function") {
this.prefix = opts.prefix;
}
this.compile = function (string, noPrefix) {
var out = "";
if (!noPrefix) {
if (typeof this.prefix === "function") {
out = this.prefix.apply({compile: compile}, [string, opts]);
} else {
out = this.prefix;
}
}
return out + compile(string, custom, opts);
};
return this;
}
module.exports = compile;
module.exports.parse = function (string) {
return createAst(parser, string);
};
module.exports.clean = function (string) {
var ast = createAst(parser, string);
return ast.reduce(function (joined, item) {
if (item.color) {
if (item.text) {
return joined + item.text;
}
}
if (item.buffer) {
return joined + item.buffer;
}
if (item.reset) {
return joined + item.text;
}
return joined;
}, "");
};
module.exports.Compiler = Compiler;