From df06ac4a8a011b38f701b3fbc852cefa3e964091 Mon Sep 17 00:00:00 2001 From: Fran Dios Date: Tue, 28 Feb 2017 13:53:32 +0900 Subject: [PATCH] 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 --- README.md | 1 + src/ls.js | 8 +++++--- test/ls.js | 32 +++++++++++++++++++++++--------- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f0884bb..b0fee03 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ls.js b/src/ls.js index 7f25056..cd2d920 100644 --- a/src/ls.js +++ b/src/ls.js @@ -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)); }); diff --git a/test/ls.js b/test/ls.js index a50f270..05d2476 100644 --- a/test/ls.js +++ b/test/ls.js @@ -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 => {