From c2033356d50b3c136d5709c9843c06c6c985b157 Mon Sep 17 00:00:00 2001 From: James Meng Date: Mon, 8 Jul 2024 15:03:53 -0700 Subject: [PATCH] Add SRC to createTheme endopint --- .../utilities/host-theme-manager-next.test.ts | 32 +++++++++++++++++-- .../cli/utilities/host-theme-manager-next.ts | 11 +++---- .../cli-kit/src/public/node/themes/api.ts | 11 +------ .../cli-kit/src/public/node/themes/types.ts | 5 +++ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/packages/app/src/cli/utilities/host-theme-manager-next.test.ts b/packages/app/src/cli/utilities/host-theme-manager-next.test.ts index d9319587ae..5994fa4227 100644 --- a/packages/app/src/cli/utilities/host-theme-manager-next.test.ts +++ b/packages/app/src/cli/utilities/host-theme-manager-next.test.ts @@ -1,8 +1,9 @@ import {HostThemeManager} from './host-theme-manager-next.js' import {waitForThemeToBeProcessed} from './host-theme-watcher.js' -import {createHostTheme} from '@shopify/cli-kit/node/themes/api' +import {createTheme} from '@shopify/cli-kit/node/themes/api' import {AdminSession} from '@shopify/cli-kit/node/session' import {beforeEach, describe, expect, test, vi} from 'vitest' +import {DEVELOPMENT_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils' vi.mock('@shopify/cli-kit/node/themes/api') vi.mock('./host-theme-watcher.js') @@ -16,9 +17,36 @@ describe('HostThemeManager', () => { }) describe('create', () => { + test('should call createTheme with the provided name and src param', async () => { + // Given + vi.mocked(createTheme).mockResolvedValue({ + id: 12345, + name: 'Theme', + role: 'development', + createdAtRuntime: true, + processing: true, + }) + + // When + await themeManager.create( + DEVELOPMENT_THEME_ROLE, + 'App Ext. Host Name', + 'https://cdn.shopify.com/theme-store/test_url.jpg', + ) + + // Then + expect(createTheme).toHaveBeenCalledWith( + { + name: 'App Ext. Host Name', + role: DEVELOPMENT_THEME_ROLE, + src: 'https://cdn.shopify.com/theme-store/test_url.jpg', + }, + {storeFqdn: 'storeFqdn', token: 'token'}, + ) + }) test('should wait for the theme to be processed', async () => { // Given - vi.mocked(createHostTheme).mockResolvedValue({ + vi.mocked(createTheme).mockResolvedValue({ id: 12345, name: 'Theme', role: 'development', diff --git a/packages/app/src/cli/utilities/host-theme-manager-next.ts b/packages/app/src/cli/utilities/host-theme-manager-next.ts index fddc6dd439..d51b75337b 100644 --- a/packages/app/src/cli/utilities/host-theme-manager-next.ts +++ b/packages/app/src/cli/utilities/host-theme-manager-next.ts @@ -2,11 +2,12 @@ import {waitForThemeToBeProcessed} from './host-theme-watcher.js' import {getHostTheme, removeHostTheme, setHostTheme} from '@shopify/cli-kit/node/themes/conf' import {ThemeManager} from '@shopify/cli-kit/node/themes/theme-manager' import {AdminSession} from '@shopify/cli-kit/node/session' -import {createHostTheme} from '@shopify/cli-kit/node/themes/api' import {BugError} from '@shopify/cli-kit/node/error' import {DEVELOPMENT_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils' import {generateThemeName} from '@shopify/cli-kit/node/themes/generate-theme-name' +import {createTheme} from '@shopify/cli-kit/node/themes/api' +const DEFAULT_THEME_ZIP = 'https://codeload.github.com/Shopify/dawn/zip/refs/tags/v15.0.0' export class HostThemeManager extends ThemeManager { protected context = 'App Ext. Host' @@ -15,12 +16,10 @@ export class HostThemeManager extends ThemeManager { this.themeId = getHostTheme(adminSession.storeFqdn) } - async create(themeRole = DEVELOPMENT_THEME_ROLE, themeName?: string) { - const name = themeName || generateThemeName(this.context) - const src = 'https://cdn.shopify.com/theme-store/uhrdefhlndzaoyrgylhto59sx2i7.jpg' - const theme = await createHostTheme({name, role: themeRole}, src, this.adminSession) + async create(role = DEVELOPMENT_THEME_ROLE, name = generateThemeName(this.context), src = DEFAULT_THEME_ZIP) { + const theme = await createTheme({name, role, src}, this.adminSession) if (!theme) { - throw new BugError(`Could not create theme with name "${name}" and role "${themeRole}"`) + throw new BugError(`Could not create theme with name "${name}" and role "${role}"`) } this.setTheme(theme.id.toString()) await waitForThemeToBeProcessed(theme.id, this.adminSession) diff --git a/packages/cli-kit/src/public/node/themes/api.ts b/packages/cli-kit/src/public/node/themes/api.ts index b307c92f35..7ba25e9dbd 100644 --- a/packages/cli-kit/src/public/node/themes/api.ts +++ b/packages/cli-kit/src/public/node/themes/api.ts @@ -13,7 +13,7 @@ import { } from '@shopify/cli-kit/node/themes/factories' import {Result, Checksum, Key, Theme, ThemeAsset} from '@shopify/cli-kit/node/themes/types' -export type ThemeParams = Partial> +export type ThemeParams = Partial> export type AssetParams = Pick & Partial> export async function fetchTheme(id: number, session: AdminSession): Promise { @@ -33,15 +33,6 @@ export async function createTheme(params: ThemeParams, session: AdminSession): P return buildTheme({...response.json.theme, createdAtRuntime: true}) } -export async function createHostTheme( - params: ThemeParams, - src: string, - session: AdminSession, -): Promise { - const response = await request('POST', '/themes', session, {theme: {...params, src}}) - return buildTheme({...response.json.theme, createdAtRuntime: true}) -} - export async function fetchThemeAsset(id: number, key: Key, session: AdminSession): Promise { const response = await request('GET', `/themes/${id}/assets`, session, undefined, { 'asset[key]': key, diff --git a/packages/cli-kit/src/public/node/themes/types.ts b/packages/cli-kit/src/public/node/themes/types.ts index 60b2010862..1f913f34a0 100644 --- a/packages/cli-kit/src/public/node/themes/types.ts +++ b/packages/cli-kit/src/public/node/themes/types.ts @@ -69,6 +69,11 @@ export interface Theme { * The remote role of the theme. */ role: string + + /** + * A public URL where Shopify can access the theme code. + */ + src?: string } /**