forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse-arguments.js
More file actions
76 lines (64 loc) · 2.05 KB
/
parse-arguments.js
File metadata and controls
76 lines (64 loc) · 2.05 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
import path from "node:path";
import { parseArgs } from "node:util";
import { DIST_DIR } from "../utils/index.js";
function parseArguments() {
const { values } = parseArgs({
options: {
playground: { type: "boolean", default: false },
"print-size": { type: "boolean", default: false },
"compare-size": { type: "boolean", default: false },
minify: { type: "boolean" },
"no-minify": { type: "boolean" },
clean: { type: "boolean", default: false },
file: { type: "string", multiple: true },
"save-as": { type: "string" },
report: { type: "string", multiple: true },
},
strict: true,
});
if (values.minify && values.noMinify) {
throw new Error("'--minify' and '--no-minify' can't be used together.");
}
const result = {
files: Array.isArray(values.file) ? new Set(values.file) : undefined,
playground: values.playground,
printSize: values["print-size"],
compareSize: values["compare-size"],
minify: values.minify ? true : values["no-minify"] ? false : undefined,
clean: values.clean,
saveAs: values["save-as"],
reports: values.report,
};
if (result.saveAs) {
if (result.files?.size !== 1) {
throw new Error(
"'--save-as' can only use together with one '--file' flag",
);
}
if (!path.join(DIST_DIR, result.saveAs).startsWith(DIST_DIR)) {
throw new Error("'--save-as' can only relative path");
}
}
if (result.compareSize) {
if (result.minify === false) {
throw new Error(
"'--compare-size' can not use together with '--no-minify' flag",
);
}
if (result.saveAs) {
throw new Error(
"'--compare-size' can not use together with '--save-as' flag",
);
}
}
if (Array.isArray(result.reports) && result.reports.includes("all")) {
if (result.reports.length !== 1) {
throw new Error(
"'--report=all' can not use with another '--report' flag",
);
}
result.reports = ["html", "text", "stdout"];
}
return result;
}
export default parseArguments;