Skip to content

Commit

Permalink
Revert "Choose to create or link storefront on init" (#1680)
Browse files Browse the repository at this point in the history
This reverts commit 4764eac.
  • Loading branch information
aswamy authored Jan 24, 2024
1 parent 4764eac commit 7d312c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 93 deletions.
24 changes: 11 additions & 13 deletions packages/cli/src/commands/hydrogen/link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(() => {
Expand Down Expand Up @@ -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 () => {
Expand Down
35 changes: 28 additions & 7 deletions packages/cli/src/commands/hydrogen/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {basename} from '@shopify/cli-kit/node/path';

import {
renderConfirmationPrompt,
renderSelectPrompt,
renderSuccess,
renderTasks,
renderTextPrompt,
Expand All @@ -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 =
Expand All @@ -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(),
Expand Down Expand Up @@ -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;
}
Expand Down
55 changes: 5 additions & 50 deletions packages/cli/src/lib/onboarding/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -222,7 +221,6 @@ export async function handleCliShortcut(
}

type StorefrontInfo = {
id?: string;
title: string;
shop: string;
shopName: string;
Expand All @@ -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<HydrogenStorefront | undefined> {
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 = {
Expand Down
29 changes: 6 additions & 23 deletions packages/cli/src/lib/onboarding/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
handleLanguage,
handleProjectLocation,
handleStorefrontLink,
type HydrogenStorefront,
type SetupSummary,
type InitOptions,
handleCliShortcut,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 7d312c6

Please sign in to comment.