2023-08-23 15:15:25 +08:00
|
|
|
import path from "node:path";
|
2023-12-17 19:34:12 +00:00
|
|
|
import { parseArgs } from "node:util";
|
2025-12-01 07:25:25 +08:00
|
|
|
import { DIST_DIR } from "../utilities/index.js";
|
2023-08-23 15:15:25 +08:00
|
|
|
|
2025-05-27 09:35:31 +08:00
|
|
|
const isUnique = (array) => new Set(array).size === array.length;
|
|
|
|
|
|
2023-08-23 15:15:25 +08:00
|
|
|
function parseArguments() {
|
|
|
|
|
const { values } = parseArgs({
|
|
|
|
|
options: {
|
2025-05-27 09:35:31 +08:00
|
|
|
package: { type: "string", multiple: true },
|
2023-08-23 15:15:25 +08:00
|
|
|
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 },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-03-18 17:21:27 +01:00
|
|
|
if (values.minify && values["no-minify"]) {
|
2023-08-23 15:15:25 +08:00
|
|
|
throw new Error("'--minify' and '--no-minify' can't be used together.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = {
|
2025-12-05 04:53:15 +08:00
|
|
|
files: Array.isArray(values.file)
|
|
|
|
|
? new Set(values.file.map((file) => path.join(DIST_DIR, file)))
|
|
|
|
|
: undefined,
|
2023-08-23 15:15:25 +08:00
|
|
|
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"],
|
2023-08-29 15:50:01 +09:00
|
|
|
reports: values.report,
|
2025-05-27 09:35:31 +08:00
|
|
|
packages: values.package,
|
2023-08-23 15:15:25 +08:00
|
|
|
};
|
|
|
|
|
|
2025-05-27 09:35:31 +08:00
|
|
|
if (Array.isArray(result.packages) && !isUnique(result.packages)) {
|
|
|
|
|
throw new Error("'--package' should be unique.");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-23 15:15:25 +08:00
|
|
|
if (result.saveAs) {
|
|
|
|
|
if (result.files?.size !== 1) {
|
|
|
|
|
throw new Error(
|
2025-05-27 09:35:31 +08:00
|
|
|
"'--save-as' can only use together with one '--file' flag.",
|
2023-08-23 15:15:25 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-09 08:38:46 +08:00
|
|
|
// TODO: Support package name
|
|
|
|
|
const distDirectory = path.join(DIST_DIR, "prettier");
|
|
|
|
|
if (!path.join(distDirectory, result.saveAs).startsWith(distDirectory)) {
|
2025-05-27 09:35:31 +08:00
|
|
|
throw new Error("'--save-as' can only relative path.");
|
2023-08-23 15:15:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.compareSize) {
|
|
|
|
|
if (result.minify === false) {
|
|
|
|
|
throw new Error(
|
2025-05-27 09:35:31 +08:00
|
|
|
"'--compare-size' can not use together with '--no-minify' flag.",
|
2023-08-23 15:15:25 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.saveAs) {
|
|
|
|
|
throw new Error(
|
2025-05-27 09:35:31 +08:00
|
|
|
"'--compare-size' can not use together with '--save-as' flag.",
|
2023-08-23 15:15:25 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 15:50:01 +09:00
|
|
|
if (Array.isArray(result.reports) && result.reports.includes("all")) {
|
|
|
|
|
if (result.reports.length !== 1) {
|
2023-08-23 15:15:25 +08:00
|
|
|
throw new Error(
|
2025-05-27 09:35:31 +08:00
|
|
|
"'--report=all' can not use with another '--report' flag.",
|
2023-08-23 15:15:25 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 15:50:01 +09:00
|
|
|
result.reports = ["html", "text", "stdout"];
|
2023-08-23 15:15:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default parseArguments;
|