-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.js
102 lines (94 loc) · 2.28 KB
/
utils.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
const child_process = require("child_process");
const path = require("path");
const tagsMap = {
b: [1, 22],
i: [3, 23],
u: [4, 24],
h: [7, 27],
};
const name = require("./package.json").name;
module.exports.name = name;
function log(msg, { prefix } = { prefix: "₪" }) {
if (prefix) {
msg = msg.replace(
/\S/,
(firstChar) =>
` \x1b[${tagsMap.h[0]}m ${prefix} \x1b[${tagsMap.h[1]}m ${firstChar}`
);
}
msg = msg.replace(/<(\/)?(.*?)>/gi, (src, isClosing, tag) => {
return tagsMap[tag]
? `\x1b[${isClosing ? tagsMap[tag][1] : tagsMap[tag][0]}m`
: src;
});
console.log(msg);
}
module.exports.log = log;
module.exports.onCompile = (msg) => {
msg
.trim()
.split("\n")
.forEach((line) => {
if (line.trim()) {
log(line);
} else {
log(line, { prefix: false });
}
});
};
module.exports.spawn = function spawn(
cliName,
{ args = {}, options = {}, async = false, onMsg }
) {
const child = (async ? child_process.spawn : child_process.spawnSync)(
path.resolve(__dirname, "node_modules", ".bin", cliName),
args,
{
stdio: process.env.DEBUG
? "inherit"
: ["inherit", onMsg ? "pipe" : "inherit", "inherit"],
...options,
}
);
if (onMsg && !process.env.DEBUG) {
if (async) {
child.stdout.on("data", onMsg);
}
onMsg(child.stdout.toString());
}
return child;
};
module.exports.help = function help() {
log(`Usage: ${name} <command> [options]\n`);
log(`Commands:\n`);
log(
` <b>dev</b> Compiles HTML files on change and serves the root folder`,
{
prefix: false,
}
);
log(` <b>build</b> Compiles the HTML files once`, { prefix: false });
log(` <b>help</b> Displays help`, { prefix: false });
log(`\nOptions:\n`);
log(` <b>--root</b> Folder to compile (default ./)`, {
prefix: false,
});
log(
` <b>--output</b> Output (destination) folder. This is necessary only when using ${name} build`,
{
prefix: false,
}
);
log(
` <b>--htmlOnly</b> Compiles and copies only the built HTML files`,
{
prefix: false,
}
);
log(
` <b>--port</b> Port used for the dev server (default: 5000)\n`,
{
prefix: false,
}
);
};