Skip to content

Instantly share code, notes, and snippets.

@radxene
Created December 28, 2018 19:30
Show Gist options
  • Save radxene/de23d13b3a2888756257b1e993224807 to your computer and use it in GitHub Desktop.
Save radxene/de23d13b3a2888756257b1e993224807 to your computer and use it in GitHub Desktop.
Read all files from directory and write their contents into one file.
#!/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