Skip to content

Commit

Permalink
Merge pull request #25 from skirianov/feat/spinner-handle-errors
Browse files Browse the repository at this point in the history
feat: spinner now accepts and onError
  • Loading branch information
ematipico authored Nov 7, 2023
2 parents 50c7a55 + 8f03132 commit 5ef4afc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-jokes-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/cli-kit": patch
---

Added onError functionality to spinner to handle errors if while arg throws an error
29 changes: 19 additions & 10 deletions src/spinner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,26 @@ export async function spinner(
{
start,
end,
onError,
while: update = () => sleep(100),
}: { start: string; end: string; while: (...args: any) => Promise<any> },
}: { start: string; end: string; onError?: (e: any) => void; while: (...args: any) => Promise<any> },
{ 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();
}
}

0 comments on commit 5ef4afc

Please sign in to comment.