@@ -975,29 +975,45 @@ export function resolve(...pathSegments: any[]): string {
975
975
export const which = im . _which ;
976
976
977
977
/**
978
- * Returns array of files in the given path, or in current directory if no path provided. See shelljs.ls
978
+ * Returns array of files in the given path, or in current directory if no path provided.
979
979
* @param {string } options Available options: -R (recursive), -A (all files, include files beginning with ., except for . and ..)
980
980
* @param {string[] } paths Paths to search.
981
981
* @return {string[] } An array of files in the given path(s).
982
982
*/
983
- export function ls ( optionsOrPaths ?: string | string [ ] , ...paths : string [ ] ) : string [ ] {
983
+ export function ls ( optionsOrPaths ?: string | string [ ] , ...paths : string [ ] ) : string [ ] ;
984
+ export function ls ( optionsOrPaths ?: string | string [ ] , paths ?: string [ ] ) : string [ ] ;
985
+ export function ls ( optionsOrPaths ?: string | string [ ] , paths ?: string ) : string [ ] ;
986
+
987
+ export function ls ( optionsOrPaths ?: string | string [ ] , ...paths : unknown [ ] ) : string [ ] {
984
988
let isRecursive = false ;
985
989
let includeHidden = false ;
986
990
let handleAsOptions = false ;
987
991
988
- if ( typeof optionsOrPaths == 'string' && optionsOrPaths . startsWith ( '-' ) ) {
992
+ if ( typeof optionsOrPaths === 'string' && optionsOrPaths . startsWith ( '-' ) ) {
989
993
optionsOrPaths = optionsOrPaths . toLowerCase ( ) ;
990
994
isRecursive = optionsOrPaths . includes ( 'r' ) ;
991
995
includeHidden = optionsOrPaths . includes ( 'a' ) ;
992
- } else {
996
+ }
997
+
998
+ // Flatten paths if the paths argument is array
999
+ if ( Array . isArray ( paths ) ) {
1000
+ paths = paths . flat ( Infinity ) ;
1001
+ }
1002
+
1003
+ // If the first argument is not options, then it is a path
1004
+ if ( typeof optionsOrPaths !== 'string' || ! optionsOrPaths . startsWith ( '-' ) ) {
1005
+ let pathsFromOptions : string [ ] = [ ] ;
1006
+
1007
+ if ( Array . isArray ( optionsOrPaths ) ) {
1008
+ pathsFromOptions = optionsOrPaths ;
1009
+ } else if ( optionsOrPaths ) {
1010
+ pathsFromOptions = [ optionsOrPaths ] ;
1011
+ }
1012
+
993
1013
if ( paths === undefined || paths . length === 0 ) {
994
- if ( Array . isArray ( optionsOrPaths ) ) {
995
- paths = optionsOrPaths as string [ ] ;
996
- } else if ( optionsOrPaths && ! handleAsOptions ) {
997
- paths = [ optionsOrPaths ] ;
998
- } else {
999
- paths = [ ] ;
1000
- }
1014
+ paths = pathsFromOptions ;
1015
+ } else {
1016
+ paths . push ( ...pathsFromOptions ) ;
1001
1017
}
1002
1018
}
1003
1019
@@ -1048,7 +1064,7 @@ export function ls(optionsOrPaths?: string | string[], ...paths: string[]): stri
1048
1064
return entries ;
1049
1065
} catch ( error ) {
1050
1066
if ( error . code === 'ENOENT' ) {
1051
- throw new Error ( `Failed ls: ${ error } ` ) ;
1067
+ throw new Error ( loc ( 'LIB_PathNotFound' , 'ls' , error . message ) ) ;
1052
1068
} else {
1053
1069
throw new Error ( loc ( 'LIB_OperationFailed' , 'ls' , error ) ) ;
1054
1070
}
@@ -1084,7 +1100,10 @@ function retryer(func: Function, retryCount: number = 0, continueOnError: boolea
1084
1100
* @param {boolean } [continueOnError] - Optional. whether to continue on error.
1085
1101
* @param {number } [retryCount=0] - Optional. Retry count to copy the file. It might help to resolve intermittent issues e.g. with UNC target paths on a remote host.
1086
1102
*/
1087
- export function cp ( sourceOrOptions : string , destinationOrSource : string , optionsOrDestination ?: string , continueOnError ?: boolean , retryCount : number = 0 ) : void {
1103
+ export function cp ( source : string , destination : string , options ?: string , continueOnError ?: boolean , retryCount : number = 0 ) : void ;
1104
+ export function cp ( options : string , source : string , destination : string , continueOnError ?: boolean , retryCount : number = 0 ) : void ;
1105
+
1106
+ export function cp ( sourceOrOptions : string , destinationOrSource : string , optionsOrDestination : string , continueOnError ?: boolean , retryCount : number = 0 ) : void {
1088
1107
retryer ( ( ) => {
1089
1108
let recursive = false ;
1090
1109
let force = true ;
@@ -1107,7 +1126,7 @@ export function cp(sourceOrOptions: string, destinationOrSource: string, options
1107
1126
}
1108
1127
1109
1128
if ( ! fs . existsSync ( destination ) && ! force ) {
1110
- throw new Error ( `ENOENT: no such file or directory: ${ destination } ` ) ;
1129
+ throw new Error ( loc ( 'LIB_PathNotFound' , 'cp' , destination ) ) ;
1111
1130
}
1112
1131
1113
1132
const lstatSource = fs . lstatSync ( source ) ;
@@ -1131,11 +1150,7 @@ export function cp(sourceOrOptions: string, destinationOrSource: string, options
1131
1150
fs . cpSync ( source , path . join ( destination , path . basename ( source ) ) , { recursive, force } ) ;
1132
1151
}
1133
1152
} catch ( error ) {
1134
- if ( error . code === 'ENOENT' ) {
1135
- throw new Error ( error ) ;
1136
- } else {
1137
- throw new Error ( loc ( 'LIB_OperationFailed' , 'cp' , error ) ) ;
1138
- }
1153
+ throw new Error ( loc ( 'LIB_OperationFailed' , 'cp' , error ) ) ;
1139
1154
}
1140
1155
} , retryCount , continueOnError ) ;
1141
1156
}
0 commit comments