Skip to content

Commit 54f5762

Browse files
Copilothotlong
andcommitted
refactor: address code review feedback - improve type safety
- Replace `any` with `Record<string, unknown>` for config type - Replace `catch (error: any)` with `catch (error: unknown)` and proper narrowing - Refactor isCommandInstance to use intermediate typed variable instead of repeated `as any` Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent d1e6da8 commit 54f5762

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

packages/cli/src/utils/plugin-commands.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface ResolvedCommandContribution {
2828
* @param program - The root Commander.js program to register commands on
2929
*/
3030
export async function loadPluginCommands(program: Command): Promise<void> {
31-
let config: any;
31+
let config: Record<string, unknown>;
3232

3333
try {
3434
const loaded = await loadConfig();
@@ -39,8 +39,8 @@ export async function loadPluginCommands(program: Command): Promise<void> {
3939
}
4040

4141
const plugins: unknown[] = [
42-
...(config.plugins || []),
43-
...(config.devPlugins || []),
42+
...((config.plugins as unknown[] | undefined) || []),
43+
...((config.devPlugins as unknown[] | undefined) || []),
4444
];
4545

4646
// Collect command contributions from plugin manifests
@@ -79,11 +79,12 @@ export async function loadPluginCommands(program: Command): Promise<void> {
7979
for (const cmd of commands) {
8080
program.addCommand(cmd);
8181
}
82-
} catch (error: any) {
82+
} catch (error: unknown) {
8383
// Log warning but don't crash — plugin commands are optional
8484
if (process.env.DEBUG) {
85+
const message = error instanceof Error ? error.message : String(error);
8586
console.error(
86-
chalk.yellow(` ⚠ Failed to load CLI command '${contribution.name}' from plugin '${contribution.pluginName}': ${error.message}`)
87+
chalk.yellow(` ⚠ Failed to load CLI command '${contribution.name}' from plugin '${contribution.pluginName}': ${message}`)
8788
);
8889
}
8990
}
@@ -140,13 +141,13 @@ async function importPluginCommands(
140141
* Uses duck-typing to avoid import dependency issues.
141142
*/
142143
function isCommandInstance(value: unknown): value is Command {
144+
if (value === null || typeof value !== 'object') return false;
145+
const obj = value as Record<string, unknown>;
143146
return (
144-
value !== null &&
145-
typeof value === 'object' &&
146-
typeof (value as any).name === 'function' &&
147-
typeof (value as any).description === 'function' &&
148-
typeof (value as any).action === 'function' &&
149-
typeof (value as any).parse === 'function'
147+
typeof obj.name === 'function' &&
148+
typeof obj.description === 'function' &&
149+
typeof obj.action === 'function' &&
150+
typeof obj.parse === 'function'
150151
);
151152
}
152153

0 commit comments

Comments
 (0)