Skip to content
Browse files

Fix ls behavior for directory and file arguments

  • Loading branch information...
1 parent e31fbf4 commit d83be38cdf4ce347df1a5bb78fb1f1c73d39811d @nfischer nfischer committed
Showing with 38 additions and 16 deletions.
  1. +7 −3 dist/commands/ls.js
  2. +7 −7 dist/index.js
  3. +7 −3 packages/ls/dist/commands/ls.js
  4. +7 −3 src/commands/ls.js
  5. +10 −0 test/ls.js
View
10 dist/commands/ls.js
@@ -62,7 +62,8 @@ var ls = {
stdout += ls.execLsOnFiles('.', preSortedPaths.files, options).results;
}
- stdout += ls.formatAll(dirResults, options);
+ var dirOutput = ls.formatAll(dirResults, options, dirResults.length + preSortedPaths.files.length > 1);
+ stdout += stdout && dirOutput ? '\n\n' + dirOutput : dirOutput;
if (strip(stdout).trim() !== '') {
ls.self.log(String(stdout).replace(/\\/g, '/'));
}
@@ -90,6 +91,8 @@ var ls = {
ls.error(p, e);
}
}
+ files.sort();
+ dirs.sort();
return { files: files, dirs: dirs };
},
@@ -347,13 +350,14 @@ var ls = {
*
* @param {Array} results
* @param {Object} options
+ * @param {boolean} showName
* @return {String} stdout
* @api private
*/
- formatAll: function formatAll(results, options) {
+ formatAll: function formatAll(results, options, showName) {
var stdout = '';
- if (results.length > 1) {
+ if (showName) {
for (var i = 0; i < results.length; ++i) {
stdout += results[i].path + ':\n';
if (options.l) {
View
14 dist/index.js
@@ -10,7 +10,7 @@ var delimiter = require('./delimiter.js');
var path = require('path');
var fs = require('fs');
-var cmds = undefined;
+var cmds = void 0;
var app = {
@@ -39,7 +39,7 @@ var app = {
out += str + '\n';
return '';
});
- var commands = undefined;
+ var commands = void 0;
if (tmpl) {
// Render into a single string, inserting interpolated values.
var interpVals = [].concat(Array.prototype.slice.call(arguments)).slice(1);
@@ -70,7 +70,7 @@ var app = {
try {
(function () {
var mod = require('./commands/' + cmd + '.js');
- var help = undefined;
+ var help = void 0;
try {
help = require('./help/' + cmd + '.js');
help = String(help).replace(/^\n|\n$/g, '');
@@ -141,7 +141,7 @@ var app = {
});
// Load aliases
- var all = undefined;
+ var all = void 0;
try {
all = JSON.parse(app.vorpal.localStorage.getItem('aliases') || []);
} catch (e) {
@@ -196,10 +196,10 @@ var app = {
return path.join(delimiter.getHomeDir(), str);
});
- for (var i = 0; i < locations.length; ++i) {
+ for (var _i = 0; _i < locations.length; ++_i) {
try {
- if (!fs.statSync(locations[i]).isDirectory()) {
- app.vorpal.execSync('source ' + locations[i]);
+ if (!fs.statSync(locations[_i]).isDirectory()) {
+ app.vorpal.execSync('source ' + locations[_i]);
break;
}
} catch (e) {
View
10 packages/ls/dist/commands/ls.js
@@ -62,7 +62,8 @@ var ls = {
stdout += ls.execLsOnFiles('.', preSortedPaths.files, options).results;
}
- stdout += ls.formatAll(dirResults, options);
+ var dirOutput = ls.formatAll(dirResults, options, dirResults.length + preSortedPaths.files.length > 1);
+ stdout += stdout && dirOutput ? '\n\n' + dirOutput : dirOutput;
if (strip(stdout).trim() !== '') {
ls.self.log(String(stdout).replace(/\\/g, '/'));
}
@@ -90,6 +91,8 @@ var ls = {
ls.error(p, e);
}
}
+ files.sort();
+ dirs.sort();
return { files: files, dirs: dirs };
},
@@ -347,13 +350,14 @@ var ls = {
*
* @param {Array} results
* @param {Object} options
+ * @param {boolean} showName
* @return {String} stdout
* @api private
*/
- formatAll: function formatAll(results, options) {
+ formatAll: function formatAll(results, options, showName) {
var stdout = '';
- if (results.length > 1) {
+ if (showName) {
for (var i = 0; i < results.length; ++i) {
stdout += results[i].path + ':\n';
if (options.l) {
View
10 src/commands/ls.js
@@ -60,7 +60,8 @@ const ls = {
stdout += ls.execLsOnFiles('.', preSortedPaths.files, options).results;
}
- stdout += ls.formatAll(dirResults, options);
+ const dirOutput = ls.formatAll(dirResults, options, (dirResults.length + preSortedPaths.files.length > 1));
+ stdout += (stdout && dirOutput) ? `\n\n${dirOutput}` : dirOutput;
if (strip(stdout).trim() !== '') {
ls.self.log(String(stdout).replace(/\\/g, '/'));
}
@@ -89,6 +90,8 @@ const ls = {
ls.error(p, e);
}
}
+ files.sort();
+ dirs.sort();
return {files, dirs};
},
@@ -348,13 +351,14 @@ const ls = {
*
* @param {Array} results
* @param {Object} options
+ * @param {boolean} showName
* @return {String} stdout
* @api private
*/
- formatAll(results, options) {
+ formatAll(results, options, showName) {
let stdout = '';
- if (results.length > 1) {
+ if (showName) {
for (let i = 0; i < results.length; ++i) {
stdout += `${results[i].path}:\n`;
if (options.l) {
View
10 test/ls.js
@@ -66,6 +66,16 @@ if (os.platform() !== 'win32') {
strip(res).should.equal(expected.subDirFlat);
});
+ it('should list files and directories', function () {
+ const res = ls(['a.txt', 'b.tgz', './sub/']);
+ strip(res).should.equal(`a.txt b.tgz\n\n./sub/:\n${expected.subDirFlat}`);
+ });
+
+ it('should sort files and directories, with files being first', function () {
+ const res = ls(['./sub', 'b.tgz', '.', 'a.txt']);
+ strip(res).should.equal(`a.txt b.tgz\n\n.:\n${expected.rootDirFlat}\n./sub:\n${expected.subDirFlat}`);
+ });
+
it('should list multiple directories', function () {
const res = ls(['.', './sub']);
strip(res).should.equal(`.:\n${expected.rootDirFlat}\n./sub:\n${expected.subDirFlat}`);

0 comments on commit d83be38

Please sign in to comment.
Something went wrong with that request. Please try again.