-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoProduceReadme.js
92 lines (85 loc) · 2.95 KB
/
autoProduceReadme.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs')
const Path = require('path')
const IGNORE_STAT_NAME = 'ignoreStat.txt'
const isDirectory = (path) => {
const stats = fs.statSync(path)
return stats.isDirectory()
}
const getFileName = (path) => {
const arr = path.split('/')
return arr[arr.length - 1]
}
const isMarkDown = (path) => {
const filename = getFileName(path)
const arr = filename.split('.')
return arr[arr.length - 1] === 'md'
}
const markdownTitle = (activeLayer, isLink) => {
let title = '##'
let i = -1
let prevSpace = ''
while(i<activeLayer){
i++
title+='#'
prevSpace += ''
}
if(isLink) title += '#'
return title + prevSpace
}
const markdownIndex = (activeLayer, isLink) => {
let index = '*'
let i = -1
let prevSpace = ''
while(i<activeLayer){
i++
// prevSpace += ' '
}
// if(isLink) title += '#'
return prevSpace + index
}
const transfer2RelativePath = (path) => {
const startIdx = path.indexOf(__dirname)
const endIdx = __dirname.length + startIdx
const filepath = path.slice(0,startIdx) + path.slice(endIdx)
const filepathWithoutSpace = Array.prototype.map.call(filepath,(char => char === ' ' ? '%20' : char)).join('')
return filepathWithoutSpace
}
const rmFilenameSapce = (filename) => {
return Array.prototype.filter.call(filename,(char => char !== ' ')).join('')
}
const getIgnoreList = (path) => {
const rawTxt = fs.readFileSync(path,{encoding:'utf-8'})
console.log(`get`,path,rawTxt)
return rawTxt.split('\n')
}
const isIgnoreStatFile = (path) => {
const filename = getFileName(path)
return filename === IGNORE_STAT_NAME
}
let rootReadme = `# Share And Note \n\n
### 写在前面 \n\n
本仓库是作者对一些框架原理、源码阅读、Web标准等前端技术的日常记录,若您发现文章中有错误的地方,请不吝赐教,作者学习后一定会及时改正.
`
const traversal = (subpath, layer = 0) => {
const filenames = fs.readdirSync(subpath)
const ignoreStatFile = filenames.find(filename => isIgnoreStatFile(filename))
const ignoreList = ignoreStatFile ? getIgnoreList(Path.join(subpath, ignoreStatFile)) : []
for(const filename of filenames){
if(ignoreList.includes(filename)) continue
const filepath = Path.join(subpath,`./${filename}`)
if(isDirectory(filepath)){
// TODO: add sub title when there are too many articles
if(layer === 0){
rootReadme += `${markdownTitle(layer)} ${rmFilenameSapce(getFileName(filepath))} \n`
}
traversal(filepath, layer + 1)
}else if(isMarkDown(filepath)){
rootReadme += `${markdownIndex(layer, true)} [${rmFilenameSapce(getFileName(filepath))}](${transfer2RelativePath(filepath)}) \n`
}
}
if(layer === 0){
rootReadme += '\n'
}
}
traversal(Path.join(__dirname, './'), 0)
fs.writeFileSync(Path.join(__dirname,'./readme.md'),rootReadme)