Skip to content

Commit 9755f57

Browse files
committed
Polyfill parseArgs for node 14
1 parent ecfd401 commit 9755f57

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
},
3939
"license": "MIT",
4040
"devDependencies": {
41-
"@types/node": "18.7.23",
41+
"@types/node": "18.11.0",
4242
"husky": "8.0.1",
4343
"lint-staged": "13.0.3",
4444
"prettier": "2.7.1",

pnpm-lock.yaml

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { existsSync } from "node:fs";
44
import Path from "node:path";
5-
import { parseArgs } from "node:util";
65

76
import { promiseErrorToSettled } from "./utils.js";
87
import {
@@ -12,6 +11,7 @@ import {
1211
} from "./pnpmWorkspace.js";
1312
import { compare } from "./git.js";
1413
import { debug } from "./debug.js";
14+
import { parseArgs } from "./parseArgs.js";
1515

1616
const cwd = process.cwd();
1717

@@ -28,7 +28,7 @@ const configuration = parseArgs({
2828
},
2929
});
3030

31-
const log = debug(configuration.values.verbose || false);
31+
const log = debug(configuration.values.verbose);
3232

3333
const [gitFromPointer = "HEAD^", gitToPointer = "HEAD"] =
3434
configuration.positionals;

src/parseArgs.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import assert from "node:assert";
2+
3+
// VERY simplified polyfill for parseArgs
4+
type Options = {
5+
type: "boolean";
6+
default: boolean;
7+
};
8+
export const parseArgs = <Keys extends string>({
9+
args,
10+
allowPositionals,
11+
options,
12+
}: {
13+
args: string[];
14+
allowPositionals: true;
15+
options: Record<Keys, Options>;
16+
}) => {
17+
if (!allowPositionals) {
18+
assert(args.every((arg) => arg.startsWith("--")));
19+
}
20+
21+
const positionals = args.filter((arg) => !arg.startsWith("--"));
22+
23+
const defaultNamed = Object.entries(options).map((arg) => {
24+
const [name, options] = arg as [Keys, Options];
25+
return [name, options.default];
26+
});
27+
const named = args
28+
.filter((arg) => arg.startsWith("--"))
29+
.map((arg) => arg.slice(2))
30+
.map((arg) => [arg, true]);
31+
32+
return {
33+
values: Object.fromEntries([...defaultNamed, ...named]) as Record<
34+
Keys,
35+
boolean
36+
>,
37+
positionals,
38+
};
39+
};

0 commit comments

Comments
 (0)