Skip to content

Commit 61b39dd

Browse files
authored
Merge pull request #6247 from Shopify/package-fix
Fix theme package folder structure
2 parents b80e57f + db9f3cf commit 61b39dd

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

packages/cli-kit/src/public/node/archiver.integration.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('zip', () => {
2626

2727
// Then
2828
const archiveEntries = await readArchiveFiles(zipPath)
29-
expect(structure.sort()).toEqual(archiveEntries.sort())
29+
const expectedEntries = ['extensions/', 'extensions/first/', 'extensions/first/main.js', 'test.json']
30+
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
3031
})
3132
})
3233

@@ -49,7 +50,11 @@ describe('zip', () => {
4950

5051
// Then
5152
const archiveEntries = await readArchiveFiles(zipPath)
52-
expect([`extensions/first/main.js`]).toEqual(archiveEntries)
53+
54+
expect(archiveEntries).toContain('extensions/')
55+
expect(archiveEntries).toContain('extensions/first/')
56+
const expectedEntries = ['extensions/', 'extensions/first/', 'extensions/first/main.js']
57+
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
5358
})
5459
})
5560
})

packages/cli-kit/src/public/node/archiver.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {relativePath, joinPath} from './path.js'
1+
import {relativePath, joinPath, dirname} from './path.js'
22
import {glob, removeFile} from './fs.js'
33
import {outputDebug, outputContent, outputToken} from '../../public/node/output.js'
44
import archiver from 'archiver'
@@ -52,16 +52,38 @@ export async function zip(options: ZipOptions): Promise<void> {
5252
})
5353
archive.pipe(output)
5454

55+
const directoriesToAdd = new Set<string>()
5556
for (const filePath of pathsToZip) {
5657
const fileRelativePath = relativePath(inputDirectory, filePath)
57-
archive.file(filePath, {name: fileRelativePath})
58+
collectParentDirectories(fileRelativePath, directoriesToAdd)
59+
}
60+
61+
const sortedDirs = Array.from(directoriesToAdd).sort((left, right) => left.localeCompare(right))
62+
for (const dir of sortedDirs) {
63+
const dirName = dir.endsWith('/') ? dir : `${dir}/`
64+
archive.append(Buffer.alloc(0), {name: dirName})
65+
}
66+
67+
for (const filePath of pathsToZip) {
68+
const fileRelativePath = relativePath(inputDirectory, filePath)
69+
if (filePath && fileRelativePath) archive.file(filePath, {name: fileRelativePath})
5870
}
5971

6072
// eslint-disable-next-line @typescript-eslint/no-floating-promises
6173
archive.finalize()
6274
})
6375
}
6476

77+
function collectParentDirectories(fileRelativePath: string, accumulator: Set<string>): void {
78+
let currentDir = dirname(fileRelativePath)
79+
while (currentDir && currentDir !== '.' && currentDir !== '/') {
80+
accumulator.add(currentDir)
81+
const parent = dirname(currentDir)
82+
if (parent === currentDir) break
83+
currentDir = parent
84+
}
85+
}
86+
6587
export interface BrotliOptions {
6688
/**
6789
* The directory to compress.
@@ -133,7 +155,7 @@ export async function brotliCompress(options: BrotliOptions): Promise<void> {
133155
// eslint-disable-next-line @typescript-eslint/no-floating-promises
134156
archive.finalize()
135157
})
136-
.catch((error) => reject(error))
158+
.catch((error) => reject(error instanceof Error ? error : new Error(String(error))))
137159
})
138160

139161
const tarContent = readFileSync(tempTarPath)

packages/theme/src/cli/services/package.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ async function readArchiveFiles(zipPath: string) {
191191
await expect(fileExists(zipPath)).resolves.toBeTruthy()
192192
// eslint-disable-next-line @babel/new-cap
193193
const archive = new StreamZip.async({file: zipPath})
194-
const archiveEntries = Object.keys(await archive.entries())
194+
const entries = await archive.entries()
195+
const archiveEntries = Object.values(entries)
196+
.filter((entry: any) => !entry.isDirectory)
197+
.map((entry: any) => entry.name)
195198
await archive.close()
196199

197200
return archiveEntries

0 commit comments

Comments
 (0)