-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: smarter initial mode prompt (#919)
## PR Checklist - [x] Addresses an existing open issue: fixes #884 - [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 Enhances `promptForMode` to give different options based on the current directory: * If it's empty, offer to `create` a new repository in it or a child directory * If it's a Git directory, offer to `initialize` or `migrate` * If it's not a Git directory, runs `create` for a new repository in a child directory In doing so, adds an optional `--directory` that defaults to the repository's name. Also cleans up `getPrefillOrPromptedOption` a bit. Instead of allowing an `existingValue` parameter, calls to `getPrefillOrPromptedOption` are just put in the right-hand-side of a `??`.
- Loading branch information
1 parent
1a69169
commit 001882b
Showing
38 changed files
with
500 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import chalk from "chalk"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { promptForMode } from "./promptForMode.js"; | ||
|
||
const mockSelect = vi.fn(); | ||
|
||
vi.mock("@clack/prompts", () => ({ | ||
isCancel: () => false, | ||
get select() { | ||
return mockSelect; | ||
}, | ||
})); | ||
|
||
const mockReaddir = vi.fn(); | ||
|
||
vi.mock("node:fs/promises", () => ({ | ||
get readdir() { | ||
return mockReaddir; | ||
}, | ||
})); | ||
|
||
const mockCwd = vi.fn(); | ||
|
||
vi.mock("node:process", () => ({ | ||
get cwd() { | ||
return mockCwd; | ||
}, | ||
})); | ||
|
||
const mockLogLine = vi.fn(); | ||
|
||
vi.mock("../shared/cli/lines.js", () => ({ | ||
get logLine() { | ||
return mockLogLine; | ||
}, | ||
})); | ||
describe("promptForMode", () => { | ||
it("returns an error when the input exists and is not a mode", async () => { | ||
const mode = await promptForMode("other"); | ||
|
||
expect(mode).toMatchInlineSnapshot( | ||
` | ||
{ | ||
"mode": [Error: Unknown --mode: other. Allowed modes are: create, initialize, migrate.], | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it("returns the input when it is a mode", async () => { | ||
const input = "create"; | ||
|
||
const mode = await promptForMode(input); | ||
|
||
expect(mode).toEqual({ mode: input }); | ||
}); | ||
|
||
it("returns creating in the current directory when the current directory is empty and the user selects create-current", async () => { | ||
mockSelect.mockResolvedValueOnce("create-current"); | ||
const directory = "test-directory"; | ||
|
||
mockReaddir.mockResolvedValueOnce([]); | ||
mockCwd.mockReturnValueOnce(`/path/to/${directory}`); | ||
|
||
const actual = await promptForMode(undefined); | ||
|
||
expect(actual).toEqual({ | ||
mode: "create", | ||
options: { directory: ".", repository: directory }, | ||
}); | ||
expect(mockLogLine).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("returns creating in a child directory when the current directory is empty and the user selects create-child", async () => { | ||
mockSelect.mockResolvedValueOnce("create-child"); | ||
const directory = "test-directory"; | ||
|
||
mockReaddir.mockResolvedValueOnce([]); | ||
mockCwd.mockReturnValueOnce(`/path/to/${directory}`); | ||
|
||
const actual = await promptForMode(undefined); | ||
|
||
expect(actual).toEqual({ | ||
mode: "create", | ||
}); | ||
expect(mockLogLine).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("returns the user selection when the current directory is a Git directory", async () => { | ||
const mode = "initialize"; | ||
mockSelect.mockResolvedValueOnce(mode); | ||
|
||
mockReaddir.mockResolvedValueOnce([".git"]); | ||
|
||
const actual = await promptForMode(undefined); | ||
|
||
expect(actual).toEqual({ mode }); | ||
expect(mockLogLine).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("returns create without prompting when the current directory contains children but is not a Git directory", async () => { | ||
const mode = "create"; | ||
|
||
mockReaddir.mockResolvedValueOnce(["file"]); | ||
|
||
const actual = await promptForMode(undefined); | ||
|
||
expect(actual).toEqual({ mode }); | ||
expect(mockSelect).not.toHaveBeenCalled(); | ||
expect(mockLogLine).toHaveBeenCalledWith( | ||
chalk.gray( | ||
"Defaulting to --mode create because the directory contains children and isn't a Git repository.", | ||
), | ||
); | ||
}); | ||
}); |
Oops, something went wrong.