1
1
import fs from 'fs'
2
2
import path from 'path'
3
3
import { getFile , getContentType , createContainerAt } from "@inrupt/solid-client"
4
- import { isRemote , isDirectory , FileInfo , ensureDirectoryExistence , fixLocalPath , readRemoteDirectoryRecursively , checkRemoteFileExists , writeErrorString , isDirectoryContents } from '../utils/util' ;
4
+ import { isRemote , isDirectory , FileInfo , ensureDirectoryExistence , fixLocalPath , readRemoteDirectoryRecursively , checkRemoteFileExists , writeErrorString , isDirectoryContents , resourceExists } from '../utils/util' ;
5
5
import Blob from 'fetch-blob'
6
- import { requestUserCLIConfirmation } from '../utils/userInteractions' ;
6
+ import { requestUserCLIConfirmationDefaultNegative } from '../utils/userInteractions' ;
7
7
import BashlibError from '../utils/errors/BashlibError' ;
8
8
import { BashlibErrorMessage } from '../utils/errors/BashlibError' ;
9
9
import type { Logger } from '../logger' ;
10
10
import { ICommandOptions , setOptionDefaults } from './solid-command' ;
11
- import { resourceExists } from './solid-touch' ;
12
11
13
12
const mime = require ( 'mime-types' ) ;
14
13
@@ -23,8 +22,8 @@ interface SourceOptions {
23
22
24
23
export interface ICommandOptionsCopy extends ICommandOptions {
25
24
all ?: boolean ,
26
- interactiveOverride ?: boolean ,
27
- noOverride ?: boolean ,
25
+ override ?: boolean ,
26
+ neverOverride ?: boolean ,
28
27
}
29
28
30
29
export default async function copy ( src : string , dst : string , options ?: ICommandOptionsCopy ) : Promise < {
@@ -34,8 +33,8 @@ export default async function copy(src: string, dst: string, options?: ICommandO
34
33
let commandOptions = setOptionDefaults < ICommandOptionsCopy > ( options || { } ) ;
35
34
let fetch = commandOptions . fetch ;
36
35
commandOptions . all = commandOptions . all || false ;
37
- commandOptions . interactiveOverride = commandOptions . interactiveOverride || false ;
38
- commandOptions . noOverride = commandOptions . noOverride || false ;
36
+ commandOptions . override = commandOptions . override || false ;
37
+ commandOptions . neverOverride = commandOptions . neverOverride || false ;
39
38
40
39
/**************************
41
40
* Preprocess src and dst *
@@ -277,12 +276,12 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
277
276
ensureDirectoryExistence ( resourcePath ) ;
278
277
279
278
let executeWrite = true
280
- if ( options . interactiveOverride || options . noOverride ) {
281
- if ( fs . existsSync ( resourcePath ) ) {
282
- if ( options . noOverride ) {
279
+ if ( options . neverOverride || ! options . override ) {
280
+ if ( await resourceExists ( resourcePath , options . fetch ) ) {
281
+ if ( options . neverOverride ) {
283
282
executeWrite = false ;
284
- } else if ( options . interactiveOverride ) {
285
- executeWrite = await requestUserCLIConfirmation ( `Overwrite local file: ${ resourcePath } ` )
283
+ } else {
284
+ executeWrite = await requestUserCLIConfirmationDefaultNegative ( `Overwrite local file: ${ resourcePath } ` )
286
285
}
287
286
}
288
287
}
@@ -309,6 +308,7 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
309
308
310
309
if ( fileData . buffer ) {
311
310
fs . writeFileSync ( resourcePath , fileData . buffer )
311
+ if ( options . verbose ) ( options . logger || console ) . log ( `Writing local resource: ${ resourcePath } ` ) ;
312
312
} else if ( fileData . blob ) {
313
313
let buffer = Buffer . from ( await fileData . blob . arrayBuffer ( ) )
314
314
fs . writeFileSync ( resourcePath , buffer )
@@ -324,17 +324,17 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
324
324
325
325
async function writeRemoteFile ( resourcePath : string , fileInfo : FileInfo , fetch : any , options : ICommandOptionsCopy ) : Promise < string | undefined > {
326
326
resourcePath = resourcePath . split ( '$.' ) [ 0 ] ;
327
-
328
327
let executeWrite = true
329
- if ( options . interactiveOverride || options . noOverride ) {
328
+ if ( options . neverOverride || ! options . override ) {
330
329
if ( await resourceExists ( resourcePath , fetch ) ) {
331
- if ( options . noOverride ) {
330
+ if ( options . neverOverride ) {
332
331
executeWrite = false ;
333
- } else if ( options . interactiveOverride ) {
334
- executeWrite = await requestUserCLIConfirmation ( `Overwrite local file: ${ resourcePath } ` )
332
+ } else {
333
+ executeWrite = await requestUserCLIConfirmationDefaultNegative ( `Overwrite remote file: ${ resourcePath } ` )
335
334
}
336
335
}
337
336
}
337
+
338
338
if ( ! executeWrite ) {
339
339
if ( options . verbose ) ( options . logger || console ) . log ( 'Skipping existing local file:' , resourcePath )
340
340
return undefined ;
@@ -358,6 +358,7 @@ async function writeRemoteFile(resourcePath: string, fileInfo: FileInfo, fetch:
358
358
)
359
359
if ( ! res . ok )
360
360
throw new BashlibError ( BashlibErrorMessage . httpResponseError , resourcePath , `${ res . status } ${ res . statusText } ` )
361
+ else if ( options . verbose ) ( options . logger || console ) . log ( `Writing remote resource: ${ resourcePath } ` ) ;
361
362
362
363
} else if ( fileData . blob ) {
363
364
let res = await fetch (
@@ -372,6 +373,7 @@ async function writeRemoteFile(resourcePath: string, fileInfo: FileInfo, fetch:
372
373
)
373
374
if ( ! res . ok )
374
375
throw new BashlibError ( BashlibErrorMessage . httpResponseError , resourcePath , `${ res . status } ${ res . statusText } ` )
376
+ else if ( options . verbose ) ( options . logger || console ) . log ( `Writing remote resource: ${ resourcePath } ` ) ;
375
377
} else {
376
378
throw new BashlibError ( BashlibErrorMessage . cannotWriteResource , resourcePath , "No contents to write" )
377
379
}
0 commit comments