Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
/******************************************************************************/
const fpath = process.argv[2];
if (!fpath) {
console.error('Add the file path, please.');
process.exit(1);
}
/******************************************************************************/
const fs = require('fs');
const inCoding = guessEncoding(fpath);
if (!inCoding) process.exit(1);
const outCoding = 'utf8';
const rl = require('readline').createInterface({
input: fs.createReadStream(fpath, inCoding),
terminal: false,
historySize: 0
});
const outfile = fs.openSync(fpath.replace(/(\.[^.]+)?$/, '.hw.txt'), 'w');
fs.writeSync(outfile, '\uFEFF', null, outCoding);
const bomRE = /^\uFEFF/;
const hwRE = /^[^#\s]/;
let lineNumber = 0;
/******************************************************************************/
rl.on('line', line => {
if (++lineNumber === 1) line = line.replace(bomRE, '');
if (hwRE.test(line)) {
fs.writeSync(outfile, `${line.trim()}\n`, null, outCoding);
}
});
/******************************************************************************/
function guessEncoding(fpath) {
try {
const fd = fs.openSync(fpath, 'r');
const bf = Buffer.alloc(2);
fs.readSync(fd, bf, 0, 2, 0);
fs.closeSync(fd);
return bf[0] === 0xFF && bf[1] === 0xFE ? 'utf16le' : 'utf8';
} catch(e) {
console.error(`Error: ${e.message}.`);
return null;
}
}
/******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment