ryanmcgrath / wrench-js

Recursive file operations in Node.js
MIT License
435 stars 71 forks source link

lineReader does not read last line of file if not terminated with \n #49

Closed dylanb closed 11 years ago

dylanb commented 11 years ago

Or if you have a file with a single line that is not terminated with a \n, then lineReader will behave as if the file is empty.

The following code generates hash values for all files in a file directory. If there are two single line files, their hash will be the same value and this value will also be the same as a file that is totally empty.

(function () {
    'use strict';
    var fs = require('fs'),
        wrench = require('wrench'),
        util = require('util'),
        crypto = require('crypto'),
        allFiles,
        fd,
        ws;

allFiles = wrench.readdirSyncRecursive('.'); allFiles.sort(); allFiles = allFiles.filter(function (element) { return (element.indexOf('.git') === -1 && element.indexOf('.log') === -1); }); if (allFiles.indexOf('review') === -1) { // create the review directory wrench.mkdirSyncRecursive('review', '0700'); } else { // don't want to create hashes for ourselves or git files allFiles.splice(allFiles.indexOf('review'), 1); allFiles.splice(allFiles.indexOf('review/hashes.txt'), 1); } ws = fs.createWriteStream('review/hashes.txt', { flags: 'w', encoding: null, mode: '0600' }); ws.on('open', function (fd) { var i, ilen, name, stats, f, content, hash, buffer; for (i = 0, ilen = allFiles.length; i < ilen; i += 1) { name = allFiles[i]; stats = fs.statSync(name); console.log('processing file [' + name + ']'); if (stats.isFile()) { f = new wrench.LineReader(name); content = ''; while (f.hasNextLine()) { content += f.getNextLine(); } fs.closeSync( f.fd); hash = crypto .createHash('md5') .update(content) .digest('hex'); buffer = name + '\n' + hash + '\n\n'; console.log(buffer); ws.write(buffer); } } ws.end(); });


}());
ryanmcgrath commented 11 years ago

Months late, but should work. Let me know if you have issues, thanks and sorry!