diff --git a/.changeset/wise-jokes-impress.md b/.changeset/wise-jokes-impress.md new file mode 100644 index 0000000..398ebb0 --- /dev/null +++ b/.changeset/wise-jokes-impress.md @@ -0,0 +1,5 @@ +--- +"@astrojs/cli-kit": patch +--- + +Added onError functionality to spinner to handle errors if while arg throws an error diff --git a/src/spinner/index.ts b/src/spinner/index.ts index 8560e8d..356a6f1 100644 --- a/src/spinner/index.ts +++ b/src/spinner/index.ts @@ -108,17 +108,26 @@ export async function spinner( { start, end, + onError, while: update = () => sleep(100), - }: { start: string; end: string; while: (...args: any) => Promise }, + }: { start: string; end: string; onError?: (e: any) => void; while: (...args: any) => Promise }, { stdin = process.stdin, stdout = process.stdout } = {} ) { - const act = update(); - const tooslow = Object.create(null); - const result = await Promise.race([sleep(500).then(() => tooslow), act]); - if (result === tooslow) { const loading = await gradient(chalk.green(start), { stdin, stdout }); - await act; - loading.stop(); - } - stdout.write(`${" ".repeat(5)} ${chalk.green("✔")} ${chalk.green(end)}\n`); -} + + const act = update(); + const tooslow = Object.create(null); + + try { + const result = await Promise.race([sleep(500).then(() => tooslow), act]); + if (result === tooslow) { + await act; + } + + stdout.write(`${" ".repeat(5)} ${chalk.green("✔")} ${chalk.green(end)}\n`); + } catch (e) { + onError?.(e); + } finally { + loading.stop(); + } +} \ No newline at end of file