Skip to content

Commit 2cce99a

Browse files
committed
fix theme package listings issue
1 parent 410ca76 commit 2cce99a

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

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

Lines changed: 20 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 = withExplicitDirectoryEntries(structure)
30+
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
3031
})
3132
})
3233

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

5051
// Then
5152
const archiveEntries = await readArchiveFiles(zipPath)
52-
expect([`extensions/first/main.js`]).toEqual(archiveEntries)
53+
const expectedEntries = withExplicitDirectoryEntries([`extensions/first/main.js`])
54+
expect(expectedEntries.sort()).toEqual(archiveEntries.sort())
5355
})
5456
})
5557
})
@@ -157,3 +159,19 @@ async function readArchiveFiles(zipPath: string) {
157159

158160
return archiveEntries
159161
}
162+
163+
function withExplicitDirectoryEntries(files: string[]): string[] {
164+
const entries = new Set<string>()
165+
for (const file of files) {
166+
entries.add(file)
167+
let currentDir = dirname(file)
168+
while (currentDir && currentDir !== '.' && currentDir !== '/') {
169+
const dirEntry = currentDir.endsWith('/') ? currentDir : `${currentDir}/`
170+
entries.add(dirEntry)
171+
const parent = dirname(currentDir)
172+
if (parent === currentDir) break
173+
currentDir = parent
174+
}
175+
}
176+
return Array.from(entries)
177+
}

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

Lines changed: 29 additions & 4 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,41 @@ export async function zip(options: ZipOptions): Promise<void> {
5252
})
5353
archive.pipe(output)
5454

55+
// Find parent directories to add explicitly to the archive
56+
const directoriesToAdd = new Set<string>()
5557
for (const filePath of pathsToZip) {
56-
const fileRelativePath = relativePath(inputDirectory, filePath)
57-
archive.file(filePath, {name: fileRelativePath})
58+
const relPath = relativePath(inputDirectory, filePath)
59+
collectParentDirectories(relPath, directoriesToAdd)
60+
}
61+
62+
// Add directories, parents before children
63+
const sortedDirs = Array.from(directoriesToAdd).sort((left, right) => left.localeCompare(right))
64+
for (const dir of sortedDirs) {
65+
const dirName = dir.endsWith('/') ? dir : `${dir}/`
66+
archive.append(Buffer.alloc(0), {name: dirName})
67+
}
68+
69+
// Add files
70+
for (const filePath of pathsToZip) {
71+
const rel = relativePath(inputDirectory, filePath)
72+
if (filePath && rel) archive.file(filePath, {name: rel})
5873
}
5974

6075
// eslint-disable-next-line @typescript-eslint/no-floating-promises
6176
archive.finalize()
6277
})
6378
}
6479

80+
function collectParentDirectories(fileRelativePath: string, accumulator: Set<string>): void {
81+
let currentDir = dirname(fileRelativePath)
82+
while (currentDir && currentDir !== '.' && currentDir !== '/') {
83+
accumulator.add(currentDir)
84+
const parent = dirname(currentDir)
85+
if (parent === currentDir) break
86+
currentDir = parent
87+
}
88+
}
89+
6590
export interface BrotliOptions {
6691
/**
6792
* The directory to compress.
@@ -133,7 +158,7 @@ export async function brotliCompress(options: BrotliOptions): Promise<void> {
133158
// eslint-disable-next-line @typescript-eslint/no-floating-promises
134159
archive.finalize()
135160
})
136-
.catch((error) => reject(error))
161+
.catch((error) => reject(error instanceof Error ? error : new Error(String(error))))
137162
})
138163

139164
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)