Skip to content

Commit

Permalink
Build: Use node:util to parse arguments (#15253)
Browse files Browse the repository at this point in the history
* Build: Use `node:util` to parse arguments

* Fix

* Update parse-arguments.js
  • Loading branch information
fisker committed Aug 23, 2023
1 parent f75f717 commit 999c2c2
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 66 deletions.
2 changes: 1 addition & 1 deletion scripts/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Available reporter formats:
- `stdout` Log report information in console.

```sh
yarn build --report
yarn build --report=all
yarn build --report=stdout --report=text --report=html
```

Expand Down
72 changes: 7 additions & 65 deletions scripts/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import path from "node:path";
import fs from "node:fs/promises";
import readline from "node:readline";
import chalk from "chalk";
import minimist from "minimist";
import prettyBytes from "pretty-bytes";
import createEsmUtils from "esm-utils";
import { DIST_DIR } from "../utils/index.js";
import files from "./config.js";
import parseArguments from "./parse-arguments.js";

const { require } = createEsmUtils(import.meta);

Expand Down Expand Up @@ -129,39 +129,8 @@ async function buildFile({ file, files, shouldCollectLicenses, cliOptions }) {
console.log(status.DONE);
}

async function run(cliOptions) {
cliOptions.files = cliOptions.file
? new Set([cliOptions.file].flat())
: cliOptions.file;
delete cliOptions.file;

cliOptions.saveAs = cliOptions["save-as"];
delete cliOptions["save-as"];

cliOptions.printSize = cliOptions["print-size"];
delete cliOptions["print-size"];

cliOptions.compareSize = cliOptions["compare-size"];
delete cliOptions["compare-size"];

if (cliOptions.report === "") {
cliOptions.report = ["html"];
}
cliOptions.reports = cliOptions.report
? [cliOptions.report].flat()
: cliOptions.report;
delete cliOptions.report;

if (cliOptions.saveAs && !(cliOptions.files && cliOptions.files.size === 1)) {
throw new Error("'--save-as' can only use together with one '--file' flag");
}

if (
cliOptions.saveAs &&
!path.join(DIST_DIR, cliOptions.saveAs).startsWith(DIST_DIR)
) {
throw new Error("'--save-as' can only relative path");
}
async function run() {
const cliOptions = parseArguments();

if (cliOptions.clean) {
let stat;
Expand All @@ -180,22 +149,10 @@ async function run(cliOptions) {
}
}

if (cliOptions.compareSize) {
if (cliOptions.minify === false) {
throw new Error(
"'--compare-size' can not use together with '--no-minify' flag",
);
}

if (cliOptions.saveAs) {
throw new Error(
"'--compare-size' can not use together with '--save-as' flag",
);
}
}

const shouldCollectLicenses =
!cliOptions.playground && !cliOptions.files && cliOptions.minify === null;
!cliOptions.playground &&
!cliOptions.files &&
typeof cliOptions.minify !== "boolean";

console.log(chalk.inverse(" Building packages "));

Expand All @@ -204,19 +161,4 @@ async function run(cliOptions) {
}
}

await run(
minimist(process.argv.slice(2), {
boolean: ["playground", "print-size", "compare-size", "minify", "clean"],
string: ["file", "save-as", "report"],
default: {
clean: false,
playground: false,
printSize: false,
compareSize: false,
minify: null,
},
unknown(flag) {
throw new Error(`Unknown flag ${chalk.red(flag)}`);
},
}),
);
await run();
75 changes: 75 additions & 0 deletions scripts/build/parse-arguments.js
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;

0 comments on commit 999c2c2

Please sign in to comment.