refactor(ls): greatly simplify ls implimentation

This commit is contained in:
Ari Porad 2016-02-20 16:37:28 -08:00
parent 3e441e4923
commit b76a5691c9
4 changed files with 37 additions and 102 deletions

View File

@ -23,7 +23,7 @@ exports.pwd = common.wrap('pwd', _pwd);
//@include ./src/ls
var _ls = require('./src/ls');
exports.ls = common.wrap('ls', _ls, {idx: 1});
exports.ls = common.wrap('ls', _ls, {noGlob: true});
//@include ./src/find
var _find = require('./src/find');

View File

@ -1,4 +1,5 @@
var fs = require('fs');
var path = require('path');
var common = require('./common');
var _ls = require('./ls');
@ -20,16 +21,15 @@ var _ls = require('./ls');
function _find(options, paths) {
if (!paths)
common.error('no path specified');
else if (typeof paths === 'object')
paths = paths; // assume array
else if (typeof paths === 'string')
paths = [].slice.call(arguments, 1);
var list = [];
function pushFile(file) {
if (common.platform === 'win')
if (common.platform === 'win') {
file = file.replace(/\\/g, '/');
}
list.push(file);
}
@ -40,8 +40,8 @@ function _find(options, paths) {
pushFile(file);
if (fs.statSync(file).isDirectory()) {
_ls('-RA', file+'/*').forEach(function(subfile) {
pushFile(subfile);
_ls('-RA', file).forEach(function(subfile) {
pushFile(path.join(file, subfile));
});
}
});

124
src/ls.js
View File

@ -1,8 +1,10 @@
var path = require('path');
var fs = require('fs');
var common = require('./common');
var _cd = require('./cd');
var _pwd = require('./pwd');
var glob = require('glob');
var globPatternAll = path.sep + '*';
var globPatternRecrusive = path.sep + '**' + globPatternAll;
//@
//@ ### ls([options,] [path, ...])
@ -46,119 +48,51 @@ function _ls(options, paths) {
if (!paths)
paths = ['.'];
else if (typeof paths === 'object')
paths = paths; // assume array
else if (typeof paths === 'string')
paths = [].slice.call(arguments, 1);
paths = common.expand(paths, { dot: options.all });
var list = [];
// Conditionally pushes file to list - returns true if pushed, false otherwise
// (e.g. prevents hidden files to be included unless explicitly told so)
function pushFile(file, query) {
var name = file.name || file;
// hidden file?
if (path.basename(name)[0] === '.') {
// not explicitly asking for hidden files?
if (!options.all && !(path.basename(query)[0] === '.' && path.basename(query).length > 1))
return false;
}
if (common.platform === 'win')
name = name.replace(/\\/g, '/');
if (file.name) {
file.name = name;
function pushFile(file, rel, stat) {
stat = stat || fs.lstatSync(file);
if (process.platform === 'win32') file = file.replace(/\\/, '/');
if (options.long) {
list.push(ls_stat(file, stat));
} else {
file = name;
list.push(path.relative(rel || '.', file));
}
list.push(file);
return true;
}
paths.forEach(function(p) {
if (fs.existsSync(p)) {
var stats = ls_stat(p);
// Simple file?
if (stats.isFile()) {
if (options.long) {
pushFile(stats, p);
} else {
pushFile(p, p);
}
return; // continue
}
var stat;
// Simple dir?
if (options.directory) {
pushFile(p, p);
return;
} else if (stats.isDirectory()) {
// Iterate over p contents
fs.readdirSync(p).forEach(function(file) {
var orig_file = file;
if (options.long)
file = ls_stat(path.join(p, file));
if (!pushFile(file, p))
return;
try {
stat = fs.lstatSync(p);
} catch (e) {
common.error('no such file or directory: ' + p, true);
}
// Recursive?
if (options.recursive) {
var oldDir = _pwd().toString();
_cd('', p);
if (fs.statSync(orig_file).isDirectory())
list = list.concat(_ls('-R'+(options.all?'A':''), orig_file+'/*'));
_cd('', oldDir);
}
// If the stat failed.
if (stat) {
if (!options.directory && stat.isDirectory()) {
var pathWithGlob = p + (options.recursive ? globPatternRecrusive : globPatternAll);
glob.sync(pathWithGlob, { dot: options.all }).forEach(function (item) {
pushFile(item, p);
});
return; // continue
} else {
pushFile(p, null, stat);
}
}
// p does not exist - possible wildcard present
var basename = path.basename(p);
var dirname = path.dirname(p);
// Wildcard present on an existing dir? (e.g. '/tmp/*.js')
if (basename.search(/\*/) > -1 && fs.existsSync(dirname) && fs.statSync(dirname).isDirectory) {
// Escape special regular expression chars
var regexp = basename.replace(/(\^|\$|\(|\)|<|>|\[|\]|\{|\}|\.|\+|\?)/g, '\\$1');
// Translates wildcard into regex
regexp = '^' + regexp.replace(/\*/g, '.*') + '$';
// Iterate over directory contents
fs.readdirSync(dirname).forEach(function(file) {
if (file.match(new RegExp(regexp))) {
var file_path = path.join(dirname, file);
file_path = options.long ? ls_stat(file_path) : file_path;
if (file_path.name)
file_path.name = path.normalize(file_path.name);
else
file_path = path.normalize(file_path);
if (!pushFile(file_path, basename))
return;
// Recursive?
if (options.recursive) {
var pp = dirname + '/' + file;
if (fs.lstatSync(pp).isDirectory())
list = list.concat(_ls('-R'+(options.all?'A':''), pp+'/*'));
} // recursive
} // if file matches
}); // forEach
return;
}
common.error('no such file or directory: ' + p, true);
});
// Add methods, to make this more compatible with ShellStrings
return new common.ShellString(list, common.state.error);
}
module.exports = _ls;
function ls_stat(path) {
var stats = fs.statSync(path);
function ls_stat(path, stats) {
// Note: this object will contain more information than .toString() returns
stats.name = path;
stats.toString = function() {
@ -167,3 +101,5 @@ function ls_stat(path) {
};
return stats;
}
module.exports = _ls;

View File

@ -144,7 +144,7 @@ assert.equal(result.indexOf('resources/ls/file1.js') > -1, true);
// wildcard, should not do partial matches
var result = shell.ls('resources/ls/*.j'); // shouldn't get .js
assert.equal(shell.error(), null);
assert.ok(shell.error());
assert.equal(result.length, 0);
// wildcard, all files with extension
@ -249,7 +249,6 @@ assert.equal(result.length, 6);
// long option, single file
var result = shell.ls('-l', 'resources/ls/file1')[0];
assert.equal(shell.error(), null);
assert.equal(result.name, 'resources/ls/file1');
assert.equal(result.nlink, 1);
assert.equal(result.size, 5);
assert.ok(result.mode); // check that these keys exist