forked from anuraghazra/CanvasFun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-folder-readmes.js
50 lines (39 loc) · 1.28 KB
/
generate-folder-readmes.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
47
48
49
50
const fs = require('fs');
const path = require('path');
const ROOT = './'
const isUnixHiddenPath = (path) => {
return !(/(^|\/)\.[^\/\.]/g).test(path);
};
(() => {
fs.readdir(ROOT, function (err, files) {
if (err) throw new Error(err);
files.filter(isUnixHiddenPath).forEach(name => {
// skip these folders
if (name.match(/thumbnails|src/)) return false;
let filePath = path.join(ROOT, name);
let stat = fs.statSync(filePath);
if (stat.isDirectory()) {
// read nested files under filePath
fs.readdir(filePath, (err, subFiles) => {
if (err) throw new Error(err);
// if already readme.md (case-sensitive) exists then skip
if (!subFiles.includes('readme.md')) {
createReadmes(filePath);
}
})
}
});
});
function createReadmes(filePath) {
let targetFile = path.join(filePath, 'README.md');
const output = [
'<!-- This file is auto generated do not edit this file directly -->',
'<!-- Edit the generate-folder-readmes.js file instead -->',
`# ${filePath}`,
`\n[Live Demo](https://anuraghazra.github.io/CanvasFun/${filePath})`,
`\n-------\n`,
`Made with :heart:`,
]
fs.writeFileSync(targetFile, output.join('\n'));
}
})()