Skip to content

Commit

Permalink
Optimize theme polling to skip re-downloading files that are uploaded…
Browse files Browse the repository at this point in the history
… from local
  • Loading branch information
jamesmengo committed May 21, 2024
1 parent 1a52026 commit 83577be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as themePolling from './theme-polling.js'
import {pollRemoteJsonChanges} from './theme-polling.js'
import {readThemeFilesFromDisk} from '../theme-fs.js'
import {fakeThemeFileSystem} from '../theme-fs/theme-fs-mock-factory.js'
import {AbortError} from '@shopify/cli-kit/node/error'
Expand All @@ -12,7 +12,6 @@ vi.mock('@shopify/cli-kit/node/themes/api')
vi.mock('../theme-fs.js')

describe('pollRemoteJsonChanges', async () => {
const {pollRemoteJsonChanges} = themePolling
const developmentTheme = buildTheme({id: 1, name: 'Theme', role: DEVELOPMENT_THEME_ROLE})!
const adminSession = {token: '', storeFqdn: ''}
const files = new Map<string, ThemeAsset>([])
Expand Down Expand Up @@ -54,6 +53,27 @@ describe('pollRemoteJsonChanges', async () => {
})
})

test('does not download newly added files from remote theme when file with equivalent checksum is already presenty locally', async () => {
// Given
const remoteChecksums: Checksum[] = []
const updatedRemoteChecksums = [{checksum: '1', key: 'templates/asset.json'}]
vi.mocked(fetchChecksums).mockResolvedValue(updatedRemoteChecksums)
const themeFileSystem = fakeThemeFileSystem(
'tmp',
new Map([['templates/asset.json', {checksum: '1', key: 'templates/asset.json'}]]),
)

// When
await pollRemoteJsonChanges(developmentTheme, adminSession, remoteChecksums, themeFileSystem)

// Then
expect(themeFileSystem.files.get('templates/asset.json')).toEqual({
checksum: '1',
key: 'templates/asset.json',
value: 'content',
})
})

test('deletes local file from remote theme when there is a change on remote', async () => {
// Given
const remoteChecksums = [{checksum: '1', key: 'templates/asset.json'}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export async function pollRemoteJsonChanges(
assetsChangedOnRemote
.filter((file) => file.key.endsWith('.json'))
.map(async (file) => {
if (localFileSystem.files.get(file.key)?.checksum === file.checksum) {
return
}
const asset = await fetchThemeAsset(targetTheme.id, file.key, currentSession)
if (asset) {
return localFileSystem.write(asset).then(() => {
Expand Down

0 comments on commit 83577be

Please sign in to comment.