Skip to content

Commit 740206e

Browse files
LS function fix for single file input (#1106)
* Adding fix for file as parameter to ls * code cleaning * code cleaning * updating version * code cleaning
1 parent badfe9f commit 740206e

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed

node/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "azure-pipelines-task-lib",
3-
"version": "5.2.0",
3+
"version": "5.2.1",
44
"description": "Azure Pipelines Task SDK",
55
"main": "./task.js",
66
"typings": "./task.d.ts",

node/task.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,6 @@ export function ls(optionsOrPaths?: unknown, ...paths: unknown[]): string[] {
10451045
includeHidden = options.includes('a');
10461046
}
10471047

1048-
// Flatten paths if the paths argument is array
10491048
if (Array.isArray(paths)) {
10501049
paths = flattenArray(paths);
10511050
}
@@ -1072,7 +1071,27 @@ export function ls(optionsOrPaths?: unknown, ...paths: unknown[]): string[] {
10721071
}
10731072
const pathsCopy = [...paths];
10741073
const preparedPaths: string[] = [];
1074+
const fileEntries: string[] = [];
1075+
10751076
try {
1077+
let remainingPaths: string[] = [];
1078+
while (paths.length > 0) {
1079+
const pathEntry = resolve(paths.shift());
1080+
1081+
if (pathEntry?.includes('*')) {
1082+
remainingPaths.push(pathEntry);
1083+
continue;
1084+
}
1085+
1086+
const stats = fs.lstatSync(pathEntry);
1087+
if (stats.isFile()) {
1088+
const fileName = path.basename(pathEntry);
1089+
fileEntries.push(fileName);
1090+
} else {
1091+
remainingPaths.push(pathEntry);
1092+
}
1093+
}
1094+
paths.push(...remainingPaths);
10761095
while (paths.length > 0) {
10771096
const pathEntry = resolve(paths.shift());
10781097
if (pathEntry?.includes('*')) {
@@ -1110,8 +1129,8 @@ export function ls(optionsOrPaths?: unknown, ...paths: unknown[]): string[] {
11101129
entries.push(path.relative(baseDir, entry));
11111130
}
11121131
}
1113-
1114-
return entries;
1132+
const finalResults = [...fileEntries, ...entries];
1133+
return finalResults;
11151134
} catch (error) {
11161135
if (error.code === 'ENOENT') {
11171136
throw new Error(loc('LIB_PathNotFound', 'ls', error.message));
@@ -1226,7 +1245,7 @@ const copyDirectoryWithResolvedSymlinks = (src: string, dest: string, force: boo
12261245
if (!fs.existsSync(dest)) {
12271246
fs.mkdirSync(dest, { recursive: true });
12281247
}
1229-
1248+
12301249
for (entry of entries) {
12311250
srcPath = path.join(src, entry.name);
12321251
destPath = path.join(dest, entry.name);

node/test/ls.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,11 @@ describe('ls cases', () => {
296296

297297
done();
298298
});
299+
300+
it('Provide filepaths only', (done) => {
301+
const result = tl.ls([TEMP_FILE_1, TEMP_FILE_1_JS]);
302+
assert.ok(result.includes(path.basename(TEMP_FILE_1)));
303+
assert.ok(result.includes(path.basename(TEMP_FILE_1_JS)));
304+
done();
305+
});
299306
});

0 commit comments

Comments
 (0)