-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.js
More file actions
executable file
·34 lines (31 loc) · 1.12 KB
/
tree.js
File metadata and controls
executable file
·34 lines (31 loc) · 1.12 KB
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
#!/usr/bin/env node
const { join } = require('path')
const { lstatSync, readdirSync, writeFileSync } = require('fs')
const TARGET_FILE = 'README.md'
const TARGET_DIR = '.'
let markup = '### The palest ink is better than the best memory(maybe)\n\n'
let indent = 0
const addIndent = () => new Array(indent * 2 + 1).join(' ')
const ignore = source => !/(README.md)|(tree.js)/g.test(source)
const isDirectory = source => lstatSync(source).isDirectory()
const isUnixHiddenPath = source => !/(^|\/)\.[^\/\.]/g.test(source)
const parseDirectory = source => `${addIndent()}- **${source}**\n`
const parseFile = (source, path) => `${addIndent()}- [${source}](${path})\n`
const parseResult = (result, source) => {
result.forEach(name => {
const path = join(source, name)
if (isDirectory(path)) {
markup += parseDirectory(name)
indent++
parseResult(readdirSync(path).filter(isUnixHiddenPath), path)
indent--
} else {
markup += parseFile(name, path)
}
})
}
const ret = readdirSync(TARGET_DIR)
.filter(isUnixHiddenPath)
.filter(ignore)
parseResult(ret, TARGET_DIR)
writeFileSync(TARGET_FILE, markup)