Files
test-repo/scripts/build/parse-arguments.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.4 KiB
JavaScript
Raw Normal View History

import path from "node:path";
import { parseArgs } from "node:util";
import { DIST_DIR } from "../utils/index.js";
2025-05-27 09:35:31 +08:00
const isUnique = (array) => new Set(array).size === array.length;
function parseArguments() {
const { values } = parseArgs({
options: {
2025-05-27 09:35:31 +08:00
package: { type: "string", multiple: true },
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 },
},
});
if (values.minify && values["no-minify"]) {
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"],
2023-08-29 15:50:01 +09:00
reports: values.report,
2025-05-27 09:35:31 +08:00
packages: values.package,
};
2025-05-27 09:35:31 +08:00
if (Array.isArray(result.packages) && !isUnique(result.packages)) {
throw new Error("'--package' should be unique.");
}
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.",
);
}
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.");
}
}
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.",
);
}
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-29 15:50:01 +09:00
if (Array.isArray(result.reports) && result.reports.includes("all")) {
if (result.reports.length !== 1) {
throw new Error(
2025-05-27 09:35:31 +08:00
"'--report=all' can not use with another '--report' flag.",
);
}
2023-08-29 15:50:01 +09:00
result.reports = ["html", "text", "stdout"];
}
return result;
}
export default parseArguments;