diff --git a/src/shared/options/ensureRepositoryExists.test.ts b/src/shared/options/ensureRepositoryExists.test.ts index 6e33f1e28..0e86ebfaf 100644 --- a/src/shared/options/ensureRepositoryExists.test.ts +++ b/src/shared/options/ensureRepositoryExists.test.ts @@ -38,6 +38,7 @@ const createMockOctokit = () => describe("ensureRepositoryExists", () => { it("returns the repository when octokit is undefined", async () => { const actual = await ensureRepositoryExists(undefined, { + mode: "initialize", owner, repository, }); @@ -51,6 +52,7 @@ describe("ensureRepositoryExists", () => { const actual = await ensureRepositoryExists( { auth, octokit }, { + mode: "initialize", owner, repository, }, @@ -67,10 +69,7 @@ describe("ensureRepositoryExists", () => { const actual = await ensureRepositoryExists( { auth, octokit }, - { - owner, - repository, - }, + { mode: "initialize", owner, repository }, ); expect(actual).toEqual({ github: { auth, octokit }, repository }); @@ -82,6 +81,26 @@ describe("ensureRepositoryExists", () => { }); }); + it("defaults to creating a repository when mode is 'create'", async () => { + const octokit = createMockOctokit(); + + mockDoesRepositoryExist.mockResolvedValue(false); + + const actual = await ensureRepositoryExists( + { auth, octokit }, + { mode: "create", owner, repository }, + ); + + expect(actual).toEqual({ github: { auth, octokit }, repository }); + expect(octokit.rest.repos.createUsingTemplate).toHaveBeenCalledWith({ + name: repository, + owner, + template_owner: "JoshuaKGoldberg", + template_repo: "create-typescript-app", + }); + expect(mockSelect).not.toHaveBeenCalled(); + }); + it("returns the second repository when the prompt is 'different', the first repository does not exist, and the second repository exists", async () => { const octokit = createMockOctokit(); const newRepository = "new-repository"; @@ -94,10 +113,7 @@ describe("ensureRepositoryExists", () => { const actual = await ensureRepositoryExists( { auth, octokit }, - { - owner, - repository, - }, + { mode: "initialize", owner, repository }, ); expect(actual).toEqual({ @@ -119,10 +135,7 @@ describe("ensureRepositoryExists", () => { const actual = await ensureRepositoryExists( { auth, octokit }, - { - owner, - repository, - }, + { mode: "initialize", owner, repository }, ); expect(actual).toEqual({ @@ -145,10 +158,7 @@ describe("ensureRepositoryExists", () => { const actual = await ensureRepositoryExists( { auth, octokit }, - { - owner, - repository, - }, + { mode: "initialize", owner, repository }, ); expect(actual).toEqual({ octokit: undefined, repository }); diff --git a/src/shared/options/ensureRepositoryExists.ts b/src/shared/options/ensureRepositoryExists.ts index f3d8f512b..2914c0afa 100644 --- a/src/shared/options/ensureRepositoryExists.ts +++ b/src/shared/options/ensureRepositoryExists.ts @@ -7,7 +7,7 @@ import { GitHub } from "./getGitHub.js"; export type EnsureRepositoryExistsOptions = Pick< Options, - "owner" | "repository" + "mode" | "owner" | "repository" >; export interface RepositoryExistsResult { @@ -19,7 +19,9 @@ export async function ensureRepositoryExists( github: GitHub | undefined, options: EnsureRepositoryExistsOptions, ): Promise> { + // We'll only respect input options once before prompting for them let { repository } = options; + let createRepository = options.mode === "create"; // We'll continuously pester the user for a repository // until they bail, create a new one, or it exists. @@ -28,23 +30,27 @@ export async function ensureRepositoryExists( return { github, repository }; } - const selection = filterPromptCancel( - (await prompts.select({ - message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`, - options: [ - { label: "Create a new repository", value: "create" }, - { - label: "Switch to a different repository name", - value: "different", - }, - { - label: "Keep changes local", - value: "local", - }, - { label: "Bail out and maybe try again later", value: "bail" }, - ], - })) as "bail" | "create" | "different" | "local", - ); + const selection = createRepository + ? "create" + : filterPromptCancel( + (await prompts.select({ + message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`, + options: [ + { label: "Create a new repository", value: "create" }, + { + label: "Switch to a different repository name", + value: "different", + }, + { + label: "Keep changes local", + value: "local", + }, + { label: "Bail out and maybe try again later", value: "bail" }, + ], + })) as "bail" | "create" | "different" | "local", + ); + + createRepository = false; switch (selection) { case undefined: diff --git a/src/shared/options/readOptions.ts b/src/shared/options/readOptions.ts index bbb011d35..d2fc9904d 100644 --- a/src/shared/options/readOptions.ts +++ b/src/shared/options/readOptions.ts @@ -137,6 +137,7 @@ export async function readOptions( ? undefined : await withSpinner("Checking GitHub authentication", getGitHub), { + mode, owner: options.owner, repository: options.repository, },