Skip to content

Commit ac286ac

Browse files
feat: run ESLint and Prettier fixers in hydration (#514)
## PR Checklist - [x] Addresses an existing open issue: fixes #452 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Per the issue, runs `pnpm lint --fix` near the end of hydration. Also runs `pnpm format --write`. For both, if there was an error, the user is warned to run the command themselves. Rewords from _"Succeeded"_ to _"Passed"_ for spinner blocks to indicate that there might not have been a total success.
1 parent 472b0a9 commit ac286ac

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

src/hydrate/hydrate.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { runOrRestore } from "../shared/runOrRestore.js";
1010
import { clearUnnecessaryFiles } from "./steps/clearUnnecessaryFiles.js";
1111
import { finalizeDependencies as finalizeDependencies } from "./steps/finalizeDependencies.js";
12+
import { runCommand } from "./steps/runCommand.js";
1213
import { writeReadme } from "./steps/writeReadme.js";
1314
import { writeStructure } from "./steps/writing/writeStructure.js";
1415
import { getHydrationDefaults } from "./values/getHydrationDefaults.js";
@@ -53,6 +54,9 @@ export async function hydrate(args: string[]) {
5354
);
5455
}
5556

57+
await runCommand("pnpm lint --fix", "auto-fixing lint rules");
58+
await runCommand("pnpm format --write", "formatting files");
59+
5660
if (hydrationSkips["skip-setup"]) {
5761
skipSpinnerBlock(`Done hydrating, and skipping setup command.`);
5862
} else {

src/hydrate/steps/runCommand.test.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import chalk from "chalk";
2+
import { describe, expect, it, vi } from "vitest";
3+
4+
import { runCommand } from "./runCommand.js";
5+
6+
const mockExecaCommand = vi.fn().mockRejectedValue("Oh no!");
7+
8+
vi.mock("execa", () => ({
9+
get execaCommand() {
10+
return mockExecaCommand;
11+
},
12+
}));
13+
14+
const mockLogLine = vi.fn();
15+
16+
vi.mock("../../shared/cli/lines.js", () => ({
17+
get logLine() {
18+
return mockLogLine;
19+
},
20+
}));
21+
22+
vi.mock("../../shared/cli/spinners.js", () => ({
23+
withSpinner: vi.fn((callback: () => unknown) => callback()),
24+
}));
25+
26+
describe("runCommand", () => {
27+
it("does not log when the command succeeds", async () => {
28+
mockExecaCommand.mockResolvedValue(undefined);
29+
30+
await runCommand("command", "label");
31+
32+
expect(mockLogLine).not.toHaveBeenCalled();
33+
});
34+
35+
it("logs when the command failures", async () => {
36+
mockExecaCommand.mockRejectedValue(new Error("Oh no!"));
37+
38+
await runCommand("command", "label");
39+
40+
expect(mockLogLine).toHaveBeenCalledWith(
41+
[
42+
chalk.yellow(`⚠️ Running \``),
43+
chalk.yellowBright("command"),
44+
chalk.yellow(`\` failed. You should run it and fix its complaints.`),
45+
].join("")
46+
);
47+
});
48+
});

src/hydrate/steps/runCommand.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import chalk from "chalk";
2+
import { execaCommand } from "execa";
3+
4+
import { logLine } from "../../shared/cli/lines.js";
5+
import { withSpinner } from "../../shared/cli/spinners.js";
6+
7+
export async function runCommand(command: string, label: string) {
8+
const succeeded = await withSpinner(async () => {
9+
try {
10+
await execaCommand(command);
11+
return true;
12+
} catch {
13+
return false;
14+
}
15+
}, label);
16+
17+
if (!succeeded) {
18+
logLine();
19+
logLine(
20+
[
21+
chalk.yellow(`⚠️ Running \``),
22+
chalk.yellowBright(command),
23+
chalk.yellow(`\` failed. You should run it and fix its complaints.`),
24+
].join("")
25+
);
26+
}
27+
}

src/shared/cli/spinners.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function withSpinner<Return>(
2727
const extra = warningHint ? `: ${warningHint}` : "";
2828
s.stop(chalk.yellow(`⚠️ Error ${label}${extra}.`));
2929
} else {
30-
s.stop(chalk.green(`✅ Success ${label}.`));
30+
s.stop(chalk.green(`✅ Passed ${label}.`));
3131
}
3232

3333
return result;

src/shared/defaults.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import chalk from "chalk";
22
import { $ } from "execa";
33

4+
import { logLine } from "./cli/lines.js";
5+
46
export async function getDefaultSettings() {
57
let gitRemoteFetch;
68
try {
@@ -9,7 +11,8 @@ export async function getDefaultSettings() {
911
.pipeStdout?.($({ stdin: "pipe" })`grep origin`)
1012
.pipeStdout?.($({ stdin: "pipe" })`grep fetch`);
1113
} catch {
12-
console.log(
14+
logLine();
15+
logLine(
1316
chalk.gray(
1417
"Could not populate default owner and repository. Did not detect a Git repository with an origin. "
1518
)

0 commit comments

Comments
 (0)