Skip to content

perf: rimraf coverage directories in --mode migrate #1248

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
Jan 16, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"prettier-plugin-curly": "^0.1.3",
"prettier-plugin-packagejson": "^2.4.7",
"release-it": "^17.0.0",
"rimraf": "^5.0.5",
"sentences-per-line": "^0.2.1",
"should-semantic-release": "^0.2.1",
"tsup": "^8.0.1",
Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion script/create-test-e2e.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { $, execaCommand } from "execa";
import { strict as assert } from "node:assert";
import { rimraf } from "rimraf";

const author = "Test Author";
const description = "Test description.";
Expand All @@ -8,7 +9,7 @@ const repository = "create-typescript-app";
const owner = "TestOwner";
const title = "Test Title";

await $`rm -rf ${repository}`;
await rimraf(["coverage*", repository]);

// Fist we run with --mode create to create a new new local repository,
// asserting that pnpm i passes in that repository's directory.
Expand Down
3 changes: 3 additions & 0 deletions script/initialize-test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { $ } from "execa";
import { globby } from "globby";
import { strict as assert } from "node:assert";
import * as fs from "node:fs/promises";
import { rimraf } from "rimraf";

const description = "New Description Test";
const owner = "RNR1";
const title = "New Title Test";
const repository = "new-repository-test";

await rimraf("coverage*");

// Fist we run with --mode initialize to modify the local repository files,
// asserting that the created package.json keeps the general description.
await $({
Expand Down
3 changes: 3 additions & 0 deletions script/migrate-test-e2e.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from "chalk";
import { $, execaCommand } from "execa";
import * as fs from "node:fs/promises";
import { rimraf } from "rimraf";
import { assert, describe, expect, test } from "vitest";

import packageData from "../package.json" assert { type: "json" };
Expand Down Expand Up @@ -37,6 +38,8 @@ const logoAlt = `Project logo: the TypeScript blue square with rounded corners,
const owner = "JoshuaKGoldberg";
const title = "Create TypeScript App";

await rimraf("coverage*");

const originalReadme = (await fs.readFile("README.md")).toString();

const originalSnapshots = (
Expand Down
12 changes: 6 additions & 6 deletions src/create/createWithOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { addToolAllContributors } from "../steps/addToolAllContributors.js";
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { populateCSpellDictionary } from "../steps/populateCSpellDictionary.js";
import { runCommands } from "../steps/runCommands.js";
import { runCleanup } from "../steps/runCleanup.js";
import { createWithOptions } from "./createWithOptions.js";

const optionsBase: Options = {
Expand Down Expand Up @@ -63,7 +63,7 @@ vi.mock("../steps/finalizeDependencies.js");

vi.mock("../steps/populateCSpellDictionary.js");

vi.mock("../steps/runCommands.js");
vi.mock("../steps/runCleanup.js");

vi.mock("../shared/doesRepositoryExist.js", () => ({
doesRepositoryExist: vi.fn().mockResolvedValue(true),
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("createWithOptions", () => {
expect(addToolAllContributors).not.toHaveBeenCalled();
});

it("does not call finalizeDependencies, populateCSpellDictionary, or runCommands when skipInstall is true", async () => {
it("does not call finalizeDependencies, populateCSpellDictionary, or runCleanup when skipInstall is true", async () => {
const options = {
...optionsBase,
skipInstall: true,
Expand All @@ -123,10 +123,10 @@ describe("createWithOptions", () => {
await createWithOptions({ github, options });
expect(finalizeDependencies).not.toHaveBeenCalled();
expect(populateCSpellDictionary).not.toHaveBeenCalled();
expect(runCommands).not.toHaveBeenCalled();
expect(runCleanup).not.toHaveBeenCalled();
});

it("calls finalizeDependencies, populateCSpellDictionary, and runCommands when skipInstall is false", async () => {
it("calls finalizeDependencies, populateCSpellDictionary, and runCleanup when skipInstall is false", async () => {
const options = {
...optionsBase,
skipInstall: false,
Expand All @@ -136,7 +136,7 @@ describe("createWithOptions", () => {

expect(finalizeDependencies).toHaveBeenCalledWith(options);
expect(populateCSpellDictionary).toHaveBeenCalled();
expect(runCommands).toHaveBeenCalled();
expect(runCleanup).toHaveBeenCalled();
});

it("does not initialize GitHub repository if repository does not exist", async () => {
Expand Down
12 changes: 3 additions & 9 deletions src/create/createWithOptions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { $ } from "execa";

import { withSpinner, withSpinners } from "../shared/cli/spinners.js";
import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js";
import { createCleanupCommands } from "../shared/createCleanupCommands.js";
import { doesRepositoryExist } from "../shared/doesRepositoryExist.js";
import { GitHubAndOptions } from "../shared/options/readOptions.js";
import { addToolAllContributors } from "../steps/addToolAllContributors.js";
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { populateCSpellDictionary } from "../steps/populateCSpellDictionary.js";
import { runCommands } from "../steps/runCommands.js";
import { runCleanup } from "../steps/runCleanup.js";
import { writeReadme } from "../steps/writeReadme/index.js";
import { writeStructure } from "../steps/writing/writeStructure.js";

Expand Down Expand Up @@ -46,13 +46,7 @@ export async function createWithOptions({ github, options }: GitHubAndOptions) {
);
}

await runCommands(
"Cleaning up files",
createCleanUpFilesCommands({
bin: !!options.bin,
dedupe: true,
}),
);
await runCleanup(createCleanupCommands(options), options.mode);
}

const sendToGitHub =
Expand Down
11 changes: 3 additions & 8 deletions src/initialize/initializeWithOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { withSpinner, withSpinners } from "../shared/cli/spinners.js";
import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js";
import { createCleanupCommands } from "../shared/createCleanupCommands.js";
import { GitHubAndOptions } from "../shared/options/readOptions.js";
import { addOwnerAsAllContributor } from "../steps/addOwnerAsAllContributor.js";
import { clearChangelog } from "../steps/clearChangelog.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { removeSetupScripts } from "../steps/removeSetupScripts.js";
import { resetGitTags } from "../steps/resetGitTags.js";
import { runCommands } from "../steps/runCommands.js";
import { runCleanup } from "../steps/runCleanup.js";
import { uninstallPackages } from "../steps/uninstallPackages.js";
import { updateAllContributorsTable } from "../steps/updateAllContributorsTable.js";
import { updateLocalFiles } from "../steps/updateLocalFiles.js";
Expand Down Expand Up @@ -61,10 +61,5 @@ export async function initializeWithOptions({
);
}

await runCommands(
"Cleaning up files",
createCleanUpFilesCommands({
bin: !!options.bin,
}),
);
await runCleanup(createCleanupCommands(options), options.mode);
}
12 changes: 3 additions & 9 deletions src/migrate/migrateWithOptions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { withSpinner, withSpinners } from "../shared/cli/spinners.js";
import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js";
import { createCleanupCommands } from "../shared/createCleanupCommands.js";
import { GitHubAndOptions } from "../shared/options/readOptions.js";
import { clearUnnecessaryFiles } from "../steps/clearUnnecessaryFiles.js";
import { detectExistingContributors } from "../steps/detectExistingContributors.js";
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { populateCSpellDictionary } from "../steps/populateCSpellDictionary.js";
import { runCommands } from "../steps/runCommands.js";
import { runCleanup } from "../steps/runCleanup.js";
import { updateAllContributorsTable } from "../steps/updateAllContributorsTable.js";
import { updateLocalFiles } from "../steps/updateLocalFiles.js";
import { writeReadme } from "../steps/writeReadme/index.js";
Expand Down Expand Up @@ -66,11 +66,5 @@ export async function migrateWithOptions({
await withSpinner("Populating CSpell dictionary", populateCSpellDictionary);
}

await runCommands(
"Cleaning up files",
createCleanUpFilesCommands({
bin: !!options.bin,
dedupe: true,
}),
);
await runCleanup(createCleanupCommands(options), options.mode);
}
25 changes: 0 additions & 25 deletions src/shared/createCleanUpFilesCommands.test.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/shared/createCleanupCommands.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";

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

describe("createCleanupCommands", () => {
it("only lints and formats when bin is not enabled and mode is initialize", () => {
const actual = createCleanupCommands({
bin: undefined,
mode: "initialize",
});

expect(actual).toEqual(["pnpm lint --fix", "pnpm format --write"]);
});

it("runs dedupe and build before it lints and formats when bin is enabled and mode is create", () => {
const actual = createCleanupCommands({
bin: "bin/index.js",
mode: "create",
});

expect(actual).toEqual([
"pnpm dedupe",
"pnpm build",
"pnpm lint --fix",
"pnpm format --write",
]);
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
export interface CleanUpFilesOptions {
bin?: boolean;
dedupe?: boolean;
}
import { Options } from "./types.js";

export function createCleanUpFilesCommands({
export function createCleanupCommands({
bin,
dedupe,
}: CleanUpFilesOptions) {
mode,
}: Pick<Options, "bin" | "mode">) {
return [
// There's no need to dedupe when initializing from the fixed template
...(dedupe ? ["pnpm dedupe"] : []),
...(mode === "initialize" ? [] : ["pnpm dedupe"]),
// n/no-missing-import rightfully reports on a missing the bin .js file
...(bin ? ["pnpm build"] : []),
"pnpm lint --fix",
Expand Down
10 changes: 5 additions & 5 deletions src/steps/runCommands.test.ts → src/steps/runCleanup.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from "chalk";
import { describe, expect, it, vi } from "vitest";

import { runCommands } from "./runCommands.js";
import { runCleanup } from "./runCleanup.js";

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

Expand All @@ -23,11 +23,11 @@ vi.mock("../shared/cli/spinners.js", () => ({
withSpinner: vi.fn((_label: string, callback: () => unknown) => callback()),
}));

describe("runCommands", () => {
describe("runCleanup", () => {
it("does not log when the commands all succeed", async () => {
mockExecaCommand.mockResolvedValue(undefined);

await runCommands("label", ["first", "second"]);
await runCleanup(["first", "second"], "create");

expect(mockLogLine).not.toHaveBeenCalled();
});
Expand All @@ -37,7 +37,7 @@ describe("runCommands", () => {
.mockRejectedValueOnce(new Error("Oh no!"))
.mockResolvedValue(undefined);

await runCommands("label", ["first", "second"]);
await runCleanup(["first", "second"], "create");

expect(mockLogLine).toHaveBeenCalledWith(
[
Expand All @@ -51,7 +51,7 @@ describe("runCommands", () => {
it("logs twice when two commands fail", async () => {
mockExecaCommand.mockRejectedValue(new Error("Oh no!"));

await runCommands("label", ["first", "second"]);
await runCleanup(["first", "second"], "create");

expect(mockLogLine).toHaveBeenCalledWith(
[
Expand Down
12 changes: 10 additions & 2 deletions src/steps/runCommands.ts → src/steps/runCleanup.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import chalk from "chalk";
import { execaCommand } from "execa";
import { rimraf } from "rimraf";

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

export async function runCommands(label: string, commands: string[]) {
export async function runCleanup(commands: string[], mode: Mode) {
const failed: string[] = [];

await withSpinner(label, async () => {
await withSpinner("Cleaning up files", async () => {
if (mode === "migrate") {
// Coverage folders can slow down format and lint times something awful.
// https://github.com/JoshuaKGoldberg/create-typescript-app/issues/1221
await rimraf("coverage*");
}

for (const command of commands) {
try {
await execaCommand(command);
Expand Down
1 change: 1 addition & 0 deletions src/steps/uninstallPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function uninstallPackages(offline: boolean | undefined) {
"c8",
"eslint-config-prettier",
"globby",
"rimraf",
"tsx",
],
packageData.devDependencies,
Expand Down