22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5+ import vscode from 'vscode'
56import path from 'path'
6- import { collectFiles , CollectFilesOptions , CollectFilesResultItem } from '../../shared/utilities/workspaceUtils'
7+ import {
8+ collectFiles ,
9+ CollectFilesOptions ,
10+ CollectFilesResultItem ,
11+ getFileInfo ,
12+ } from '../../shared/utilities/workspaceUtils'
713import { CurrentWsFolders } from '../commons/types'
814import { ZipStream } from '../../shared/utilities/zipStream'
915
@@ -28,14 +34,19 @@ interface ZipProjectOptions {
2834 nonPosixPath ?: boolean
2935}
3036
37+ export type ZipExcluder = ( file : Omit < CollectFilesResultItem , 'workspaceFolder' > ) => boolean
38+ export type ZipErrorCheck = ( file : Omit < CollectFilesResultItem , 'workspaceFolder' > ) => Error | undefined
39+ export type ZipTracker = ( file : Omit < CollectFilesResultItem , 'workspaceFolder' > ) => Promise < void > | void
40+
3141interface ZipProjectCustomizations {
32- isExcluded ?: ( file : CollectFilesResultItem ) => boolean
33- checkForError ?: ( file : CollectFilesResultItem ) => Error | undefined
34- computeSideEffects ?: ( file : CollectFilesResultItem ) => Promise < void > | void
42+ isExcluded ?: ZipExcluder
43+ checkForError ?: ZipErrorCheck
44+ computeSideEffects ?: ZipTracker
3545}
3646
3747export async function addFileToZip (
38- file : CollectFilesResultItem ,
48+ file : Omit < CollectFilesResultItem , 'workspaceFolder' > ,
49+ targetFilePath : string ,
3950 zip : ZipStream ,
4051 customizations ?: ZipProjectCustomizations ,
4152 options ?: ZipProjectOptions
@@ -48,19 +59,12 @@ export async function addFileToZip(
4859 throw errorToThrow
4960 }
5061
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-
5862 try {
5963 // filepath will be out-of-sync for files with unsaved changes.
6064 if ( file . isText ) {
61- zip . writeString ( file . fileContent , posixPath )
65+ zip . writeString ( file . fileContent , targetFilePath )
6266 } else {
63- zip . writeFile ( file . fileUri . fsPath , path . dirname ( posixPath ) )
67+ zip . writeFile ( file . fileUri . fsPath , path . dirname ( targetFilePath ) )
6468 }
6569 } catch ( error ) {
6670 if ( error instanceof Error && error . message . includes ( 'File not found' ) ) {
@@ -95,7 +99,14 @@ export async function addProjectToZip(
9599 }
96100 zippedFiles . add ( file . zipFilePath )
97101
98- const addFileResult = await addFileToZip ( file , zip , customizations , options )
102+ // Paths in zip should be POSIX compliant regardless of OS
103+ // Reference: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
104+ const zipFilePath = options ?. includeProjectName
105+ ? path . join ( path . basename ( file . workspaceFolder . uri . fsPath ) , file . zipFilePath )
106+ : file . zipFilePath
107+ const targetPath = options ?. nonPosixPath ? zipFilePath : zipFilePath . split ( path . sep ) . join ( path . posix . sep )
108+
109+ const addFileResult = await addFileToZip ( file , targetPath , zip , customizations , options )
99110 if ( addFileResult . result === 'added' ) {
100111 totalBytes += addFileResult . addedBytes
101112 }
@@ -127,3 +138,22 @@ export async function zipProject(
127138 totalFileBytes : totalBytesAdded ,
128139 }
129140}
141+ // TODO: remove vscode dep
142+ export async function zipFile (
143+ file : vscode . Uri ,
144+ targetPath : string ,
145+ customizations ?: ZipProjectCustomizations ,
146+ options ?: ZipProjectOptions
147+ ) {
148+ return await addFileToZip (
149+ {
150+ ...( await getFileInfo ( file , true ) ) ,
151+ zipFilePath : targetPath ,
152+ relativeFilePath : file . fsPath ,
153+ } ,
154+ targetPath ,
155+ new ZipStream ( ) ,
156+ customizations ,
157+ options
158+ )
159+ }
0 commit comments