Skip to content

Commit 1dbf2cb

Browse files
committed
Fix bug where theme dev doesnt sync images
1 parent c2f96c2 commit 1dbf2cb

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

packages/theme/src/cli/utilities/theme-fs.test.ts

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -710,40 +710,45 @@ describe('theme-fs', () => {
710710
test('returns false if file is not in unsyncedFileKeys', async () => {
711711
// Given
712712
unsyncedFileKeys = new Set()
713-
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)!
713+
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)
714714

715715
// When
716-
const result = await handler('content')
716+
const result = await handler({value: 'content'})
717717

718718
// Then
719719
expect(result).toBe(false)
720720
expect(bulkUploadThemeAssets).not.toHaveBeenCalled()
721721
expect(triggerBrowserFullReload).not.toHaveBeenCalled()
722722
})
723723

724-
test('uploads file and returns true on successful sync', async () => {
725-
// Given
726-
vi.mocked(bulkUploadThemeAssets).mockResolvedValue([
727-
{
728-
key: fileKey,
729-
success: true,
730-
operation: Operation.Upload,
731-
},
732-
])
733-
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)!
734-
735-
// When
736-
const result = await handler('content')
737-
738-
// Then
739-
expect(result).toBe(true)
740-
expect(bulkUploadThemeAssets).toHaveBeenCalledWith(
741-
Number(themeId),
742-
[{key: fileKey, value: 'content'}],
743-
adminSession,
744-
)
745-
expect(unsyncedFileKeys.has(fileKey)).toBe(false)
746-
expect(triggerBrowserFullReload).not.toHaveBeenCalled()
724+
Object.entries({
725+
text: {value: 'content'},
726+
image: {attachment: 'content'},
727+
}).forEach(([fileType, fileContent]) => {
728+
test(`uploads ${fileType} file and returns true on successful sync`, async () => {
729+
// Given
730+
vi.mocked(bulkUploadThemeAssets).mockResolvedValue([
731+
{
732+
key: fileKey,
733+
success: true,
734+
operation: Operation.Upload,
735+
},
736+
])
737+
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)
738+
739+
// When
740+
const result = await handler(fileContent)
741+
742+
// Then
743+
expect(result).toBe(true)
744+
expect(bulkUploadThemeAssets).toHaveBeenCalledWith(
745+
Number(themeId),
746+
[{key: fileKey, ...fileContent}],
747+
adminSession,
748+
)
749+
expect(unsyncedFileKeys.has(fileKey)).toBe(false)
750+
expect(triggerBrowserFullReload).not.toHaveBeenCalled()
751+
})
747752
})
748753

749754
test('throws error and sets uploadErrors on failed sync', async () => {
@@ -757,10 +762,10 @@ describe('theme-fs', () => {
757762
errors: {asset: errors},
758763
},
759764
])
760-
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)!
765+
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)
761766

762767
// When/Then
763-
await expect(handler('content')).rejects.toThrow('{{ broken liquid file')
768+
await expect(handler({value: 'content'})).rejects.toThrow('{{ broken liquid file')
764769
expect(uploadErrors.get(fileKey)).toEqual(errors)
765770
expect(unsyncedFileKeys.has(fileKey)).toBe(true)
766771
expect(triggerBrowserFullReload).toHaveBeenCalledWith(themeId, fileKey)
@@ -776,10 +781,10 @@ describe('theme-fs', () => {
776781
operation: Operation.Upload,
777782
},
778783
])
779-
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)!
784+
const handler = handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)
780785

781786
// When
782-
await handler('content')
787+
await handler({value: 'content'})
783788

784789
// Then
785790
expect(uploadErrors.has(fileKey)).toBe(false)

packages/theme/src/cli/utilities/theme-fs.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,28 @@ export function mountThemeFileSystem(root: string, options?: ThemeFileSystemOpti
139139
const file = files.get(fileKey)
140140

141141
if (!file) {
142-
return ''
142+
return
143143
}
144144

145145
if (file.checksum !== previousChecksum) {
146146
// Sync only if the file has changed
147147
unsyncedFileKeys.add(fileKey)
148148
}
149149

150-
// file.value has a fallback value of '', so we want to ignore this eslint rule
151-
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
152-
return file.value || file.attachment || ''
150+
return {
151+
// file.value has a fallback value of '', so we want to ignore this eslint rule
152+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
153+
value: file.value || undefined,
154+
attachment: file.attachment,
155+
}
153156
})
154157

155158
const syncPromise = contentPromise
156-
.then(handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession))
159+
.then((content) => {
160+
if (!content) return
161+
162+
return handleSyncUpdate(unsyncedFileKeys, uploadErrors, fileKey, themeId, adminSession)(content)
163+
})
157164
.catch(createSyncingCatchError(fileKey, 'upload'))
158165

159166
emitEvent(eventName, {
@@ -162,7 +169,7 @@ export function mountThemeFileSystem(root: string, options?: ThemeFileSystemOpti
162169
contentPromise
163170
.then((content) => {
164171
// Run only if content has changed
165-
if (unsyncedFileKeys.has(fileKey)) fn(content)
172+
if (unsyncedFileKeys.has(fileKey)) fn(content?.value ?? content?.attachment ?? '')
166173
})
167174
.catch(() => {})
168175
},
@@ -275,13 +282,13 @@ export function handleSyncUpdate(
275282
fileKey: string,
276283
themeId: string,
277284
adminSession: AdminSession,
278-
): ((value: string) => boolean | PromiseLike<boolean>) | null | undefined {
285+
): (content: {value?: string; attachment?: string}) => PromiseLike<boolean> {
279286
return async (content) => {
280287
if (!unsyncedFileKeys.has(fileKey)) {
281288
return false
282289
}
283290

284-
const [result] = await bulkUploadThemeAssets(Number(themeId), [{key: fileKey, value: content}], adminSession)
291+
const [result] = await bulkUploadThemeAssets(Number(themeId), [{key: fileKey, ...content}], adminSession)
285292

286293
if (!result?.success) {
287294
const errors = result?.errors?.asset ?? ['Response was not successful.']

0 commit comments

Comments
 (0)