Skip to content

Commit ef2e348

Browse files
fix: correct create-repository-by-default logic (#922)
## PR Checklist - [x] Addresses an existing open issue: fixes #920 (but better) - [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 I remember now why `--create-repository` existed: if `mode` is `create`, then we'll want to attempt creating a new repo by default. So this PR infers a `createRepository` again in `ensureRepositoryExists`.
1 parent 0b542be commit ef2e348

File tree

3 files changed

+51
-34
lines changed

3 files changed

+51
-34
lines changed

src/shared/options/ensureRepositoryExists.test.ts

+26-16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const createMockOctokit = () =>
3838
describe("ensureRepositoryExists", () => {
3939
it("returns the repository when octokit is undefined", async () => {
4040
const actual = await ensureRepositoryExists(undefined, {
41+
mode: "initialize",
4142
owner,
4243
repository,
4344
});
@@ -51,6 +52,7 @@ describe("ensureRepositoryExists", () => {
5152
const actual = await ensureRepositoryExists(
5253
{ auth, octokit },
5354
{
55+
mode: "initialize",
5456
owner,
5557
repository,
5658
},
@@ -67,10 +69,7 @@ describe("ensureRepositoryExists", () => {
6769

6870
const actual = await ensureRepositoryExists(
6971
{ auth, octokit },
70-
{
71-
owner,
72-
repository,
73-
},
72+
{ mode: "initialize", owner, repository },
7473
);
7574

7675
expect(actual).toEqual({ github: { auth, octokit }, repository });
@@ -82,6 +81,26 @@ describe("ensureRepositoryExists", () => {
8281
});
8382
});
8483

84+
it("defaults to creating a repository when mode is 'create'", async () => {
85+
const octokit = createMockOctokit();
86+
87+
mockDoesRepositoryExist.mockResolvedValue(false);
88+
89+
const actual = await ensureRepositoryExists(
90+
{ auth, octokit },
91+
{ mode: "create", owner, repository },
92+
);
93+
94+
expect(actual).toEqual({ github: { auth, octokit }, repository });
95+
expect(octokit.rest.repos.createUsingTemplate).toHaveBeenCalledWith({
96+
name: repository,
97+
owner,
98+
template_owner: "JoshuaKGoldberg",
99+
template_repo: "create-typescript-app",
100+
});
101+
expect(mockSelect).not.toHaveBeenCalled();
102+
});
103+
85104
it("returns the second repository when the prompt is 'different', the first repository does not exist, and the second repository exists", async () => {
86105
const octokit = createMockOctokit();
87106
const newRepository = "new-repository";
@@ -94,10 +113,7 @@ describe("ensureRepositoryExists", () => {
94113

95114
const actual = await ensureRepositoryExists(
96115
{ auth, octokit },
97-
{
98-
owner,
99-
repository,
100-
},
116+
{ mode: "initialize", owner, repository },
101117
);
102118

103119
expect(actual).toEqual({
@@ -119,10 +135,7 @@ describe("ensureRepositoryExists", () => {
119135

120136
const actual = await ensureRepositoryExists(
121137
{ auth, octokit },
122-
{
123-
owner,
124-
repository,
125-
},
138+
{ mode: "initialize", owner, repository },
126139
);
127140

128141
expect(actual).toEqual({
@@ -145,10 +158,7 @@ describe("ensureRepositoryExists", () => {
145158

146159
const actual = await ensureRepositoryExists(
147160
{ auth, octokit },
148-
{
149-
owner,
150-
repository,
151-
},
161+
{ mode: "initialize", owner, repository },
152162
);
153163

154164
expect(actual).toEqual({ octokit: undefined, repository });

src/shared/options/ensureRepositoryExists.ts

+24-18
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GitHub } from "./getGitHub.js";
77

88
export type EnsureRepositoryExistsOptions = Pick<
99
Options,
10-
"owner" | "repository"
10+
"mode" | "owner" | "repository"
1111
>;
1212

1313
export interface RepositoryExistsResult {
@@ -19,7 +19,9 @@ export async function ensureRepositoryExists(
1919
github: GitHub | undefined,
2020
options: EnsureRepositoryExistsOptions,
2121
): Promise<Partial<RepositoryExistsResult>> {
22+
// We'll only respect input options once before prompting for them
2223
let { repository } = options;
24+
let createRepository = options.mode === "create";
2325

2426
// We'll continuously pester the user for a repository
2527
// until they bail, create a new one, or it exists.
@@ -28,23 +30,27 @@ export async function ensureRepositoryExists(
2830
return { github, repository };
2931
}
3032

31-
const selection = filterPromptCancel(
32-
(await prompts.select({
33-
message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`,
34-
options: [
35-
{ label: "Create a new repository", value: "create" },
36-
{
37-
label: "Switch to a different repository name",
38-
value: "different",
39-
},
40-
{
41-
label: "Keep changes local",
42-
value: "local",
43-
},
44-
{ label: "Bail out and maybe try again later", value: "bail" },
45-
],
46-
})) as "bail" | "create" | "different" | "local",
47-
);
33+
const selection = createRepository
34+
? "create"
35+
: filterPromptCancel(
36+
(await prompts.select({
37+
message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`,
38+
options: [
39+
{ label: "Create a new repository", value: "create" },
40+
{
41+
label: "Switch to a different repository name",
42+
value: "different",
43+
},
44+
{
45+
label: "Keep changes local",
46+
value: "local",
47+
},
48+
{ label: "Bail out and maybe try again later", value: "bail" },
49+
],
50+
})) as "bail" | "create" | "different" | "local",
51+
);
52+
53+
createRepository = false;
4854

4955
switch (selection) {
5056
case undefined:

src/shared/options/readOptions.ts

+1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export async function readOptions(
137137
? undefined
138138
: await withSpinner("Checking GitHub authentication", getGitHub),
139139
{
140+
mode,
140141
owner: options.owner,
141142
repository: options.repository,
142143
},

0 commit comments

Comments
 (0)