lineReader does not read last line of file if not terminated with \n #49
Labels
No labels
Bug
Enhancement
Feature Request
No milestone
No project
No assignees
1 participant
Due date
No due date set.
Dependencies
No dependencies set.
Reference: code/wrench-js#49
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
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();
});
Months late, but should work. Let me know if you have issues, thanks and sorry!