-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Build: Use
node:util
to parse arguments (#15253)
* Build: Use `node:util` to parse arguments * Fix * Update parse-arguments.js
- Loading branch information
Showing
3 changed files
with
83 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { parseArgs } from "node:util"; | ||
import path from "node:path"; | ||
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"], | ||
report: 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.report) && result.report.includes("all")) { | ||
if (result.report.length !== 1) { | ||
throw new Error( | ||
"'--report=all' can not use with another '--report' flag", | ||
); | ||
} | ||
|
||
result.report = ["html", "text", "stdin"]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export default parseArguments; |