-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·261 lines (217 loc) · 6.75 KB
/
cli.js
File metadata and controls
executable file
·261 lines (217 loc) · 6.75 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
"use strict";
const path = require("path");
const fs = require("fs");
const parseArgs = () => {
/*
* 0 - node
* 1 - this file name
* 2 - command
* 3... - arguments
*/
const [interpreter, executable, command] = process.argv;
const args = {};
for (let index = 3; index < process.argv.length; ++index) {
// all arguments have same shape, always have name and value
const parsed = process.argv[index].match(/^--([^=]+)=(.*)$/);
// misshaped argument breaks command line reading
if (!parsed) {
break;
}
const [, name, value] = parsed;
if (args.hasOwnProperty(name)) {
args[name].push(value);
} else {
args[name] = [value];
}
}
return { command, args };
};
const { command, args } = parseArgs();
const getFullDirPaths = (dirs) =>
dirs.map((target) => {
const fullPath = path.resolve(process.cwd(), String(target));
if (!fullPath) {
exitWithError(`Path "${target}" could not be resolved.`);
}
if (!fs.existsSync(fullPath)) {
exitWithError(`Directory "${fullPath}" does not exist.`);
}
return fullPath;
});
// validate targets, must be at least one, all must be dirs
const validateTargetDirs = () => {
if (!targetDirs.length) {
exitWithError(
"No target directories were provided, please use --target-dir argument to specify at least one target directory."
);
}
};
const exitWithError = (message) => {
console.error(message);
process.exit(1);
};
let targetDirs = args["target-dir"] || [];
targetDirs = getFullDirPaths(targetDirs);
// TM char codes 84 and 77
const DEFAULT_PORT = 8477;
const theme = String(args.theme || 'light').trim();
let feaureTableType = "default";
if (String(args.compact) === "true") {
feaureTableType = "compact";
}
switch (command) {
case "serve":
{
/**
* serve --target-dir= --port= --key= --cert= --compact=true --theme=light|dark|hc-light|hc-dark
*/
validateTargetDirs();
const port = args.port ? parseInt(String(args.port), 10) : DEFAULT_PORT;
let keyFilePath = (args.key || [])[0];
let certFilePath = (args.cert || [])[0];
let useHttps = false;
if (Number.isNaN(port)) {
exitWithError(`Port value "${args.port}" results to NaN.`);
}
if (keyFilePath || certFilePath) {
if (!keyFilePath || !certFilePath) {
exitWithError(
`Parameters "--key" and "--cert" are both required for HTTPS server and must point to corresponding files.`
);
}
keyFilePath = path.resolve(process.cwd(), keyFilePath);
if (
!keyFilePath ||
!fs.existsSync(keyFilePath) ||
!fs.statSync(keyFilePath).isFile()
) {
exitWithError(
`Parameter "--key" must point at file with private key for certificate.`
);
}
certFilePath = path.resolve(process.cwd(), certFilePath);
if (
!certFilePath ||
!fs.existsSync(certFilePath) ||
!fs.statSync(certFilePath).isFile()
) {
exitWithError(
`Parameter "--cert" must point at file with signed certificate.`
);
}
useHttps = true;
}
const { serve } = require("./commands/serve.js");
serve(targetDirs, port, keyFilePath, certFilePath, feaureTableType, theme).then(
() => {
import("open").then(({ default: open }) =>
open(
useHttps
? `https://localhost:${port}`
: `http://localhost:${port}`
)
);
}
);
}
break;
case "generate":
{
/**
* generate --target-dir= --output-dir= --compact=true --force-cleanup=true --theme=light|dark|hc-light|hc-dark
*/
validateTargetDirs();
const outputDir = path.resolve(process.cwd(), String(args["output-dir"]));
if (fs.existsSync(outputDir)) {
if (!fs.statSync(outputDir).isDirectory()) {
exitWithError(`"${outputDir}" exists and is not a directory.`);
}
if (args["force-cleanup"]) {
fs.rmSync(outputDir, { recursive: true, force: true });
}
} else {
fs.mkdirSync(outputDir);
}
const { generateStatic } = require("./commands/generate-static.js");
generateStatic(targetDirs, outputDir, feaureTableType, theme);
}
break;
case "lcov":
{
/**
* lcov --target-dir= --output-dir= --relative-dir= --force-cleanup=true --format=md/js
*/
validateTargetDirs();
const outputDir = path.resolve(process.cwd(), String(args["output-dir"]));
if (fs.existsSync(outputDir)) {
if (!fs.statSync(outputDir).isDirectory()) {
exitWithError(`"${outputDir}" exists and is not a directory.`);
}
if (args["force-cleanup"]) {
fs.rmSync(join(outputDir, "lcov"), { recursive: true, force: true });
}
} else {
fs.mkdirSync(outputDir);
}
const format = args["format"]?.toLowerCase() || "js";
const relativeDir = args["relative-dir"]
? String(args["relative-dir"])
: undefined;
const { lcov } = require("./commands/lcov.js");
lcov(targetDirs, outputDir, relativeDir, format);
}
break;
case "threshold":
{
/**
* threshold --target-dir= --total=80 --per-feature=40
*/
validateTargetDirs();
const total =
args["total"] === undefined ? 100 : parseInt(args["total"], 10);
const perFeature =
args["per-feature"] === undefined
? 100
: parseInt(args["per-feature"], 10);
if (isNaN(total) || isNaN(perFeature)) {
exitWithError(
"Coverage thresholds should be positive integer values between 0 and 100."
);
}
const { threshold } = require("./commands/threshold.js");
threshold(targetDirs, total, perFeature);
}
break;
case "stats":
{
/**
* stats --target-dir= --feature=
*/
validateTargetDirs();
const features = args["feature"] || [];
const { stats } = require("./commands/stats.js");
stats(targetDirs, features);
}
break;
case "scan":
{
/**
* Scan features dirs for any feature descriptions and store in target dir where all coverage reports.
* scan --features-dir= --target-dir=
*/
validateTargetDirs();
const featureDirs = getFullDirPaths(args["features-dir"] || []);
const { scanFeatures } = require("./commands/scan_features.js");
scanFeatures(featureDirs, targetDirs[0]);
}
break;
case "help":
/**
* help --command=
*/
const command = args["command"]?.[0] || "";
const { help } = require("./commands/help.js");
help(command);
break;
}