Skip to content

Commit

Permalink
Merge pull request #26 from hjdivad/sort-entries
Browse files Browse the repository at this point in the history
Return lexicographically sorted entries
  • Loading branch information
stefanpenner authored Jul 11, 2016
2 parents 4db098f + 53ee266 commit f436012
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function handleRelativePath(_relativePath) {
return '';
} else if (_relativePath.slice(-1) !== '/') {
return _relativePath + '/';
} else {
return _relativePath;
}
}

Expand Down Expand Up @@ -54,23 +56,47 @@ function _walkSync(baseDir, options, _relativePath) {
return results;
}

var entries = fs.readdirSync(baseDir + '/' + relativePath).sort();
for (var i = 0; i < entries.length; i++) {
var entryRelativePath = relativePath + entries[i];
var names = fs.readdirSync(baseDir + '/' + relativePath);
var entries = names.map(function (name) {
var entryRelativePath = relativePath + name;
var fullPath = baseDir + '/' + entryRelativePath;
var stats = getStat(fullPath);

if (stats && stats.isDirectory()) {
if (options.directories !== false && (!m || m.match(entryRelativePath))) {
results.push(new Entry(entryRelativePath + '/', baseDir, stats.mode, stats.size, stats.mtime.getTime()));
return new Entry(entryRelativePath + '/', baseDir, stats.mode, stats.size, stats.mtime.getTime());
} else {
return new Entry(entryRelativePath, baseDir, stats && stats.mode, stats && stats.size, stats && stats.mtime.getTime());
}
}).filter(Boolean);

var sortedEntries = entries.sort(function (a, b) {
var aPath = a.relativePath;
var bPath = b.relativePath;

if (aPath === bPath) {
return 0;
} else if (aPath < bPath) {
return -1;
} else {
return 1;
}
});

for (var i=0; i<sortedEntries.length; ++i) {
var entry = sortedEntries[i];

if (entry.isDirectory()) {
if (options.directories !== false && (!m || m.match(entry.relativePath))) {
results.push(entry);
}
results = results.concat(_walkSync(baseDir, options, entryRelativePath));
results = results.concat(_walkSync(baseDir, options, entry.relativePath));
} else {
if (!m || m.match(entryRelativePath)) {
results.push(new Entry(entryRelativePath, baseDir, stats && stats.mode, stats && stats.size, stats && stats.mtime.getTime()));
if (!m || m.match(entry.relativePath)) {
results.push(entry);
}
}
}

return results;
}

Expand Down
Empty file added test/fixtures/foo/a.js
Empty file.
18 changes: 16 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ symlink('./some-other-dir', 'test/fixtures/symlink1');
symlink('doesnotexist', 'test/fixtures/symlink2', true);

// this allows us to call walkSync with fixed path separators,
// but CI will use it's native format (Windows testing).
// but CI will use its native format (Windows testing).
// we can't duplicate our tests hardcoding windows paths
// because walkSync checks path.sep, not your supplied path format
var oldWalkSync = walkSync;
Expand All @@ -43,20 +43,26 @@ walkSync.entries = function() {
};

test('walkSync', function (t) {
t.deepEqual(walkSync('test/fixtures'), [
var entries = walkSync('test/fixtures');

t.deepEqual(entries, [
'dir/',
'dir/bar.txt',
'dir/subdir/',
'dir/subdir/baz.txt',
'dir/zzz.txt',
'foo.txt',
'foo/',
'foo/a.js',
'some-other-dir/',
'some-other-dir/qux.txt',
'symlink1/',
'symlink1/qux.txt',
'symlink2'
]);

t.deepEqual(entries, entries.slice().sort());

t.matchThrows(function() {
walkSync('test/doesnotexist');
}, {
Expand Down Expand Up @@ -111,6 +117,14 @@ test('entries', function (t) {
basePath: 'test/fixtures',
fullPath: 'test/fixtures/foo.txt'
},
{
basePath: 'test/fixtures',
fullPath: 'test/fixtures/foo/'
},
{
basePath: 'test/fixtures',
fullPath: 'test/fixtures/foo/a.js'
},
{
basePath: 'test/fixtures',
fullPath: 'test/fixtures/some-other-dir/'
Expand Down

0 comments on commit f436012

Please sign in to comment.