Skip to content

Commit 14e700b

Browse files
committed
Typeguards for style points
1 parent 50ebe24 commit 14e700b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/bin/cli.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ import * as utils from "../utils";
1818
import { enableExperimentsFromCliEnvVariable } from "../experiments";
1919
import { fetchMOTD } from "../fetchMOTD";
2020

21+
interface Command {
22+
load: () => void;
23+
}
24+
25+
function isCommand(value: unknown): value is Command {
26+
return typeof value === "function" && typeof (value as any).load === "function";
27+
}
28+
2129
export function cli(pkg: any) {
2230
const updateNotifier = updateNotifierPkg({ pkg });
2331

@@ -108,15 +116,16 @@ export function cli(pkg: any) {
108116
});
109117

110118
if (!handlePreviewToggles(args)) {
111-
// determine if there are any arguments. if not, display help
112-
if (!args.length || args[0] === "help") {
119+
// If this is a help command, load all commands so we can display them.
120+
const isHelp = !args.length || args[0] === "help";
121+
if (isHelp) {
113122
const seen = new Set();
114123
const loadAll = (obj: any) => {
115124
if (seen.has(obj)) return;
116125
seen.add(obj);
117126
for (const [key, value] of Object.entries(obj)) {
118-
if (typeof value === "function" && (value as any).load) {
119-
(value as any).load();
127+
if (isCommand(value)) {
128+
value.load();
120129
} else if (
121130
typeof value === "object" &&
122131
value !== null &&
@@ -128,6 +137,9 @@ export function cli(pkg: any) {
128137
}
129138
};
130139
loadAll(client);
140+
}
141+
// If there are no args, display help
142+
if (!args.length) {
131143
client.cli.help();
132144
} else {
133145
cmd = client.cli.parse(process.argv);

0 commit comments

Comments
 (0)