From 42c6ebd4866c36884c34a439553a736008964a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Tue, 24 Dec 2024 13:08:17 -0500 Subject: [PATCH] fix: don't suggest redundant --directory (#1805) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR Checklist - [x] Addresses an existing open issue: fixes #1126 - [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 💖 --- .../createRerunDirectorySuggestion.test.ts | 48 +++++++++++++++++++ src/create/createRerunDirectorySuggestion.ts | 7 +++ src/create/createRerunSuggestion.test.ts | 30 ++++++++++-- src/create/createRerunSuggestion.ts | 2 + 4 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/create/createRerunDirectorySuggestion.test.ts create mode 100644 src/create/createRerunDirectorySuggestion.ts diff --git a/src/create/createRerunDirectorySuggestion.test.ts b/src/create/createRerunDirectorySuggestion.test.ts new file mode 100644 index 000000000..9b4798348 --- /dev/null +++ b/src/create/createRerunDirectorySuggestion.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it } from "vitest"; + +import { createRerunDirectorySuggestion } from "./createRerunDirectorySuggestion.js"; + +const directory = "test-directory"; +const repository = "test-repository"; + +describe("createRerunDirectorySuggestion", () => { + it("returns undefined when mode is create and directory matches repository", () => { + const suggestion = createRerunDirectorySuggestion({ + directory: repository, + mode: "create", + repository, + }); + + expect(suggestion).toBe(undefined); + }); + + it("returns directory when mode is create and directory doesn't match repository", () => { + const suggestion = createRerunDirectorySuggestion({ + directory, + mode: "create", + repository, + }); + + expect(suggestion).toBe(directory); + }); + + it("returns undefined when mode is initialize and directory is .", () => { + const suggestion = createRerunDirectorySuggestion({ + directory: ".", + mode: "initialize", + repository, + }); + + expect(suggestion).toBe(undefined); + }); + + it("returns directory when mode is initialize and directory is not .", () => { + const suggestion = createRerunDirectorySuggestion({ + directory, + mode: "initialize", + repository, + }); + + expect(suggestion).toBe(directory); + }); +}); diff --git a/src/create/createRerunDirectorySuggestion.ts b/src/create/createRerunDirectorySuggestion.ts new file mode 100644 index 000000000..307299423 --- /dev/null +++ b/src/create/createRerunDirectorySuggestion.ts @@ -0,0 +1,7 @@ +import { Options } from "../shared/types.js"; + +export function createRerunDirectorySuggestion(options: Partial) { + const defaultValue = options.mode === "create" ? options.repository : "."; + + return options.directory === defaultValue ? undefined : options.directory; +} diff --git a/src/create/createRerunSuggestion.test.ts b/src/create/createRerunSuggestion.test.ts index 9854ca3c2..856d55c9c 100644 --- a/src/create/createRerunSuggestion.test.ts +++ b/src/create/createRerunSuggestion.test.ts @@ -89,7 +89,7 @@ describe("createRerunSuggestion", () => { }); expect(actual).toMatchInlineSnapshot( - `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --directory . --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --guide https://example.com --guide-title "Test Title" --keywords "abc def ghi jkl mno pqr" --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, + `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --guide https://example.com --guide-title "Test Title" --keywords "abc def ghi jkl mno pqr" --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, ); }); @@ -104,7 +104,31 @@ describe("createRerunSuggestion", () => { }); expect(actual).toMatchInlineSnapshot( - `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --directory . --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --keywords "abc def ghi jkl mno pqr" --logo test/src.png --logo-alt "Test alt." --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, + `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --keywords "abc def ghi jkl mno pqr" --logo test/src.png --logo-alt "Test alt." --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, + ); + }); + + it("does not include directory when it is repository and the mode is create", () => { + const actual = createRerunSuggestion({ + ...options, + directory: options.repository, + mode: "create", + }); + + expect(actual).toMatchInlineSnapshot( + `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --keywords "abc def ghi jkl mno pqr" --mode create --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, + ); + }); + + it("includes directory when it is repository and the mode is migrate", () => { + const actual = createRerunSuggestion({ + ...options, + directory: options.repository, + mode: "migrate", + }); + + expect(actual).toMatchInlineSnapshot( + `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --directory test-repository --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-package-json --exclude-lint-perfectionist --keywords "abc def ghi jkl mno pqr" --mode migrate --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, ); }); @@ -118,7 +142,7 @@ describe("createRerunSuggestion", () => { }); expect(actual).toMatchInlineSnapshot( - `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --directory . --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-md --exclude-lint-package-json --exclude-lint-perfectionist --exclude-lint-spelling --keywords "abc def ghi jkl mno pqr" --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, + `"npx create-typescript-app --base everything --author TestAuthor --description "Test description." --email-github github@email.com --email-npm npm@email.com --exclude-all-contributors --exclude-compliance --exclude-lint-jsdoc --exclude-lint-json --exclude-lint-knip --exclude-lint-md --exclude-lint-package-json --exclude-lint-perfectionist --exclude-lint-spelling --keywords "abc def ghi jkl mno pqr" --mode initialize --owner TestOwner --repository test-repository --skip-github-api --skip-install --skip-removal --title "Test Title""`, ); }); diff --git a/src/create/createRerunSuggestion.ts b/src/create/createRerunSuggestion.ts index 33fe3aadd..0ffb97865 100644 --- a/src/create/createRerunSuggestion.ts +++ b/src/create/createRerunSuggestion.ts @@ -4,6 +4,7 @@ import { getExclusions, } from "../shared/options/exclusionKeys.js"; import { Options } from "../shared/types.js"; +import { createRerunDirectorySuggestion } from "./createRerunDirectorySuggestion.js"; function getFirstMatchingArg(key: string) { return Object.keys(allArgOptions).find( @@ -37,6 +38,7 @@ export function createRerunSuggestion(options: Partial): string { skipAllContributorsApi: undefined, skipGitHubApi: undefined, }), + directory: createRerunDirectorySuggestion(options), }; const args = Object.entries(optionsNormalized)