util.js
1.83 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
"use strict";
var callerPath,chalk,eol,fs,path,_splitargs;
var colorsEnabled;
function forceColors(forced)
{
lazyRequire("chalk");
if (colorsEnabled === undefined)
{
// Not solidly reliable, as it's global and any library could have modified it
// prior to this file accessing it, but it's probable that nothing else will
colorsEnabled = chalk.enabled;
}
chalk.enabled = (forced || forced===undefined) ? true : colorsEnabled;
}
function lazyRequire(dep)
{
switch (dep)
{
case "caller-path":
{
if (!callerPath) callerPath = require("caller-path");
break;
}
case "chalk":
{
if (!chalk) chalk = require("chalk");
break;
}
case "eol":
{
if (!eol) eol = require("eol");
break;
}
case "fs":
{
if (!fs) fs = require("fs");
break;
}
case "path":
{
if (!path) path = require("path");
break;
}
case "splitargs":
{
if (!_splitargs) _splitargs = require("splitargs");
break;
}
}
}
function readHelpFile(filepath)
{
lazyRequire("caller-path");
lazyRequire("eol");
lazyRequire("fs");
lazyRequire("path");
var str;
filepath = path.resolve( path.dirname(callerPath()), filepath );
str = fs.readFileSync(filepath, {encoding:"utf8"});
str = eol.lf(str);
return str;
}
function replaceColorVars(str)
{
lazyRequire("chalk");
return str.replace(/({{(\/?([^}]+))}})/g, function(match, p1, p2, p3, offset, string)
{
if (p3)
{
var closer = p2.indexOf("/") == 0;
return chalk.styles[p3][closer ? "close" : "open"];
}
});
}
function splitargs(str)
{
lazyRequire("splitargs");
return _splitargs(str);
}
function stripColors(str)
{
lazyRequire("chalk");
return chalk.stripColor(str);
}
module.exports =
{
forceColors: forceColors,
readHelpFile: readHelpFile,
replaceColorVars: replaceColorVars,
splitargs: splitargs,
stripColors: stripColors
};