Skip to content

Commit e36f765

Browse files
authoredNov 11, 2023
fix: graceful recovery for failed spinner step (#1017)
## 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.
1 parent a412154 commit e36f765

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
 

‎src/shared/cli/spinners.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,33 @@ export async function withSpinners(
4040
let currentLabel!: string;
4141
let lastLogged!: string;
4242

43-
try {
44-
for (const [label, run] of tasks) {
45-
currentLabel = label;
43+
for (const [label, run] of tasks) {
44+
currentLabel = label;
4645

47-
const line = makeLine(chalk.gray(` - ${label}`));
48-
const stopWriting = startLineWithDots(line);
46+
const line = makeLine(chalk.gray(` - ${label}`));
47+
const stopWriting = startLineWithDots(line);
4948

49+
try {
5050
await run();
51+
} catch (error) {
52+
const descriptor = `${lowerFirst(label)} > ${lowerFirst(currentLabel)}`;
5153

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

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

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

63-
logNewSection(chalk.green(`✅ Passed ${lowerFirst(label)}.`));
64-
} catch (error) {
65-
const descriptor = `${lowerFirst(label)} > ${lowerFirst(currentLabel)}`;
65+
process.stdout.write(lastLogged);
66+
}
6667

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

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

0 commit comments

Comments
 (0)