'use strict'; | |
/******************************************************************************/ | |
const path = process.argv[2]; | |
if (!path) { | |
console.log('Add the file path, please.'); | |
process.exit(); | |
} | |
/******************************************************************************/ | |
const fs = require('fs'); | |
const encoding = 'utf8'; //ascii utf8 utf16le | |
const rl = require('readline').createInterface({ | |
input: fs.createReadStream(path, encoding), | |
terminal: false, | |
historySize: 0 | |
}); | |
const logFile = fs.openSync(path.replace(/(\.[^.]+)?$/, '.-split_log$1'), 'w'); | |
fs.writeSync(logFile, '\uFEFF', null, encoding); | |
const abc = Array.from('ABCDEFGHIJKLMNOPQRSTUVWXYZ´'); | |
const formatNumberRE = /\B(?=(?:\d{3})+$)/g; | |
let outFile; | |
let lines; | |
let nextChr; | |
let nextChrRE; | |
/******************************************************************************/ | |
newPart(abc.shift()); | |
/******************************************************************************/ | |
rl.on('line', line => { | |
if (nextChr && nextChrRE.test(line)) { | |
newPart(nextChr); | |
} | |
fs.writeSync(outFile, `${line}\n`, null, encoding); | |
lines++; | |
}).on('close', () => { | |
fs.closeSync(outFile); | |
fs.writeSync(logFile, | |
`${lines.toString().replace(formatNumberRE, ' ')}\n`, null, encoding); | |
fs.closeSync(logFile); | |
}); | |
/******************************************************************************/ | |
function newPart(chr) { | |
if(outFile) { | |
fs.closeSync(outFile); | |
fs.writeSync(logFile, | |
`${lines.toString().replace(formatNumberRE, ' ')}\n`, null, encoding); | |
} | |
lines = 0; | |
outFile = fs.openSync(path.replace(/(\.[^.]+)?$/, `.${chr}$1`), 'w'); | |
if (chr !== 'A') fs.writeSync(outFile, '\uFEFF', null, encoding); | |
console.log(chr); | |
fs.writeSync(logFile, `${chr}: `, null, encoding); | |
if(nextChr = abc.shift()) nextChrRE = new RegExp(`^• ${nextChr}`, 'i'); | |
} | |
/******************************************************************************/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment