@@ -6,6 +6,7 @@ import Blob from 'fetch-blob'
6
6
import { requestUserCLIConfirmation } from '../utils/userInteractions' ;
7
7
import BashlibError from '../utils/errors/BashlibError' ;
8
8
import { BashlibErrorMessage } from '../utils/errors/BashlibError' ;
9
+ import type { Logger } from '../logger' ;
9
10
10
11
const mime = require ( 'mime-types' ) ;
11
12
@@ -24,6 +25,7 @@ type CopyOptions = {
24
25
all ?: boolean ,
25
26
interactiveOverride ?: boolean ,
26
27
noOverride ?: boolean ,
28
+ logger ?: Logger
27
29
}
28
30
29
31
export default async function copy ( src : string , dst : string , options : CopyOptions ) : Promise < {
@@ -69,15 +71,15 @@ export default async function copy(src: string, dst: string, options: CopyOption
69
71
**********************/
70
72
71
73
if ( source . isDir && ! destination . isDir ) {
72
- console . error ( 'Cannot copy a directory to a file' )
74
+ ( options . logger || console ) . error ( 'Cannot copy a directory to a file' )
73
75
process . exit ( 1 ) ;
74
76
}
75
77
76
78
let resourcesToTransfer : { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } ;
77
79
if ( source . isRemote ) {
78
- resourcesToTransfer = await getRemoteSourceFiles ( source , fetch , options . verbose , options . all )
80
+ resourcesToTransfer = await getRemoteSourceFiles ( source , fetch , options . verbose , options . all , options )
79
81
} else {
80
- resourcesToTransfer = await getLocalSourceFiles ( source , options . verbose , options . all )
82
+ resourcesToTransfer = await getLocalSourceFiles ( source , options . verbose , options . all , options )
81
83
}
82
84
83
85
let destinationInfo : { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } = {
@@ -169,41 +171,41 @@ export default async function copy(src: string, dst: string, options: CopyOption
169
171
* UTILITY FUNCTIONS *
170
172
*********************/
171
173
172
- async function getLocalSourceFiles ( source : srcOptions , verbose : boolean , all : boolean ) : Promise < { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } > {
174
+ async function getLocalSourceFiles ( source : srcOptions , verbose : boolean , all : boolean , options ?: { logger ?: Logger } ) : Promise < { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } > {
173
175
if ( source . isDir ) {
174
176
let filePathInfos = readLocalDirectoryRecursively ( source . path , undefined , { verbose, all} )
175
177
let files = await Promise . all ( filePathInfos . files . map ( async fileInfo => {
176
- fileInfo . loadFile = async ( ) => readLocalFile ( fileInfo . absolutePath , verbose )
178
+ fileInfo . loadFile = async ( ) => readLocalFile ( fileInfo . absolutePath , verbose , options )
177
179
return fileInfo
178
180
} ) )
179
181
let aclfiles = await Promise . all ( filePathInfos . aclfiles . map ( async fileInfo => {
180
- fileInfo . loadFile = async ( ) => readLocalFile ( fileInfo . absolutePath , verbose )
182
+ fileInfo . loadFile = async ( ) => readLocalFile ( fileInfo . absolutePath , verbose , options )
181
183
return fileInfo
182
184
} ) )
183
185
return { files, aclfiles, directories : filePathInfos . directories }
184
186
} else {
185
187
return { files : [ {
186
188
absolutePath : source . path ,
187
189
relativePath : '' ,
188
- loadFile : async ( ) => readLocalFile ( source . path , verbose )
190
+ loadFile : async ( ) => readLocalFile ( source . path , verbose , options )
189
191
} ] , aclfiles : [ ] , directories : [ ] }
190
192
}
191
193
}
192
194
193
- async function getRemoteSourceFiles ( source : srcOptions , fetch : Function , verbose : boolean , all : boolean ) : Promise < { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } > {
195
+ async function getRemoteSourceFiles ( source : srcOptions , fetch : Function , verbose : boolean , all : boolean , options ?: { logger ?: Logger } ) : Promise < { files : FileInfo [ ] , directories : FileInfo [ ] , aclfiles : FileInfo [ ] } > {
194
196
if ( source . isDir ) {
195
197
let discoveredResources = await readRemoteDirectoryRecursively ( source . path , { fetch, verbose, all} )
196
198
197
199
// Filter out files that return errors (e.g no authentication privileges)
198
200
let files = ( await Promise . all ( discoveredResources . files . map ( async fileInfo => {
199
- fileInfo . loadFile = async ( ) => readRemoteFile ( fileInfo . absolutePath , fetch , verbose )
201
+ fileInfo . loadFile = async ( ) => readRemoteFile ( fileInfo . absolutePath , fetch , verbose , options )
200
202
return fileInfo
201
203
} ) ) ) . filter ( f => f ) as FileInfo [ ]
202
204
203
205
let aclfiles : FileInfo [ ] = [ ]
204
206
if ( all ) {
205
207
aclfiles = ( await Promise . all ( discoveredResources . aclfiles . map ( async fileInfo => {
206
- fileInfo . loadFile = async ( ) => readRemoteFile ( fileInfo . absolutePath , fetch , verbose )
208
+ fileInfo . loadFile = async ( ) => readRemoteFile ( fileInfo . absolutePath , fetch , verbose , options )
207
209
return fileInfo
208
210
} ) ) ) . filter ( f => f ) as FileInfo [ ]
209
211
}
@@ -212,39 +214,39 @@ async function getRemoteSourceFiles(source: srcOptions, fetch: Function, verbose
212
214
return { files : [ {
213
215
absolutePath : source . path ,
214
216
relativePath : '' ,
215
- loadFile : async ( ) => readRemoteFile ( source . path , fetch , verbose )
217
+ loadFile : async ( ) => readRemoteFile ( source . path , fetch , verbose , options )
216
218
} ] , aclfiles : [ ] , directories : [ ] }
217
219
}
218
220
219
221
}
220
222
221
- function readLocalFile ( path : string , verbose : boolean ) : { buffer : Buffer , contentType : string } {
222
- if ( verbose ) console . log ( 'Reading local file:' , path )
223
+ function readLocalFile ( path : string , verbose : boolean , options ?: { logger ?: Logger } ) : { buffer : Buffer , contentType : string } {
224
+ if ( verbose ) ( options ?. logger || console ) . log ( 'Reading local file:' , path )
223
225
const file = fs . readFileSync ( path )
224
226
let contentType = path . endsWith ( '.acl' ) || path . endsWith ( '.meta' ) ? 'text/turtle' : mime . lookup ( path )
225
227
return { buffer : file , contentType } ;
226
228
}
227
229
228
- async function readRemoteFile ( path : string , fetch : any , verbose : boolean ) : Promise < { blob : any , contentType : string } > {
229
- if ( verbose ) console . log ( 'Reading remote file:' , path )
230
+ async function readRemoteFile ( path : string , fetch : any , verbose : boolean , options ?: { logger ?: Logger } ) : Promise < { blob : any , contentType : string } > {
231
+ if ( verbose ) ( options ?. logger || console ) . log ( 'Reading remote file:' , path )
230
232
const file = await getFile ( path , { fetch } )
231
233
const contentType = await getContentType ( file ) as string // TODO:: error handling?
232
234
return { blob : file as any , contentType } ;
233
235
234
236
}
235
237
236
238
async function writeLocalDirectory ( path : string , fileInfo : FileInfo , options : CopyOptions ) : Promise < any > {
237
- if ( options . verbose ) console . log ( 'Writing local directory:' , path )
239
+ if ( options . verbose ) ( options . logger || console ) . log ( 'Writing local directory:' , path )
238
240
fs . mkdirSync ( path , { recursive : true } )
239
241
return true ;
240
242
}
241
243
242
244
async function writeRemoteDirectory ( path : string , fileInfo : FileInfo , fetch : any , options : CopyOptions ) : Promise < any > {
243
- if ( options . verbose ) console . log ( 'Writing remote directory:' , path )
245
+ if ( options . verbose ) ( options . logger || console ) . log ( 'Writing remote directory:' , path )
244
246
try {
245
247
await createContainerAt ( path , { fetch } )
246
248
} catch ( e ) {
247
- if ( options . verbose ) writeErrorString ( `Could not write directory for ${ path } ` , e ) ;
249
+ if ( options . verbose ) writeErrorString ( `Could not write directory for ${ path } ` , e , options ) ;
248
250
}
249
251
}
250
252
@@ -262,11 +264,11 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
262
264
}
263
265
}
264
266
if ( ! executeWrite ) {
265
- if ( options . verbose ) console . log ( 'Skipping existing local file:' , resourcePath )
267
+ if ( options . verbose ) ( options . logger || console ) . log ( 'Skipping existing local file:' , resourcePath )
266
268
return undefined ;
267
269
}
268
270
269
- if ( options . verbose ) console . log ( 'processing local file:' , resourcePath )
271
+ if ( options . verbose ) ( options . logger || console ) . log ( 'processing local file:' , resourcePath )
270
272
try {
271
273
if ( ! fileInfo . loadFile ) throw new Error ( `Could not load file at location: ${ fileInfo . absolutePath } ` )
272
274
let fileData = await fileInfo . loadFile ( ) ;
@@ -287,11 +289,11 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
287
289
let buffer = Buffer . from ( await fileData . blob . arrayBuffer ( ) )
288
290
fs . writeFileSync ( resourcePath , buffer )
289
291
} else {
290
- console . error ( 'No content to write for:' , resourcePath )
292
+ ( options . logger || console ) . error ( 'No content to write for:' , resourcePath )
291
293
}
292
294
return resourcePath ;
293
295
} catch ( e ) {
294
- if ( options . verbose ) writeErrorString ( `Could not save local file ${ resourcePath } ` , e ) ;
296
+ if ( options . verbose ) writeErrorString ( `Could not save local file ${ resourcePath } ` , e , options ) ;
295
297
return undefined ;
296
298
}
297
299
}
@@ -310,7 +312,7 @@ async function writeRemoteFile(resourcePath: string, fileInfo: FileInfo, fetch:
310
312
}
311
313
}
312
314
if ( ! executeWrite ) {
313
- if ( options . verbose ) console . log ( 'Skipping existing local file:' , resourcePath )
315
+ if ( options . verbose ) ( options . logger || console ) . log ( 'Skipping existing local file:' , resourcePath )
314
316
return undefined ;
315
317
}
316
318
0 commit comments