Skip to content

Commit

Permalink
fix: delete any existing Git tags in --mode create (#1614)
Browse files Browse the repository at this point in the history
## PR Checklist

- [x] Addresses an existing open issue: fixes #926
- [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

At the end of `createWithOptions`, prepends a nice little Git command
before the other commands. Includes a small refactor of
`createCleanupCommands` to not care about the mode.

💖
  • Loading branch information
JoshuaKGoldberg authored Aug 11, 2024
1 parent 4b975d2 commit 95cebe1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/create/createWithOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ vi.mock("../steps/finalizeDependencies.js");

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

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

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

vi.mock("../shared/doesRepositoryExist.js", () => ({
Expand Down
5 changes: 4 additions & 1 deletion src/create/createWithOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 { clearLocalGitTags } from "../steps/clearLocalGitTags.js";
import { finalizeDependencies } from "../steps/finalizeDependencies.js";
import { initializeGitHubRepository } from "../steps/initializeGitHubRepository/index.js";
import { populateCSpellDictionary } from "../steps/populateCSpellDictionary.js";
Expand Down Expand Up @@ -46,9 +47,11 @@ export async function createWithOptions({ github, options }: GitHubAndOptions) {
);
}

await runCleanup(createCleanupCommands(options), options.mode);
await runCleanup(createCleanupCommands(options.bin), options.mode);
}

await withSpinner("Clearing any local Git tags", clearLocalGitTags);

const sendToGitHub =
github && (await doesRepositoryExist(github.octokit, options));

Expand Down
5 changes: 4 additions & 1 deletion src/initialize/initializeWithOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@ export async function initializeWithOptions({
);
}

await runCleanup(createCleanupCommands(options), options.mode);
await runCleanup(
createCleanupCommands(options.bin, "pnpm dedupe --offline"),
options.mode,
);
}
2 changes: 1 addition & 1 deletion src/migrate/migrateWithOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ export async function migrateWithOptions({
await withSpinner("Populating CSpell dictionary", populateCSpellDictionary);
}

await runCleanup(createCleanupCommands(options), options.mode);
await runCleanup(createCleanupCommands(options.bin), options.mode);
}
26 changes: 15 additions & 11 deletions src/shared/createCleanupCommands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@ 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",
});
it("only lints and formats when bin is not enabled and there are no prepended commands", () => {
const actual = createCleanupCommands(undefined);

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",
});
it("runs prepended commands before it lints and formats when bin is not enabled", () => {
const actual = createCleanupCommands(undefined, "prepended");

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

it("runs prepended commands before it builds, lints, and formats when bin is not enabled", () => {
const actual = createCleanupCommands("bin/index.js", "prepended");

expect(actual).toEqual([
"prepended",
"pnpm build",
"pnpm lint --fix",
"pnpm format --write",
Expand Down
13 changes: 5 additions & 8 deletions src/shared/createCleanupCommands.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Options } from "./types.js";

export function createCleanupCommands({
bin,
mode,
}: Pick<Options, "bin" | "mode">) {
export function createCleanupCommands(
bin: string | undefined,
...prependedCommands: string[]
) {
return [
// There's no need to dedupe when initializing from the fixed template
...(mode === "initialize" ? [] : ["pnpm dedupe --offline"]),
...prependedCommands,
// n/no-missing-import rightfully reports on a missing the bin .js file
...(bin ? ["pnpm build"] : []),
"pnpm lint --fix",
Expand Down
7 changes: 7 additions & 0 deletions src/steps/clearLocalGitTags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { execaCommand } from "execa";

export async function clearLocalGitTags() {
const tags = await execaCommand("git tag -l");

await execaCommand(`git tag -d ${tags.stdout.replaceAll("\n", " ")}`);
}

0 comments on commit 95cebe1

Please sign in to comment.