From 4912be19151818a2dc4ebf093903707da2f8e8a5 Mon Sep 17 00:00:00 2001 From: iamtoruk Date: Fri, 28 Aug 2026 05:43:18 -0700 Subject: [PATCH] fix(plugins): polite one-line errors for plugin commands instead of stack traces Convert user-facing plugin commands (info, verify, add, remove) to use process.stderr.write() + process.exitCode pattern. Wrap internal helper calls in try-catch at action boundary. Update tests to capture stderr and check exitCode instead of expecting thrown errors. --- src/plugins/cli.ts | 47 ++++++++++++++++++++++++------------- src/plugins/manifest.ts | 1 + tests/plugin-socket.test.ts | 41 +++++++++++++++++++++++++++----- 3 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/plugins/cli.ts b/src/plugins/cli.ts index b057136d..bd79ed44 100644 --- a/src/plugins/cli.ts +++ b/src/plugins/cli.ts @@ -73,9 +73,13 @@ export function registerPluginCommands(program: Command): void { } const rejected = loads.find((l): l is Extract => l.status === 'rejected' && l.name === name) if (rejected) { - throw new Error(`Plugin "${name}" is not loaded: ${rejected.reason}`) + process.stderr.write(`Error: Plugin "${name}" is not loaded: ${rejected.reason}\n`) + process.exitCode = 1 + return } - throw new Error(`Plugin "${name}" not found in ${opts.dir ?? defaultPluginsDir()}.`) + process.stderr.write(`Error: Plugin "${name}" not found in ${opts.dir ?? defaultPluginsDir()}.\n`) + process.exitCode = 1 + return }) plugin @@ -86,13 +90,17 @@ export function registerPluginCommands(program: Command): void { const dir = join(opts.dir ?? defaultPluginsDir(), name) const manifest = await readManifestForVerify(dir, name) if (!manifest) { - throw new Error(`Plugin "${name}" could not be loaded for verify.`) + process.stderr.write(`Error: Plugin "${name}" could not be loaded for verify.\n`) + process.exitCode = 1 + return } const result = await verifyPlugin(dir, manifest, process.env) if (result.ok) { process.stdout.write(`verified ${name}@${manifest.version}\n`) } else { - throw new Error(`unverified ${name}@${manifest.version} ${result.reason ?? 'verification failed'}`) + process.stderr.write(`Error: unverified ${name}@${manifest.version} ${result.reason ?? 'verification failed'}\n`) + process.exitCode = 1 + return } }) @@ -103,18 +111,24 @@ export function registerPluginCommands(program: Command): void { .action(async (source: string, opts: { dir?: string }) => { const pluginsDir = opts.dir ?? defaultPluginsDir() - // Dispatch: if source looks like a path or exists as directory, use local flow; otherwise remote - const isLocal = source.includes('/') || source.includes('.') || - (await stat(source).then(() => true).catch(() => false)) + try { + // Dispatch: if source looks like a path or exists as directory, use local flow; otherwise remote + const isLocal = source.includes('/') || source.includes('.') || + (await stat(source).then(() => true).catch(() => false)) - if (isLocal) { - await addLocal(source, pluginsDir) - } else { - // Validate plugin name - if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(source)) { - throw new Error(`Invalid plugin name "${source}". Must match [a-z0-9]([a-z0-9-]*[a-z0-9])?`) + if (isLocal) { + await addLocal(source, pluginsDir) + } else { + // Validate plugin name + if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(source)) { + throw new Error(`Invalid plugin name "${source}". Must match [a-z0-9]([a-z0-9-]*[a-z0-9])?`) + } + await addRemote(source, pluginsDir) } - await addRemote(source, pluginsDir) + } catch (err) { + process.stderr.write(`Error: ${(err as Error).message}\n`) + process.exitCode = 1 + return } }) @@ -127,8 +141,9 @@ export function registerPluginCommands(program: Command): void { const pluginsDir = opts.dir ?? defaultPluginsDir() const destDir = join(pluginsDir, name) if (!opts.confirm) { - process.stdout.write(`Would remove plugin directory: ${destDir}\nUse --confirm to proceed.\n`) - process.exit(1) + process.stderr.write(`Error: Use --confirm to proceed with removal of ${destDir}\n`) + process.exitCode = 1 + return } await rm(destDir, { recursive: true, force: true }) process.stdout.write(`Plugin "${name}" removed.\n`) diff --git a/src/plugins/manifest.ts b/src/plugins/manifest.ts index 96f08051..da5a17dd 100644 --- a/src/plugins/manifest.ts +++ b/src/plugins/manifest.ts @@ -49,6 +49,7 @@ export function parsePluginManifest(raw: unknown, source: string): ParseResult { /// Minimal semver-ish compare: dot-separated numeric segments, missing = 0. /// Enough for the ">=x.y.z "1.0.0". export function compareVersions(a: string, b: string): number { const pa = a.split('.').map(s => parseInt(s, 10) || 0) const pb = b.split('.').map(s => parseInt(s, 10) || 0) diff --git a/tests/plugin-socket.test.ts b/tests/plugin-socket.test.ts index 582a7bec..b3dd7076 100644 --- a/tests/plugin-socket.test.ts +++ b/tests/plugin-socket.test.ts @@ -196,9 +196,24 @@ describe('plugin CLI: codeburn plugin list|info|verify', () => { const emptyDir = await mkdtemp(join(tmpdir(), 'empty-info-')) try { const program = makeProgram() - await expect( - program.parseAsync(['node', 'codeburn', 'plugin', 'info', 'nope', '--dir', emptyDir]), - ).rejects.toThrow() + const savedStderr = process.stderr.write + const savedExitCode = process.exitCode + let stderrOutput = '' + process.stderr.write = (chunk: string | Uint8Array | Buffer): boolean => { + stderrOutput += chunk.toString() + return true + } + process.exitCode = undefined + + try { + await program.parseAsync(['node', 'codeburn', 'plugin', 'info', 'nope', '--dir', emptyDir]) + expect(stderrOutput).toContain('Error:') + expect(stderrOutput).toContain('nope') + expect(process.exitCode).toBe(1) + } finally { + process.stderr.write = savedStderr + process.exitCode = savedExitCode + } } finally { await rm(emptyDir, { recursive: true, force: true }) } @@ -224,9 +239,23 @@ describe('plugin CLI: codeburn plugin list|info|verify', () => { await mkdir(pluginDir, { recursive: true }) await writeFile(join(pluginDir, 'codeburn-plugin.json'), '{ not json') const program = makeProgram() - await expect( - program.parseAsync(['node', 'codeburn', 'plugin', 'verify', 'broken', '--dir', tmpDir]), - ).rejects.toThrow() + const savedStderr = process.stderr.write + const savedExitCode = process.exitCode + let stderrOutput = '' + process.stderr.write = (chunk: string | Uint8Array | Buffer): boolean => { + stderrOutput += chunk.toString() + return true + } + process.exitCode = undefined + + try { + await program.parseAsync(['node', 'codeburn', 'plugin', 'verify', 'broken', '--dir', tmpDir]) + expect(stderrOutput).toContain('Error:') + expect(process.exitCode).toBe(1) + } finally { + process.stderr.write = savedStderr + process.exitCode = savedExitCode + } }) })