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.
This commit is contained in:
iamtoruk 2026-08-28 05:43:18 -07:00
parent 56dcc402d7
commit 4912be1915
3 changed files with 67 additions and 22 deletions

View file

@ -73,9 +73,13 @@ export function registerPluginCommands(program: Command): void {
}
const rejected = loads.find((l): l is Extract<typeof l, { status: 'rejected' }> => 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`)

View file

@ -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 <a.b.c" ranges manifests declare; no dependency.
/// Note: prerelease suffixes compare as extra numeric segments, so "1.0.0-canary" > "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)

View file

@ -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
}
})
})