11import fs from 'fs'
22import path from 'path'
33import { 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' ;
55import Blob from 'fetch-blob'
6- import { requestUserCLIConfirmation } from '../utils/userInteractions' ;
6+ import { requestUserCLIConfirmationDefaultNegative } from '../utils/userInteractions' ;
77import BashlibError from '../utils/errors/BashlibError' ;
88import { BashlibErrorMessage } from '../utils/errors/BashlibError' ;
99import type { Logger } from '../logger' ;
1010import { ICommandOptions , setOptionDefaults } from './solid-command' ;
11- import { resourceExists } from './solid-touch' ;
1211
1312const mime = require ( 'mime-types' ) ;
1413
@@ -23,8 +22,8 @@ interface SourceOptions {
2322
2423export interface ICommandOptionsCopy extends ICommandOptions {
2524 all ?: boolean ,
26- interactiveOverride ?: boolean ,
27- noOverride ?: boolean ,
25+ override ?: boolean ,
26+ neverOverride ?: boolean ,
2827}
2928
3029export 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
3433 let commandOptions = setOptionDefaults < ICommandOptionsCopy > ( options || { } ) ;
3534 let fetch = commandOptions . fetch ;
3635 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 ;
3938
4039 /**************************
4140 * Preprocess src and dst *
@@ -277,12 +276,12 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
277276 ensureDirectoryExistence ( resourcePath ) ;
278277
279278 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 ) {
283282 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 } ` )
286285 }
287286 }
288287 }
@@ -309,6 +308,7 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
309308
310309 if ( fileData . buffer ) {
311310 fs . writeFileSync ( resourcePath , fileData . buffer )
311+ if ( options . verbose ) ( options . logger || console ) . log ( `Writing local resource: ${ resourcePath } ` ) ;
312312 } else if ( fileData . blob ) {
313313 let buffer = Buffer . from ( await fileData . blob . arrayBuffer ( ) )
314314 fs . writeFileSync ( resourcePath , buffer )
@@ -324,17 +324,17 @@ async function writeLocalFile(resourcePath: string, fileInfo: FileInfo, options:
324324
325325async function writeRemoteFile ( resourcePath : string , fileInfo : FileInfo , fetch : any , options : ICommandOptionsCopy ) : Promise < string | undefined > {
326326 resourcePath = resourcePath . split ( '$.' ) [ 0 ] ;
327-
328327 let executeWrite = true
329- if ( options . interactiveOverride || options . noOverride ) {
328+ if ( options . neverOverride || ! options . override ) {
330329 if ( await resourceExists ( resourcePath , fetch ) ) {
331- if ( options . noOverride ) {
330+ if ( options . neverOverride ) {
332331 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 } ` )
335334 }
336335 }
337336 }
337+
338338 if ( ! executeWrite ) {
339339 if ( options . verbose ) ( options . logger || console ) . log ( 'Skipping existing local file:' , resourcePath )
340340 return undefined ;
@@ -358,6 +358,7 @@ async function writeRemoteFile(resourcePath: string, fileInfo: FileInfo, fetch:
358358 )
359359 if ( ! res . ok )
360360 throw new BashlibError ( BashlibErrorMessage . httpResponseError , resourcePath , `${ res . status } ${ res . statusText } ` )
361+ else if ( options . verbose ) ( options . logger || console ) . log ( `Writing remote resource: ${ resourcePath } ` ) ;
361362
362363 } else if ( fileData . blob ) {
363364 let res = await fetch (
@@ -372,6 +373,7 @@ async function writeRemoteFile(resourcePath: string, fileInfo: FileInfo, fetch:
372373 )
373374 if ( ! res . ok )
374375 throw new BashlibError ( BashlibErrorMessage . httpResponseError , resourcePath , `${ res . status } ${ res . statusText } ` )
376+ else if ( options . verbose ) ( options . logger || console ) . log ( `Writing remote resource: ${ resourcePath } ` ) ;
375377 } else {
376378 throw new BashlibError ( BashlibErrorMessage . cannotWriteResource , resourcePath , "No contents to write" )
377379 }
0 commit comments