feat(cli): enhance error handling and auto-create output directory

This commit is contained in:
chenbaowang 2026-04-24 11:40:50 +08:00
parent bc9b3d1eb2
commit d96fb008f9
2 changed files with 21 additions and 3 deletions

View file

@ -1,6 +1,6 @@
import { Command } from "commander";
import { createWriteStream } from "node:fs";
import { resolve } from "node:path";
import { createWriteStream, mkdirSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { finished } from "node:stream/promises";
import { ApiClient } from "../core/api-client.js";
import { loadConfigFromProgram } from "../core/config.js";
@ -190,6 +190,14 @@ export function registerDownload(program: Command) {
}
const outPath = resolve(outputDir, `${skillSlug}.zip`);
const outDir = dirname(outPath);
try {
mkdirSync(outDir, { recursive: true });
} catch (e: any) {
spinner.fail(`Failed to create output directory: ${outDir}`);
process.exitCode = 1;
return;
}
const fileStream = createWriteStream(outPath);
await finished(body.pipe(fileStream));

View file

@ -444,7 +444,17 @@ async function installFromRegistry(
const { statusCode, body } = response;
if (statusCode >= 400) {
spinner.fail(`Skill not found: ${ns}/${actualSlug}`);
let errorMsg = `Failed to download skill: ${ns}/${actualSlug}`;
if (statusCode === 404) {
errorMsg = `Skill not found: ${ns}/${actualSlug}`;
} else if (statusCode === 403) {
errorMsg = `Access denied: ${ns}/${actualSlug}. Check your token permissions.`;
} else if (statusCode === 503) {
errorMsg = `Service temporarily unavailable. Please try again later.`;
} else if (statusCode >= 500) {
errorMsg = `Server error (${statusCode}). Please try again later.`;
}
spinner.fail(errorMsg);
await rm(tmpDir, { recursive: true, force: true });
process.exitCode = 1;
return;