forked from chruxin/merge-md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.js
executable file
·37 lines (29 loc) · 879 Bytes
/
merge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env node
const fs = require('fs');
if (process.argv.length < 2 || process.argv.length > 5) {
console.log("Usage: '$ merge path/to/directory' or '$ merge' to merge files in current directory.");
process.exit(-1);
}
let path;
// 2 argvs, use current directory path as path
if (process.argv.length === 2) {
path = process.cwd();
} else {
path = process.argv[2];
}
let mergedContent = '';
let fileExt = '.md';
let mdOutPath = path + '/merged' + fileExt;
try {
fs.readdirSync(path).forEach((fileName) => {
if (fileName.indexOf('.DS_Store') === -1) {
mergedContent += fs.readFileSync(path + '/' + fileName, 'utf-8') + '\n';
}
});
fs.writeFileSync(mdOutPath, mergedContent);
console.log(`Success! Check your merged${fileExt} in ${path}`);
}
catch (err) {
console.log(`Oh no, An error occurred! ${err.message}`);
process.exit(-1);
}