Skip to content

Commit

Permalink
fix: graceful recovery for failed spinner step (#1017)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #1016
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

Moves the recovery pieces of the `withSpinners` loop into a `finally` so
they always get called.
  • Loading branch information
JoshuaKGoldberg authored Nov 11, 2023
1 parent a412154 commit e36f765
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/shared/cli/spinners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,33 @@ export async function withSpinners(
let currentLabel!: string;
let lastLogged!: string;

try {
for (const [label, run] of tasks) {
currentLabel = label;
for (const [label, run] of tasks) {
currentLabel = label;

const line = makeLine(chalk.gray(` - ${label}`));
const stopWriting = startLineWithDots(line);
const line = makeLine(chalk.gray(` - ${label}`));
const stopWriting = startLineWithDots(line);

try {
await run();
} catch (error) {
const descriptor = `${lowerFirst(label)} > ${lowerFirst(currentLabel)}`;

const lineLength = stopWriting();
lastLogged = chalk.gray(`${line} ✔️\n`);
logLine(chalk.red(`❌ Error ${descriptor}.`));

throw new Error(`Failed ${descriptor}`, { cause: error });
} finally {
const lineLength = stopWriting();
readline.clearLine(process.stdout, -1);
readline.moveCursor(process.stdout, -lineLength, 0);
process.stdout.write(lastLogged);
}

readline.moveCursor(process.stdout, -lastLogged.length, -tasks.length - 2);
readline.clearScreenDown(process.stdout);
lastLogged = chalk.gray(`${line} ✔️\n`);

logNewSection(chalk.green(`✅ Passed ${lowerFirst(label)}.`));
} catch (error) {
const descriptor = `${lowerFirst(label)} > ${lowerFirst(currentLabel)}`;
process.stdout.write(lastLogged);
}

logLine(chalk.red(`❌ Error ${descriptor}.`));
readline.moveCursor(process.stdout, -lastLogged.length, -tasks.length - 2);
readline.clearScreenDown(process.stdout);

throw new Error(`Failed ${descriptor}`, { cause: error });
}
logNewSection(chalk.green(`✅ Passed ${lowerFirst(label)}.`));
}

0 comments on commit e36f765

Please sign in to comment.