ls -L (follow symlinks) (#665)

* feat: -L (follow symlinks) option for ls

* test: ls -L option

* docs: Add ls -L option to readme

* fix: ls -L ternary operator

* Revert "test: ls -L option"

This reverts commit dbb057ac087b23aa2c1358018d3c832dd546c5f2.

* test: ls -L option

* test: Remove duplicate test
This commit is contained in:
Fran Dios 2017-02-28 13:53:32 +09:00 committed by Brandon Freitag
parent 006b8c2460
commit df06ac4a8a
3 changed files with 29 additions and 12 deletions

View File

@ -418,6 +418,7 @@ Available options:
+ `-R`: recursive
+ `-A`: all files (include files beginning with `.`, except for `.` and `..`)
+ `-L`: follow symlinks
+ `-d`: list directories themselves, not their contents
+ `-l`: list objects representing each file, each with fields containing `ls
-l` output fields. See

View File

@ -9,6 +9,7 @@ common.register('ls', _ls, {
cmdOptions: {
'R': 'recursive',
'A': 'all',
'L': 'link',
'a': 'all_deprecated',
'd': 'directory',
'l': 'long',
@ -22,6 +23,7 @@ common.register('ls', _ls, {
//@
//@ + `-R`: recursive
//@ + `-A`: all files (include files beginning with `.`, except for `.` and `..`)
//@ + `-L`: follow symlinks
//@ + `-d`: list directories themselves, not their contents
//@ + `-l`: list objects representing each file, each with fields containing `ls
//@ -l` output fields. See
@ -60,7 +62,7 @@ function _ls(options, paths) {
relName = relName.replace(/\\/g, '/');
}
if (options.long) {
stat = stat || fs.lstatSync(abs);
stat = stat || (options.link ? fs.statSync(abs) : fs.lstatSync(abs));
list.push(addLsAttributes(relName, stat));
} else {
// list.push(path.relative(rel || '.', file));
@ -72,7 +74,7 @@ function _ls(options, paths) {
var stat;
try {
stat = fs.lstatSync(p);
stat = options.link ? fs.statSync(p) : fs.lstatSync(p);
} catch (e) {
common.error('no such file or directory: ' + p, 2, { continue: true });
return;
@ -82,7 +84,7 @@ function _ls(options, paths) {
if (stat.isDirectory() && !options.directory) {
if (options.recursive) {
// use glob, because it's simple
glob.sync(p + globPatternRecursive, { dot: options.all })
glob.sync(p + globPatternRecursive, { dot: options.all, follow: options.link })
.forEach(function (item) {
pushFile(item, path.relative(p, item));
});

View File

@ -271,7 +271,7 @@ test('recursive, no path', t => {
t.is(result.length, 9);
});
test('recusive, path given', t => {
test('recursive, path given', t => {
const result = shell.ls('-R', 'resources/ls');
t.falsy(shell.error());
t.is(result.code, 0);
@ -292,14 +292,28 @@ test('-RA flag, path given', t => {
t.is(result.length, 14);
});
test('recursive, wildcard', t => {
const result = shell.ls('-R', 'resources/ls');
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(result.indexOf('a_dir') > -1);
t.truthy(result.indexOf('a_dir/b_dir') > -1);
t.truthy(result.indexOf('a_dir/b_dir/z') > -1);
t.is(result.length, 9);
test('-RAL flag, path given', t => {
if (process.platform !== 'win32') {
const result = shell.ls('-RAL', 'resources/rm');
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(result.indexOf('a_dir') > -1);
t.truthy(result.indexOf('a_dir/a_file') > -1);
t.truthy(result.indexOf('link_to_a_dir') > -1);
t.truthy(result.indexOf('link_to_a_dir/a_file') > -1);
t.truthy(result.indexOf('fake.lnk') > -1);
t.is(result.length, 5);
}
});
test('-L flag, path is symlink', t => {
if (process.platform !== 'win32') {
const result = shell.ls('-L', 'resources/rm/link_to_a_dir');
t.falsy(shell.error());
t.is(result.code, 0);
t.truthy(result.indexOf('a_file') > -1);
t.is(result.length, 1);
}
});
test('-Rd works like -d', t => {