Skip to content

Commit c7d65ac

Browse files
authored
fix(ls): ls not following links to directories by default (shelljs#764)
* Fix ls not following links to directories by default
1 parent 2ee83eb commit c7d65ac

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/ls.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ function _ls(options, paths) {
7575

7676
try {
7777
stat = options.link ? fs.statSync(p) : fs.lstatSync(p);
78+
// follow links to directories by default
79+
if (stat.isSymbolicLink()) {
80+
try {
81+
var _stat = fs.statSync(p);
82+
if (_stat.isDirectory()) {
83+
stat = _stat;
84+
}
85+
} catch (_) {} // bad symlink, treat it like a file
86+
}
7887
} catch (e) {
7988
common.error('no such file or directory: ' + p, 2, { continue: true });
8089
return;

test/ls.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,16 @@ test('-L flag, path is symlink', t => {
328328
});
329329
});
330330

331+
test('follow links to directories by default', t => {
332+
utils.skipOnWin(t, () => {
333+
const result = shell.ls('test/resources/rm/link_to_a_dir');
334+
t.falsy(shell.error());
335+
t.is(result.code, 0);
336+
t.truthy(result.indexOf('a_file') > -1);
337+
t.is(result.length, 1);
338+
});
339+
});
340+
331341
test('-Rd works like -d', t => {
332342
const result = shell.ls('-Rd', 'test/resources/ls');
333343
t.falsy(shell.error());

0 commit comments

Comments
 (0)