Skip to content

feat: run ESLint and Prettier fixers in hydration #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/hydrate/hydrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { runOrRestore } from "../shared/runOrRestore.js";
import { clearUnnecessaryFiles } from "./steps/clearUnnecessaryFiles.js";
import { finalizeDependencies as finalizeDependencies } from "./steps/finalizeDependencies.js";
import { runCommand } from "./steps/runCommand.js";
import { writeReadme } from "./steps/writeReadme.js";
import { writeStructure } from "./steps/writing/writeStructure.js";
import { getHydrationDefaults } from "./values/getHydrationDefaults.js";
Expand Down Expand Up @@ -53,6 +54,9 @@ export async function hydrate(args: string[]) {
);
}

await runCommand("pnpm lint --fix", "auto-fixing lint rules");
await runCommand("pnpm format --write", "formatting files");

if (hydrationSkips["skip-setup"]) {
skipSpinnerBlock(`Done hydrating, and skipping setup command.`);
} else {
Expand Down
48 changes: 48 additions & 0 deletions src/hydrate/steps/runCommand.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import chalk from "chalk";
import { describe, expect, it, vi } from "vitest";

import { runCommand } from "./runCommand.js";

const mockExecaCommand = vi.fn().mockRejectedValue("Oh no!");

vi.mock("execa", () => ({
get execaCommand() {
return mockExecaCommand;
},
}));

const mockLogLine = vi.fn();

vi.mock("../../shared/cli/lines.js", () => ({
get logLine() {
return mockLogLine;
},
}));

vi.mock("../../shared/cli/spinners.js", () => ({
withSpinner: vi.fn((callback: () => unknown) => callback()),
}));

describe("runCommand", () => {
it("does not log when the command succeeds", async () => {
mockExecaCommand.mockResolvedValue(undefined);

await runCommand("command", "label");

expect(mockLogLine).not.toHaveBeenCalled();
});

it("logs when the command failures", async () => {
mockExecaCommand.mockRejectedValue(new Error("Oh no!"));

await runCommand("command", "label");

expect(mockLogLine).toHaveBeenCalledWith(
[
chalk.yellow(`⚠️ Running \``),
chalk.yellowBright("command"),
chalk.yellow(`\` failed. You should run it and fix its complaints.`),
].join("")
);
});
});
27 changes: 27 additions & 0 deletions src/hydrate/steps/runCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import chalk from "chalk";
import { execaCommand } from "execa";

import { logLine } from "../../shared/cli/lines.js";
import { withSpinner } from "../../shared/cli/spinners.js";

export async function runCommand(command: string, label: string) {
const succeeded = await withSpinner(async () => {
try {
await execaCommand(command);
return true;
} catch {
return false;
}
}, label);

if (!succeeded) {
logLine();
logLine(
[
chalk.yellow(`⚠️ Running \``),
chalk.yellowBright(command),
chalk.yellow(`\` failed. You should run it and fix its complaints.`),
].join("")
);
}
}
2 changes: 1 addition & 1 deletion src/shared/cli/spinners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function withSpinner<Return>(
const extra = warningHint ? `: ${warningHint}` : "";
s.stop(chalk.yellow(`⚠️ Error ${label}${extra}.`));
} else {
s.stop(chalk.green(`✅ Success ${label}.`));
s.stop(chalk.green(`✅ Passed ${label}.`));
}

return result;
Expand Down
5 changes: 4 additions & 1 deletion src/shared/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import chalk from "chalk";
import { $ } from "execa";

import { logLine } from "./cli/lines.js";

export async function getDefaultSettings() {
let gitRemoteFetch;
try {
Expand All @@ -9,7 +11,8 @@ export async function getDefaultSettings() {
.pipeStdout?.($({ stdin: "pipe" })`grep origin`)
.pipeStdout?.($({ stdin: "pipe" })`grep fetch`);
} catch {
console.log(
logLine();
logLine(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't resist this little cleanup 😄

chalk.gray(
"Could not populate default owner and repository. Did not detect a Git repository with an origin. "
)
Expand Down