-
Notifications
You must be signed in to change notification settings - Fork 2
/
ns-summary.js
44 lines (35 loc) · 1.24 KB
/
ns-summary.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
#!/usr/bin/env node
var program = require('commander');
var path = require('path');
var fs = require('fs');
var async = require('async');
program.parse(process.argv);
var args = program.args;
var pth = path.resolve(args.length ? args[0] : '');
console.log('Path: ' + pth);
if (!fs.existsSync(pth)) {
console.log('Path not exist. Abort.');
process.exit(1);
}
var summaryPath = path.join(pth, 'SUMMARY.md');
fs.writeFileSync(summaryPath, '# Summary\r\n\r\n* [Introduction](README.md)\r\n');
var folders = fs.readdirSync(pth).filter(function(file) {
return fs.statSync(path.join(pth, file)).isDirectory();
});
var things = [];
folders.forEach(function(folder) {
var readme = path.join(pth, folder, 'README.md');
var thingName = '';
if (fs.existsSync(readme)) {
var fileContent = fs.readFileSync(readme, { encoding: 'UTF-8' });
var lines = fileContent.split('\r\n');
if (lines.length) {
thingName = lines[0].replace('#', '').trim();
things.push('* [{thingName}]({url}/README.md)'
.replace('{thingName}', thingName).replace('{url}', folder));
process.stdout.write('.');
}
}
});
things.length && fs.appendFileSync(path.join(pth, 'SUMMARY.md'), things.join('\r\n'));
console.log('Creating SUMMARY completed!');