diff --git a/packages/cli/src/commands/hydrogen/link.test.ts b/packages/cli/src/commands/hydrogen/link.test.ts index 25ee0beffd..c6c244efb9 100644 --- a/packages/cli/src/commands/hydrogen/link.test.ts +++ b/packages/cli/src/commands/hydrogen/link.test.ts @@ -54,9 +54,6 @@ describe('link', () => { storefront: undefined, }; - const expectedStorefrontName = 'New Storefront'; - const expectedJobId = 'gid://shopify/Job/1'; - beforeEach(async () => { vi.mocked(login).mockResolvedValue({ session: ADMIN_SESSION, @@ -72,15 +69,6 @@ describe('link', () => { ]); vi.mocked(renderSelectPrompt).mockResolvedValue(FULL_SHOPIFY_CONFIG.shop); - - vi.mocked(createStorefront).mockResolvedValue({ - storefront: { - id: 'gid://shopify/HydrogenStorefront/1', - title: expectedStorefrontName, - productionUrl: 'https://example.com', - }, - jobId: expectedJobId, - }); }); afterEach(() => { @@ -140,10 +128,20 @@ describe('link', () => { }); describe('when you want to link a new Hydrogen storefront', () => { - + const expectedStorefrontName = 'New Storefront'; + const expectedJobId = 'gid://shopify/Job/1'; beforeEach(async () => { vi.mocked(renderSelectPrompt).mockResolvedValue(null); + + vi.mocked(createStorefront).mockResolvedValue({ + storefront: { + id: 'gid://shopify/HydrogenStorefront/1', + title: expectedStorefrontName, + productionUrl: 'https://example.com', + }, + jobId: expectedJobId, + }); }); it('chooses to create a new storefront given the directory path', async () => { diff --git a/packages/cli/src/commands/hydrogen/link.ts b/packages/cli/src/commands/hydrogen/link.ts index 7c79ebec49..80e1038b6d 100644 --- a/packages/cli/src/commands/hydrogen/link.ts +++ b/packages/cli/src/commands/hydrogen/link.ts @@ -4,6 +4,7 @@ import {basename} from '@shopify/cli-kit/node/path'; import { renderConfirmationPrompt, + renderSelectPrompt, renderSuccess, renderTasks, renderTextPrompt, @@ -20,10 +21,6 @@ import {titleize} from '../../lib/string.js'; import {getCliCommand} from '../../lib/shell.js'; import {login} from '../../lib/auth.js'; import type {AdminSession} from '../../lib/auth.js'; -import { - type HydrogenStorefront, - handleStorefrontSelection, -} from '../../lib/onboarding/common.js'; export default class Link extends Command { static description = @@ -50,6 +47,12 @@ export interface LinkStorefrontArguments { storefront?: string; } +interface HydrogenStorefront { + id: string; + title: string; + productionUrl: string; +} + export async function runLink({ force, path: root = process.cwd(), @@ -131,14 +134,32 @@ export async function linkStorefront( return; } } else { - selectedStorefront = await handleStorefrontSelection(storefronts); + const choices = [ + { + label: 'Create a new storefront', + value: null, + }, + ...storefronts.map(({id, title, productionUrl}) => ({ + label: `${title} (${productionUrl})`, + value: id, + })), + ]; + + const storefrontId = await renderSelectPrompt({ + message: 'Select a Hydrogen storefront to link', + choices, + }); - if (!selectedStorefront) { + if (storefrontId) { + selectedStorefront = storefronts.find(({id}) => id === storefrontId); + } else { selectedStorefront = await createNewStorefront(root, session); } } - await setStorefront(root, selectedStorefront); + if (selectedStorefront) { + await setStorefront(root, selectedStorefront); + } return selectedStorefront; } diff --git a/packages/cli/src/lib/onboarding/common.ts b/packages/cli/src/lib/onboarding/common.ts index d14595df71..a57dfc777e 100644 --- a/packages/cli/src/lib/onboarding/common.ts +++ b/packages/cli/src/lib/onboarding/common.ts @@ -65,7 +65,6 @@ import { renderRoutePrompt, } from '../setups/routes/generate.js'; import {execAsync} from '../process.js'; -import {getStorefronts} from '../graphql/admin/link-storefront.js'; export type InitOptions = { path?: string; @@ -222,7 +221,6 @@ export async function handleCliShortcut( } type StorefrontInfo = { - id?: string; title: string; shop: string; shopName: string; @@ -240,56 +238,13 @@ export async function handleStorefrontLink( const {session, config} = await login(); renderLoginSuccess(config); - const storefronts = await getStorefronts(session); - - let selectedStorefront = await handleStorefrontSelection(storefronts); - - let title; - - if (selectedStorefront) { - title = selectedStorefront.title; - } else { - title = await renderTextPrompt({ - message: 'New storefront name', - defaultValue: titleize(config.shopName), - abortSignal: controller.signal, - }); - } - - return { - ...config, - id: selectedStorefront?.id, - title, - session, - }; -} - -export type HydrogenStorefront = { - id: string; - title: string; - productionUrl: string; -}; - -export async function handleStorefrontSelection( - storefronts: HydrogenStorefront[], -): Promise { - const choices = [ - { - label: 'Create a new storefront', - value: null, - }, - ...storefronts.map(({id, title, productionUrl}) => ({ - label: `${title} (${productionUrl})`, - value: id, - })), - ]; - - const storefrontId = await renderSelectPrompt({ - message: 'Select a Hydrogen storefront to link', - choices, + const title = await renderTextPrompt({ + message: 'New storefront name', + defaultValue: titleize(config.shopName), + abortSignal: controller.signal, }); - return storefronts.find(({id}) => id === storefrontId)!; + return {...config, title, session}; } type Project = { diff --git a/packages/cli/src/lib/onboarding/local.ts b/packages/cli/src/lib/onboarding/local.ts index e5aa32f57b..d7188af75a 100644 --- a/packages/cli/src/lib/onboarding/local.ts +++ b/packages/cli/src/lib/onboarding/local.ts @@ -18,7 +18,6 @@ import { handleLanguage, handleProjectLocation, handleStorefrontLink, - type HydrogenStorefront, type SetupSummary, type InitOptions, handleCliShortcut, @@ -79,7 +78,6 @@ export async function setupLocalStarterTemplate( const createStorefrontPromise = storefrontInfo && - !storefrontInfo.id && createStorefront(storefrontInfo.session, storefrontInfo.title) .then(async ({storefront, jobId}) => { if (jobId) await waitForJob(storefrontInfo.session, jobId); @@ -143,25 +141,17 @@ export async function setupLocalStarterTemplate( '# Run `h2 link` to also inject environment variables from your storefront,\n' + '# or `h2 env pull` to populate this file.'; - let storefrontToLink: {id: string; title: string} | undefined; - - if (storefrontInfo) { + if (storefrontInfo && createStorefrontPromise) { promises.push( // Save linked storefront in project setUserAccount(project.directory, storefrontInfo), + createStorefrontPromise.then((storefront) => + // Save linked storefront in project + setStorefront(project.directory, storefront), + ), // Write empty dotenv file to fallback to remote Oxygen variables writeFile(joinPath(project.directory, '.env'), envLeadingComment), ); - - if (storefrontInfo.id) { - storefrontToLink = {id: storefrontInfo.id, title: storefrontInfo.title}; - } else if (createStorefrontPromise) { - promises.push( - createStorefrontPromise.then((createdStorefront) => { - storefrontToLink = createdStorefront; - }), - ); - } } else if (templateAction === 'mock') { promises.push( // Set required env vars @@ -180,14 +170,7 @@ export async function setupLocalStarterTemplate( ); } - return Promise.all(promises) - .then(() => { - if (storefrontToLink) { - // Save linked storefront in project - setStorefront(project.directory, storefrontToLink); - } - }) - .catch(abort); + return Promise.all(promises).catch(abort); }); const {language, transpileProject} = await handleLanguage(