Skip to content

Commit

Permalink
Add SRC to createTheme endopint
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jul 8, 2024
1 parent 7c6e387 commit c203335
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
32 changes: 30 additions & 2 deletions packages/app/src/cli/utilities/host-theme-manager-next.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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',
Expand Down
11 changes: 5 additions & 6 deletions packages/app/src/cli/utilities/host-theme-manager-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions packages/cli-kit/src/public/node/themes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pick<Theme, 'name' | 'role' | 'processing'>>
export type ThemeParams = Partial<Pick<Theme, 'name' | 'role' | 'processing' | 'src'>>
export type AssetParams = Pick<ThemeAsset, 'key'> & Partial<Pick<ThemeAsset, 'value' | 'attachment'>>

export async function fetchTheme(id: number, session: AdminSession): Promise<Theme | undefined> {
Expand All @@ -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<Theme | undefined> {
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<ThemeAsset | undefined> {
const response = await request('GET', `/themes/${id}/assets`, session, undefined, {
'asset[key]': key,
Expand Down
5 changes: 5 additions & 0 deletions packages/cli-kit/src/public/node/themes/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down

0 comments on commit c203335

Please sign in to comment.