diff --git a/.github/workflows/lint-knip.yml b/.github/workflows/lint-knip.yml index df3f955b7..781d52ef1 100644 --- a/.github/workflows/lint-knip.yml +++ b/.github/workflows/lint-knip.yml @@ -4,7 +4,6 @@ jobs: steps: - uses: actions/checkout@v4 - uses: ./.github/actions/prepare - - run: pnpm build || true - run: pnpm lint:knip name: Lint Knip diff --git a/script/__snapshots__/migrate-test-e2e.js.snap b/script/__snapshots__/migrate-test-e2e.js.snap index aa15dcd39..0d171bfe5 100644 --- a/script/__snapshots__/migrate-test-e2e.js.snap +++ b/script/__snapshots__/migrate-test-e2e.js.snap @@ -50,19 +50,6 @@ exports[`expected file changes > .eslintrc.cjs 1`] = ` {" `; -exports[`expected file changes > .github/workflows/lint-knip.yml 1`] = ` -"--- a/.github/workflows/lint-knip.yml -+++ b/.github/workflows/lint-knip.yml -@@ ... @@ jobs: - steps: - - uses: actions/checkout@v4 - - uses: ./.github/actions/prepare -- - run: pnpm build || true - - run: pnpm lint:knip - - name: Lint Knip" -`; - exports[`expected file changes > .github/workflows/test.yml 1`] = ` "--- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml diff --git a/script/migrate-test-e2e.js b/script/migrate-test-e2e.js index 773231c85..d7527c1c5 100644 --- a/script/migrate-test-e2e.js +++ b/script/migrate-test-e2e.js @@ -11,7 +11,6 @@ const filesExpectedToBeChanged = [ "package.json", ".eslintignore", ".eslintrc.cjs", - ".github/workflows/lint-knip.yml", ".github/workflows/test.yml", ".gitignore", ".prettierignore", diff --git a/src/create/createWithOptions.ts b/src/create/createWithOptions.ts index 8dc5caa80..8c03d0c8d 100644 --- a/src/create/createWithOptions.ts +++ b/src/create/createWithOptions.ts @@ -1,6 +1,7 @@ import { $ } from "execa"; import { withSpinner, withSpinners } from "../shared/cli/spinners.js"; +import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js"; import { doesRepositoryExist } from "../shared/doesRepositoryExist.js"; import { GitHubAndOptions } from "../shared/options/readOptions.js"; import { addToolAllContributors } from "../steps/addToolAllContributors.js"; @@ -37,11 +38,13 @@ export async function createWithOptions({ github, options }: GitHubAndOptions) { finalizeDependencies(options), ); - await runCommands("Cleaning up files", [ - "pnpm dedupe", - "pnpm lint --fix", - "pnpm format --write", - ]); + await runCommands( + "Cleaning up files", + createCleanUpFilesCommands({ + bin: !!options.bin, + dedupe: true, + }), + ); } const sendToGitHub = diff --git a/src/initialize/initializeWithOptions.ts b/src/initialize/initializeWithOptions.ts index 960c2c329..127fb6b14 100644 --- a/src/initialize/initializeWithOptions.ts +++ b/src/initialize/initializeWithOptions.ts @@ -1,4 +1,5 @@ import { withSpinner, withSpinners } from "../shared/cli/spinners.js"; +import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js"; import { GitHubAndOptions } from "../shared/options/readOptions.js"; import { addOwnerAsAllContributor } from "../steps/addOwnerAsAllContributor.js"; import { clearChangelog } from "../steps/clearChangelog.js"; @@ -60,8 +61,10 @@ export async function initializeWithOptions({ ); } - await runCommands("Cleaning up files", [ - "pnpm lint --fix", - "pnpm format --write", - ]); + await runCommands( + "Cleaning up files", + createCleanUpFilesCommands({ + bin: !!options.bin, + }), + ); } diff --git a/src/migrate/migrateWithOptions.ts b/src/migrate/migrateWithOptions.ts index 01e4a3291..04b769565 100644 --- a/src/migrate/migrateWithOptions.ts +++ b/src/migrate/migrateWithOptions.ts @@ -1,4 +1,5 @@ import { withSpinner, withSpinners } from "../shared/cli/spinners.js"; +import { createCleanUpFilesCommands } from "../shared/createCleanUpFilesCommands.js"; import { GitHubAndOptions } from "../shared/options/readOptions.js"; import { clearUnnecessaryFiles } from "../steps/clearUnnecessaryFiles.js"; import { detectExistingContributors } from "../steps/detectExistingContributors.js"; @@ -60,8 +61,11 @@ export async function migrateWithOptions({ ); } - await runCommands("Cleaning up files", [ - "pnpm lint --fix", - "pnpm format --write", - ]); + await runCommands( + "Cleaning up files", + createCleanUpFilesCommands({ + bin: !!options.bin, + dedupe: true, + }), + ); } diff --git a/src/shared/createCleanUpFilesCommands.test.ts b/src/shared/createCleanUpFilesCommands.test.ts new file mode 100644 index 000000000..6dafc83a8 --- /dev/null +++ b/src/shared/createCleanUpFilesCommands.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; + +import { createCleanUpFilesCommands } from "./createCleanUpFilesCommands.js"; + +describe("createCleanUpFilesCommands", () => { + it("only lints and formats when no options are specified", () => { + const actual = createCleanUpFilesCommands({}); + + expect(actual).toEqual(["pnpm lint --fix", "pnpm format --write"]); + }); + + it("runs dedupe and build before it lints and formats when both options are specified", () => { + const actual = createCleanUpFilesCommands({ + bin: true, + dedupe: true, + }); + + expect(actual).toEqual([ + "pnpm dedupe", + "pnpm build || exit 0", + "pnpm lint --fix", + "pnpm format --write", + ]); + }); +}); diff --git a/src/shared/createCleanUpFilesCommands.ts b/src/shared/createCleanUpFilesCommands.ts new file mode 100644 index 000000000..b13033b48 --- /dev/null +++ b/src/shared/createCleanUpFilesCommands.ts @@ -0,0 +1,18 @@ +export interface CleanUpFilesOptions { + bin?: boolean; + dedupe?: boolean; +} + +export function createCleanUpFilesCommands({ + bin, + dedupe, +}: CleanUpFilesOptions) { + return [ + // There's no need to dedupe when initializing from the fixed template + ...(dedupe ? ["pnpm dedupe"] : []), + // n/no-missing-import rightfully reports on a missing the bin .js file + ...(bin ? ["pnpm build || exit 0"] : []), + "pnpm lint --fix", + "pnpm format --write", + ]; +} diff --git a/src/steps/writing/creation/dotGitHub/createWorkflows.test.ts b/src/steps/writing/creation/dotGitHub/createWorkflows.test.ts index 4ee3242d7..da37f2444 100644 --- a/src/steps/writing/creation/dotGitHub/createWorkflows.test.ts +++ b/src/steps/writing/creation/dotGitHub/createWorkflows.test.ts @@ -7,6 +7,7 @@ const createOptions = (exclude: boolean) => ({ access: "public", base: "everything", + bin: exclude ? undefined : "./bin/index.js", description: "Test description.", directory: ".", email: { @@ -413,7 +414,6 @@ describe("createWorkflows", () => { steps: - uses: actions/checkout@v4 - uses: ./.github/actions/prepare - - run: pnpm build || true - run: pnpm lint name: Lint diff --git a/src/steps/writing/creation/dotGitHub/createWorkflows.ts b/src/steps/writing/creation/dotGitHub/createWorkflows.ts index 150604898..d97027d00 100644 --- a/src/steps/writing/creation/dotGitHub/createWorkflows.ts +++ b/src/steps/writing/creation/dotGitHub/createWorkflows.ts @@ -114,7 +114,7 @@ export function createWorkflows(options: Options) { }), "lint.yml": createWorkflowFile({ name: "Lint", - runs: ["pnpm build || true", "pnpm lint"], + runs: [...(options.bin ? ["pnpm build || true"] : []), "pnpm lint"], }), ...(!options.excludeLintKnip && { "lint-knip.yml": createWorkflowFile({