Skip to content

Commit 31ed0d0

Browse files
committed
fix(cli): fix abuse of ora spinners
ora only supports one spinner at a time, otherwise the behaviour is... undefined.
1 parent 7befb37 commit 31ed0d0

File tree

1 file changed

+34
-44
lines changed

1 file changed

+34
-44
lines changed

src/cli.ts

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {normalize} from "path";
44
import ora = require("ora");
55
import mri = require("mri");
66
import glob = require("glob");
7-
import {notNull} from "@softwareventures/nullable";
87
import {hasProperty} from "unknown";
98
import {concatMap, isArray} from "@softwareventures/array";
109
import {pairwise} from "rxjs";
@@ -74,9 +73,7 @@ const options = {
7473
formatter
7574
};
7675

77-
const primarySpinner = ora(` Running ${libraryName}...`);
78-
const modifiedFilesSpinner = ora(" Detecting modified files from git...");
79-
const fileSpinners: ora.Ora[] = [];
76+
const spinner = ora(`Running ${libraryName}`);
8077

8178
let shouldErrorOut = false;
8279

@@ -86,66 +83,59 @@ main(process.cwd(), options)
8683
next: ([previous, current]) => {
8784
if (current.state === "Running") {
8885
if (previous.state === "Initializing") {
89-
primarySpinner.start();
90-
modifiedFilesSpinner.start();
86+
spinner.start(`Running ${libraryName}`);
9187
}
9288

93-
modifiedFilesSpinner.text = ` ${libraryName}: ${current.files.length} modified file(s) found`;
94-
if (current.gitSearchComplete) {
95-
modifiedFilesSpinner.succeed();
89+
if (current.files.length > 0) {
90+
spinner.text = `${libraryName}: ${current.files.length} modified file(s) found`;
9691
}
9792

9893
current.files.forEach((fileState, index) => {
99-
if (index > fileSpinners.length) {
100-
fileSpinners.push(
101-
ora().start(
102-
` [${index + 1}/${current.files.length}] Processing file: ${
103-
fileState.filename
104-
}`
105-
)
106-
);
94+
const previousFileState =
95+
previous.state === "Running" ? previous.files[index] : undefined;
96+
97+
if (previousFileState == null) {
98+
spinner.info(`Processing file: ${fileState.filename}`);
10799
}
108100

109-
if (fileState.status === "Updated") {
110-
notNull(fileSpinners[index]).succeed(
111-
` --> Updated formatting in: ${fileState.filename}`
112-
);
113-
} else if (fileState.status === "NotUpdated") {
114-
notNull(fileSpinners[index]).info(
115-
` --> No formatting changes required in: ${fileState.filename}`
116-
);
117-
} else if (fileState.status === "InvalidFormatting") {
118-
// If --check-only is passed as a CLI argument, the script will error out.
119-
if (options.checkOnly) {
120-
shouldErrorOut = true;
101+
if (previousFileState?.status !== fileState.status) {
102+
if (fileState.status === "Updated") {
103+
spinner.succeed(`Updated formatting in: ${fileState.filename}`);
104+
} else if (fileState.status === "NotUpdated") {
105+
spinner.succeed(
106+
`No formatting changes required in: ${fileState.filename}`
107+
);
108+
} else if (fileState.status === "InvalidFormatting") {
109+
// If --check-only is passed as a CLI argument, the script will error out.
110+
if (options.checkOnly) {
111+
shouldErrorOut = true;
112+
}
113+
114+
spinner.fail(`Invalid formatting detected in: ${fileState.filename}`);
121115
}
122-
123-
notNull(fileSpinners[index]).fail(
124-
` --> Invalid formatting detected in: ${fileState.filename}`
125-
);
126116
}
127117
});
128118
} else if (current.state === "Finished") {
129119
if (current.fileCount === 0) {
130-
modifiedFilesSpinner.info(` ${libraryName}: No matching modified files detected.
131-
132-
--> If you feel that one or more files should be showing up here, be sure to first check what file extensions prettier supports, and whether or not you have included those files in a .prettierignore file
133-
134-
`);
135-
primarySpinner.stop();
120+
spinner.info("No matching modified files detected.");
121+
spinner.info(
122+
"If you feel that one or more files should be showing up here, be sure to first check what file extensions prettier supports, and whether or not you have included those files in a .prettierignore file"
123+
);
124+
spinner.stop();
136125
} else if (options.checkOnly) {
137-
primarySpinner.succeed(" Checks complete 🎉");
126+
spinner.succeed("Checks complete 🎉");
138127
} else {
139-
primarySpinner.succeed(" Formatting complete 🎉");
128+
spinner.succeed("Formatting complete 🎉");
140129
}
141130
return process.exit(shouldErrorOut ? 1 : 0);
142131
}
143132
},
144133
error: (error: unknown) => {
145-
modifiedFilesSpinner.fail(` ${libraryName}: An Error occurred\n`);
134+
spinner.fail("An Error occurred");
135+
console.error("\n");
146136
console.error(error);
147-
console.log("\n");
148-
primarySpinner.stop();
137+
console.error("\n");
138+
spinner.stop();
149139
return process.exit(1);
150140
}
151141
});

0 commit comments

Comments
 (0)