Skip to content

Commit d5bf33e

Browse files
Refactoring ls method and unit tests
1 parent 952c027 commit d5bf33e

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

node/task.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -975,29 +975,45 @@ export function resolve(...pathSegments: any[]): string {
975975
export const which = im._which;
976976

977977
/**
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.
979979
* @param {string} options Available options: -R (recursive), -A (all files, include files beginning with ., except for . and ..)
980980
* @param {string[]} paths Paths to search.
981981
* @return {string[]} An array of files in the given path(s).
982982
*/
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[] {
984988
let isRecursive = false;
985989
let includeHidden = false;
986990
let handleAsOptions = false;
987991

988-
if (typeof optionsOrPaths == 'string' && optionsOrPaths.startsWith('-')) {
992+
if (typeof optionsOrPaths === 'string' && optionsOrPaths.startsWith('-')) {
989993
optionsOrPaths = optionsOrPaths.toLowerCase();
990994
isRecursive = optionsOrPaths.includes('r');
991995
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+
9931013
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);
10011017
}
10021018
}
10031019

@@ -1048,7 +1064,7 @@ export function ls(optionsOrPaths?: string | string[], ...paths: string[]): stri
10481064
return entries;
10491065
} catch (error) {
10501066
if (error.code === 'ENOENT') {
1051-
throw new Error(`Failed ls: ${error}`);
1067+
throw new Error(loc('LIB_PathNotFound', 'ls', error.message));
10521068
} else {
10531069
throw new Error(loc('LIB_OperationFailed', 'ls', error));
10541070
}
@@ -1084,7 +1100,10 @@ function retryer(func: Function, retryCount: number = 0, continueOnError: boolea
10841100
* @param {boolean} [continueOnError] - Optional. whether to continue on error.
10851101
* @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.
10861102
*/
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 {
10881107
retryer(() => {
10891108
let recursive = false;
10901109
let force = true;
@@ -1107,7 +1126,7 @@ export function cp(sourceOrOptions: string, destinationOrSource: string, options
11071126
}
11081127

11091128
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));
11111130
}
11121131

11131132
const lstatSource = fs.lstatSync(source);
@@ -1131,11 +1150,7 @@ export function cp(sourceOrOptions: string, destinationOrSource: string, options
11311150
fs.cpSync(source, path.join(destination, path.basename(source)), { recursive, force });
11321151
}
11331152
} 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));
11391154
}
11401155
}, retryCount, continueOnError);
11411156
}

node/test/ls.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ describe('ls cases', () => {
7373

7474
it('Provide the folder which does not exist', (done) => {
7575
assert.ok(!fs.existsSync('/thisfolderdoesnotexist'));
76-
assert.throws(() => tl.ls('/thisfolderdoesnotexist'), { message: /^Failed ls: Error: ENOENT: no such file or directory, lstat/ });
77-
76+
assert.throws(() => tl.ls('/thisfolderdoesnotexist'), { message: /^Not found ls: ENOENT: no such file or directory, lstat/ });
7877
done();
7978
});
8079

@@ -266,7 +265,7 @@ describe('ls cases', () => {
266265
done();
267266
});
268267

269-
it('Empty attributes, but several paths', (done) => {
268+
it('Empty attributes, but several paths as multiple arguments', (done) => {
270269
const result = tl.ls('', TEMP_SUBDIR_1, TEMP_FILE_1);
271270

272271
assert.ok(result.includes(TEMP_FILE_1));
@@ -277,6 +276,37 @@ describe('ls cases', () => {
277276
done();
278277
});
279278

279+
it('Empty attributes, but several paths in array', (done) => {
280+
const result = tl.ls('', [TEMP_SUBDIR_1]);
281+
282+
assert.ok(result.includes(TEMP_SUBDIR_FILE_1));
283+
assert.ok(result.includes(TEMP_SUBDIR_FILELINK_1));
284+
assert.equal(result.length, 2);
285+
286+
done();
287+
});
288+
289+
it('Empty attributes, but one path', (done) => {
290+
const result = tl.ls('', TEMP_SUBDIR_1);
291+
292+
assert.ok(result.includes(TEMP_SUBDIR_FILE_1));
293+
assert.ok(result.includes(TEMP_SUBDIR_FILELINK_1));
294+
assert.equal(result.length, 2);
295+
296+
done();
297+
});
298+
299+
it('Provide path as first argument and subdir as second argument', (done) => {
300+
const result = tl.ls(TEMP_FILE_1, TEMP_SUBDIR_1);
301+
302+
assert.ok(result.includes(TEMP_FILE_1));
303+
assert.ok(result.includes(TEMP_SUBDIR_FILE_1));
304+
assert.ok(result.includes(TEMP_SUBDIR_FILELINK_1));
305+
assert.equal(result.length, 3);
306+
307+
done();
308+
});
309+
280310
it('New one folder without content', (done) => {
281311
tl.mkdirP('foo');
282312

0 commit comments

Comments
 (0)