-
Notifications
You must be signed in to change notification settings - Fork 39
/
index.js
executable file
·48 lines (40 loc) · 1.47 KB
/
index.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
#!/usr/bin/env node
'use strict';
const tree = require('./walker');
const emoji = process.argv.includes('--emoji') || process.argv.includes('-e');
const cleanMarkdown = name => name.replace(/([\\\/_*|-])/g, '\\$1');
const directoryName = name => {
return '- ' + (emoji ? '📂 ' : '') + '__' + cleanMarkdown(name) + '__\n';
};
const filename = (name, path) => {
const link = path.replace(/^\/?(.+?)\/?$/, '$1') + '/' + encodeURIComponent(name);
return '- ' + (emoji ? '📄 ' : '') + '[' + cleanMarkdown(name) + '](' + link.replace(/^\/?(.+?)$/, '$1') + ')\n';
};
const addIndentation = i => {
return ' '.repeat(i * 2 + 1);
};
const main = () => {
const dirPath = process.cwd();
const dir = dirPath.split('/').pop();
let indentation = 0;
let output = directoryName(dir);
const parseResult = result => {
indentation++;
Object.keys(result).sort().forEach(key => {
const data = result[key];
if (typeof data === 'string' && key[0] !== '.') {
const path = data.split('/');
output += addIndentation(indentation) + filename(path.pop(), path.join('/'));
} else if (typeof data === 'object') {
output += addIndentation(indentation) + directoryName(key);
parseResult(data);
indentation--;
}
});
};
tree(dirPath, (err, result) => {
parseResult(result);
console.log(output);
});
};
main();