Skip to content

Commit f597897

Browse files
committed
refactor: more strict types
1 parent b90a84a commit f597897

File tree

19 files changed

+103
-92
lines changed

19 files changed

+103
-92
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"lint": "eslint --cache . && prettier -c src test",
2020
"lint:fix": "eslint --cache . --fix && prettier -c src test -w",
2121
"prepack": "pnpm run build",
22-
"play": "jiti ./playground/cli.ts",
22+
"play": "node ./playground/cli.ts",
2323
"release": "pnpm test && pnpm build && changelogen --release --push && npm publish",
2424
"test": "pnpm lint && pnpm test:types && vitest run --coverage",
2525
"test:types": "tsc --noEmit"
@@ -34,7 +34,6 @@
3434
"changelogen": "^0.6.2",
3535
"eslint": "^9.39.2",
3636
"eslint-config-unjs": "^0.6.2",
37-
"jiti": "^2.6.1",
3837
"obuild": "^0.4.18",
3938
"prettier": "^3.8.0",
4039
"scule": "^1.3.0",

playground/cli.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineCommand, runMain } from "../src";
1+
import { defineCommand, runMain } from "../src/index.ts";
22

33
const main = defineCommand({
44
meta: {
@@ -13,9 +13,9 @@ const main = defineCommand({
1313
console.log("Cleanup");
1414
},
1515
subCommands: {
16-
build: () => import("./commands/build").then((r) => r.default),
17-
deploy: () => import("./commands/deploy").then((r) => r.default),
18-
debug: () => import("./commands/debug").then((r) => r.default),
16+
build: () => import("./commands/build.ts").then((r) => r.default),
17+
deploy: () => import("./commands/deploy.ts").then((r) => r.default),
18+
debug: () => import("./commands/debug.ts").then((r) => r.default),
1919
},
2020
});
2121

playground/commands/build.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import consola from "consola";
2-
import { defineCommand } from "../../src";
2+
import { defineCommand } from "../../src/index.ts";
33

44
export default defineCommand({
55
meta: {

playground/commands/debug.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import consola from "consola";
2-
import { defineCommand } from "../../src";
2+
import { defineCommand } from "../../src/index.ts";
33

44
export default defineCommand({
55
meta: {
66
name: "debug",
77
description: "Debug the project",
8-
hidden: true
8+
hidden: true,
99
},
1010
args: {
1111
verbose: {

playground/commands/deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import consola from "consola";
2-
import { defineCommand } from "../../src";
2+
import { defineCommand } from "../../src/index.ts";
33

44
export default defineCommand({
55
meta: {

playground/hello.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import consola from "consola";
2-
import { defineCommand, createMain } from "../src";
2+
import { defineCommand, createMain } from "../src/index.ts";
33

44
const command = defineCommand({
55
meta: {

src/_parser.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ function toArr(any: any) {
2121
return any == undefined ? [] : Array.isArray(any) ? any : [any];
2222
}
2323

24-
function toVal(out, key, val, opts) {
24+
function toVal(out: any, key: string, val: any, opts: Options) {
2525
let x;
2626
const old = out[key];
27-
const nxt = ~opts.string.indexOf(key)
27+
const nxt = ~opts.string!.indexOf(key)
2828
? val == undefined || val === true
2929
? ""
3030
: String(val)
3131
: typeof val === "boolean"
3232
? val
33-
: ~opts.boolean.indexOf(key)
33+
: ~opts.boolean!.indexOf(key)
3434
? val === "false"
3535
? false
3636
: val === "true" ||
@@ -51,7 +51,7 @@ export function parseRawArgs<T = Default>(
5151
let arg;
5252
let name;
5353
let val;
54-
const out = { _: [] };
54+
const out = { _: [] as string[] } as Argv<T>;
5555
let i = 0;
5656
let j = 0;
5757
let idx = 0;
@@ -75,27 +75,27 @@ export function parseRawArgs<T = Default>(
7575
}
7676

7777
for (i = opts.boolean.length; i-- > 0; ) {
78-
arr = opts.alias[opts.boolean[i]] || [];
78+
arr = opts.alias[opts.boolean[i]!] || [];
7979
for (j = arr.length; j-- > 0; ) {
80-
opts.boolean.push(arr[j]);
80+
opts.boolean.push(arr[j]!);
8181
}
8282
}
8383

8484
for (i = opts.string.length; i-- > 0; ) {
85-
arr = opts.alias[opts.string[i]] || [];
85+
arr = opts.alias[opts.string[i]!] || [];
8686
for (j = arr.length; j-- > 0; ) {
87-
opts.string.push(arr[j]);
87+
opts.string.push(arr[j] as string);
8888
}
8989
}
9090

9191
if (defaults) {
9292
for (k in opts.default) {
93-
name = typeof opts.default[k];
93+
name = typeof (opts as any).default[k];
9494
arr = opts.alias[k] = opts.alias[k] || [];
95-
if (opts[name] !== void 0) {
96-
opts[name].push(k);
95+
if ((opts as any)[name] !== void 0) {
96+
(opts as any)[name].push(k);
9797
for (i = 0; i < arr.length; i++) {
98-
opts[name].push(arr[i]);
98+
(opts as any)[name].push(arr[i]);
9999
}
100100
}
101101
}
@@ -104,7 +104,7 @@ export function parseRawArgs<T = Default>(
104104
const keys = strict ? Object.keys(opts.alias) : [];
105105

106106
for (i = 0; i < len; i++) {
107-
arg = args[i];
107+
arg = args[i]!;
108108

109109
if (arg === "--") {
110110
out._ = out._.concat(args.slice(++i));
@@ -122,9 +122,9 @@ export function parseRawArgs<T = Default>(
122122
} else if (arg.substring(j, j + 3) === "no-") {
123123
name = arg.slice(Math.max(0, j + 3));
124124
if (strict && !~keys.indexOf(name)) {
125-
return opts.unknown(arg);
125+
return opts.unknown!(arg)!;
126126
}
127-
out[name] = false;
127+
(out as any)[name] = false;
128128
} else {
129129
for (idx = j + 1; idx < arg.length; idx++) {
130130
if (arg.charCodeAt(idx) === 61) {
@@ -142,18 +142,18 @@ export function parseRawArgs<T = Default>(
142142

143143
for (idx = 0; idx < arr.length; idx++) {
144144
name = arr[idx];
145-
if (strict && !~keys.indexOf(name)) {
146-
return opts.unknown("-".repeat(j) + name);
145+
if (strict && !~keys.indexOf(name!)) {
146+
return opts.unknown!("-".repeat(j) + name)!;
147147
}
148-
toVal(out, name, idx + 1 < arr.length || val, opts);
148+
toVal(out, name!, idx + 1 < arr.length || val, opts);
149149
}
150150
}
151151
}
152152

153153
if (defaults) {
154154
for (k in opts.default) {
155-
if (out[k] === void 0) {
156-
out[k] = opts.default[k];
155+
if ((out as any)[k] === void 0) {
156+
(out as any)[k] = opts.default![k];
157157
}
158158
}
159159
}
@@ -162,7 +162,7 @@ export function parseRawArgs<T = Default>(
162162
for (k in out) {
163163
arr = opts.alias[k] || [];
164164
while (arr.length > 0) {
165-
out[arr.shift()] = out[k];
165+
(out as any)[(arr as string[]).shift()!] = (out as any)[k];
166166
}
167167
}
168168
}

src/_utils.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Resolvable } from "./types";
1+
import type { Resolvable } from "./types.ts";
22

33
export function toArray(val: any) {
44
if (Array.isArray(val)) {
@@ -19,7 +19,7 @@ export function formatLineColumns(lines: string[][], linePrefix = "") {
1919
l
2020
.map(
2121
(c, i) =>
22-
linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLength[i]),
22+
linePrefix + c[i === 0 ? "padStart" : "padEnd"](maxLength[i]!),
2323
)
2424
.join(" "),
2525
)
@@ -31,11 +31,10 @@ export function resolveValue<T>(input: Resolvable<T>): T | Promise<T> {
3131
}
3232

3333
export class CLIError extends Error {
34-
constructor(
35-
message: string,
36-
public code?: string,
37-
) {
34+
code: string | undefined;
35+
constructor(message: string, code?: string) {
3836
super(message);
3937
this.name = "CLIError";
38+
this.code = code;
4039
}
4140
}

src/args.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { kebabCase, camelCase } from "scule";
2-
import { parseRawArgs } from "./_parser";
3-
import type { Arg, ArgsDef, ParsedArgs } from "./types";
4-
import { CLIError, toArray } from "./_utils";
2+
import { parseRawArgs } from "./_parser.ts";
3+
import type { Arg, ArgsDef, ParsedArgs } from "./types.ts";
4+
import { CLIError, toArray } from "./_utils.ts";
55

66
export function parseArgs<T extends ArgsDef = ArgsDef>(
77
rawArgs: string[],

src/command.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { CommandContext, CommandDef, ArgsDef } from "./types";
2-
import { CLIError, resolveValue } from "./_utils";
3-
import { parseArgs } from "./args";
1+
import type { CommandContext, CommandDef, ArgsDef } from "./types.ts";
2+
import { CLIError, resolveValue } from "./_utils.ts";
3+
import { parseArgs } from "./args.ts";
44

55
export function defineCommand<const T extends ArgsDef = ArgsDef>(
66
def: CommandDef<T>,
@@ -80,7 +80,7 @@ export async function resolveSubCommand<T extends ArgsDef = ArgsDef>(
8080
const subCommands = await resolveValue(cmd.subCommands);
8181
if (subCommands && Object.keys(subCommands).length > 0) {
8282
const subCommandArgIndex = rawArgs.findIndex((arg) => !arg.startsWith("-"));
83-
const subCommandName = rawArgs[subCommandArgIndex];
83+
const subCommandName = rawArgs[subCommandArgIndex]!;
8484
const subCommand = await resolveValue(subCommands[subCommandName]);
8585
if (subCommand) {
8686
return resolveSubCommand(

0 commit comments

Comments
 (0)