11// eslint-disable-next-line import/no-unassigned-import
22import 'typed-query-selector' ;
3- import saveFile from 'save-file' ;
43import {
54 getDirectoryContentViaContentsApi ,
65 getDirectoryContentViaTreesApi ,
@@ -18,6 +17,15 @@ function isError(error: unknown): error is Error {
1817 return error instanceof Error ;
1918}
2019
20+ function saveFile ( blob : Blob , filename : string ) {
21+ const url = URL . createObjectURL ( blob ) ;
22+ const a = document . createElement ( 'a' ) ;
23+ a . href = url ;
24+ a . download = filename ;
25+ a . click ( ) ;
26+ URL . revokeObjectURL ( url ) ;
27+ }
28+
2129async function listFiles (
2230 repoListingConfig : ApiOptions ,
2331) : Promise < Array < TreeResponseObject | ContentsReponseObject > > {
@@ -181,20 +189,19 @@ async function init() {
181189 throw error ;
182190 }
183191
184- updateStatus ( `Zipping ${ downloaded } files` ) ;
192+ updateStatus ( `Zipping ${ downloaded } files... ` ) ;
185193
186194 const zip = await zipPromise ;
187195 const zipBlob = await zip . generateAsync ( {
188196 type : 'blob' ,
189197 } ) ;
190198
191- const filename = query . get ( 'filename' ) ;
192- const zipFilename = filename
193- ? ( filename . toLowerCase ( ) . endsWith ( '.zip' )
194- ? filename
195- : filename + '.zip' )
196- : `${ user } ${ repository } ${ gitReference } ${ directory } .zip` . replace ( / \/ / , '-' ) ;
197- await saveFile ( zipBlob , zipFilename ) ;
199+ const filename
200+ = query . get ( 'filename' )
201+ ?? `${ user } ${ repository } ${ gitReference } ${ directory } ` . replace ( / \/ / , '-' ) ;
202+
203+ const zipFilename = filename . endsWith ( '.zip' ) ? filename : `${ filename } .zip` ;
204+ saveFile ( zipBlob , zipFilename ) ;
198205 updateStatus ( `Downloaded ${ downloaded } files! Done!` ) ;
199206}
200207
0 commit comments