@@ -13,6 +13,16 @@ export interface ZippedWorkspaceResult {
1313 totalFileBytes : number
1414}
1515
16+ interface ZipFileAddedResult {
17+ result : 'added'
18+ addedBytes : number
19+ }
20+
21+ interface ZipFileSkippedResult {
22+ result : 'skipped'
23+ reason : 'excluded' | 'missing'
24+ }
25+
1626interface ZipProjectOptions {
1727 includeProjectName ?: boolean
1828 nonPosixPath ?: boolean
@@ -24,7 +34,51 @@ interface ZipProjectCustomizations {
2434 computeSideEffects ?: ( file : CollectFilesResultItem ) => Promise < void > | void
2535}
2636
27- export async function addToZip (
37+ export async function addFileToZip (
38+ file : CollectFilesResultItem ,
39+ zip : ZipStream ,
40+ customizations ?: ZipProjectCustomizations ,
41+ options ?: ZipProjectOptions
42+ ) : Promise < ZipFileAddedResult | ZipFileSkippedResult > {
43+ if ( customizations ?. isExcluded && customizations . isExcluded ( file ) ) {
44+ return { result : 'skipped' , reason : 'excluded' }
45+ }
46+ const errorToThrow = customizations ?. checkForError ? customizations . checkForError ( file ) : undefined
47+ if ( errorToThrow ) {
48+ throw errorToThrow
49+ }
50+
51+ // Paths in zip should be POSIX compliant regardless of OS
52+ // Reference: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
53+ const zipFilePath = options ?. includeProjectName
54+ ? path . join ( path . basename ( file . workspaceFolder . uri . fsPath ) , file . zipFilePath )
55+ : file . zipFilePath
56+ const posixPath = options ?. nonPosixPath ? zipFilePath : zipFilePath . split ( path . sep ) . join ( path . posix . sep )
57+
58+ try {
59+ // filepath will be out-of-sync for files with unsaved changes.
60+ if ( file . isText ) {
61+ zip . writeString ( file . fileContent , posixPath )
62+ } else {
63+ zip . writeFile ( file . fileUri . fsPath , path . dirname ( posixPath ) )
64+ }
65+ } catch ( error ) {
66+ if ( error instanceof Error && error . message . includes ( 'File not found' ) ) {
67+ // No-op: Skip if file was deleted or does not exist
68+ // Reference: https://github.com/cthackers/adm-zip/blob/1cd32f7e0ad3c540142a76609bb538a5cda2292f/adm-zip.js#L296-L321
69+ return { result : 'skipped' , reason : 'missing' }
70+ }
71+ throw error
72+ }
73+
74+ if ( customizations ?. computeSideEffects ) {
75+ await customizations . computeSideEffects ( file )
76+ }
77+
78+ return { result : 'added' , addedBytes : file . fileSizeBytes }
79+ }
80+
81+ export async function addProjectToZip (
2882 repoRootPaths : string [ ] ,
2983 workspaceFolders : CurrentWsFolders ,
3084 collectFilesOptions : CollectFilesOptions ,
@@ -41,40 +95,9 @@ export async function addToZip(
4195 }
4296 zippedFiles . add ( file . zipFilePath )
4397
44- if ( customizations ?. isExcluded && customizations . isExcluded ( file ) ) {
45- continue
46- }
47- const errorToThrow = customizations ?. checkForError ? customizations . checkForError ( file ) : undefined
48- if ( errorToThrow ) {
49- throw errorToThrow
50- }
51-
52- totalBytes += file . fileSizeBytes
53- // Paths in zip should be POSIX compliant regardless of OS
54- // Reference: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
55- const zipFilePath = options ?. includeProjectName
56- ? path . join ( path . basename ( file . workspaceFolder . uri . fsPath ) , file . zipFilePath )
57- : file . zipFilePath
58- const posixPath = options ?. nonPosixPath ? zipFilePath : zipFilePath . split ( path . sep ) . join ( path . posix . sep )
59-
60- try {
61- // filepath will be out-of-sync for files with unsaved changes.
62- if ( file . isText ) {
63- zip . writeString ( file . fileContent , posixPath )
64- } else {
65- zip . writeFile ( file . fileUri . fsPath , path . dirname ( posixPath ) )
66- }
67- } catch ( error ) {
68- if ( error instanceof Error && error . message . includes ( 'File not found' ) ) {
69- // No-op: Skip if file was deleted or does not exist
70- // Reference: https://github.com/cthackers/adm-zip/blob/1cd32f7e0ad3c540142a76609bb538a5cda2292f/adm-zip.js#L296-L321
71- continue
72- }
73- throw error
74- }
75-
76- if ( customizations ?. computeSideEffects ) {
77- await customizations . computeSideEffects ( file )
98+ const addFileResult = await addFileToZip ( file , zip , customizations , options )
99+ if ( addFileResult . result === 'added' ) {
100+ totalBytes += addFileResult . addedBytes
78101 }
79102 }
80103
@@ -88,7 +111,7 @@ export async function zipProject(
88111 customizations ?: ZipProjectCustomizations ,
89112 options ?: ZipProjectOptions & { zip ?: ZipStream }
90113) : Promise < ZippedWorkspaceResult > {
91- const { zip, totalBytesAdded } = await addToZip (
114+ const { zip, totalBytesAdded } = await addProjectToZip (
92115 repoRootPaths ,
93116 workspaceFolders ,
94117 collectFilesOptions ,
0 commit comments