Created
December 28, 2018 19:30
-
-
Save radxene/de23d13b3a2888756257b1e993224807 to your computer and use it in GitHub Desktop.
Read all files from directory and write their contents into one file.
This file contains 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
#!/usr/bin/env node | |
const fs = require('fs'); | |
const os = require('os'); | |
const path = require('path'); | |
const readline = require('readline'); | |
const pkgDir = path.join(os.homedir(), '.emacs.d/scripts/pkg'); | |
const outputFile = path.join(os.homedir(), '.emacs.d/scripts/packages.el'); | |
const writer = fs.createWriteStream(outputFile); | |
const files = fs.readdirSync(pkgDir); | |
let pos = 0; | |
const readAndMerge = file => { | |
const filepath = path.join(pkgDir, file); | |
const rl = readline.createInterface({ | |
input: fs.createReadStream(filepath), | |
crlfDelay: Infinity, | |
}); | |
rl.on('line', (line) => { | |
writer.write(line + '\n'); | |
}); | |
rl.on('close', () => { | |
const nextFile = files[++pos]; | |
if (nextFile) { | |
readAndMerge(nextFile); | |
} else { | |
writer.end(); | |
console.log('Writing file succeeded!'); | |
} | |
}); | |
}; | |
readAndMerge(files[pos]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment