Last active
December 14, 2015 19:48
-
-
Save zenlambda/5138747 to your computer and use it in GitHub Desktop.
This program lists files in a non-deterministic order on windows (node v0.8.22 and v0.10.0)
On linux on v0.8.22 the files are listed arbitrarily but consistently between runs On linux with v0.10.0 the entries in a directory are listed alphabetically Invoke with:
node ls-tree /path/to/tree/to/crawl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this program lists files in a non-deterministic order on windows (node v0.8.22 and v0.10.0) | |
// on linux on v0.8.22 the files are listed arbitrarily but consistently between runs | |
// on linux with v0.10.0 the entries in a directory are listed alphabetically | |
// invoke with: | |
// node ls-tree /path/to/tree/to/crawl | |
var events = require('events'); | |
var path = require('path'); | |
var Stream = require('stream'); | |
var fs = require('fs'); | |
function createDirectoryStream (dir) { | |
var s = new Stream; | |
s.readable = true; | |
fs.readdir(dir, function (err, files) { | |
if (err) { | |
s.emit('error', err); | |
return; | |
} | |
files.forEach(function (file) { | |
s.emit('data', path.join(dir, file)); | |
}); | |
process.nextTick(function () { | |
s.emit('end'); | |
}); | |
}); | |
return s; | |
} | |
function createRecursiveDirectoryStream (dir) { | |
var s = new Stream, | |
directoryStream = createDirectoryStream(dir), | |
childStream, | |
childDirs; | |
s.readable = true; | |
directoryStream.on('data', function (file) { | |
fs.stat(file, function (err, stat) { | |
if(err) { | |
s.emit('error',err); | |
return; | |
} | |
s.emit('data', file); | |
if (stat.isDirectory()) { | |
childDirs++; | |
childStream = createRecursiveDirectoryStream(file); | |
childStream.on('data', function (file) { | |
s.emit('data', file); | |
}); | |
childStream.on('end', function () { | |
childDirs--; | |
if(!childDirs) { | |
s.emit('end'); | |
} | |
}); | |
} | |
}); | |
}); | |
return s; | |
} | |
function newLineStream() { | |
var s = new Stream; | |
s.readable = true; | |
s.writeable = true; | |
s.writable = true; // work around little bugette in v0.8.22 | |
s.write = function (data) { | |
s.emit('data', data+'\n'); | |
return true; | |
} | |
s.end = function (data) { | |
s.emit('end'); | |
} | |
s.destroy = function() { | |
stream.emit("close"); | |
} | |
s.emit("drain"); | |
return s; | |
} | |
createRecursiveDirectoryStream(process.argv[2]) | |
.pipe(newLineStream()) // comment this out if you want | |
.pipe(process.stdout); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment