-
Notifications
You must be signed in to change notification settings - Fork 3
/
merge.js
executable file
·46 lines (37 loc) · 1.08 KB
/
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
38
39
40
41
42
43
44
45
46
#!/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';
if (process.argv.includes('-mdx')){
fileExt = '.mdx';
}
let commentsEnabled = true;
if (process.argv.includes('-no-file-info')){
commentsEnabled = false;
}
try {
fs.readdirSync(path).forEach((fileName) => {
if (fileName.indexOf('.DS_Store') === -1) {
if(commentsEnabled){
mergedContent += '# ' + fileName + '\n';
}
mergedContent += fs.readFileSync(path + '/' + fileName, 'utf-8') + '\n';
}
});
fs.writeFileSync(path + '/merged' + fileExt, mergedContent);
console.log(`Success! Check your merged${fileExt} in ${path}`);
} catch (err) {
console.log(`Oh no, An error occurred! ${err.message}`);
process.exit(-1);
}