-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathgenerate.js
77 lines (65 loc) · 2.54 KB
/
generate.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
'use strict'
const fs = require('fs')
const os = require('os')
const {cwd, exists} = require('../util')
const path = require('path')
const logger = require('../util/logger')
const ignoreFiles = ['_navbar', '_coverpage', '_sidebar']
module.exports = function (path, sidebar, options) {
const cwdPath = cwd(path || '.')
if (exists(cwdPath)) {
if (sidebar) {
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md'
if (!exists(sidebarPath) || options.overwrite) {
genSidebar(cwdPath, sidebarPath)
logger.success(`Successfully generated the sidebar file '${sidebar}'.`)
return true
}
logger.error(`The sidebar file '${sidebar}' already exists.`)
process.exitCode = 1
return false
}
}
logger.error(`${cwdPath} directory does not exist.`)
}
function genSidebar(cwdPath, sidebarPath) {
const sidebarContent = generateContent(cwdPath, cwdPath).join(os.EOL)
fs.writeFileSync(sidebarPath, sidebarContent, 'utf8', err => {
if (err) {
logger.error(`Couldn't generate the sidebar file, error: ${err.message}`)
}
})
}
function generateContent(directory, rootPath) {
const content = []
fs.readdirSync(directory).forEach(file => {
const cwdPath = path.join(directory, file)
const relativePath = path.relative(rootPath, cwdPath)
const filePath = relativePath.replace(/\s/g, '%20')
const filename = modifyFileName(file)
if (fs.statSync(cwdPath).isDirectory()) {
const childContent = generateContent(cwdPath, rootPath) // Recursive call
const isReadmePresent = fs.existsSync(path.join(cwdPath, 'README.md'))
const hasChildContent = childContent.length > 0
if (hasChildContent && isReadmePresent) {
content.push(`- [${filename}](${filePath}/README.md)`, ...childContent.map(item => ` ${item}`))
} else if (hasChildContent && !isReadmePresent) {
content.push(`- ${filename}`, ...childContent.map(item => ` ${item}`))
} else if (!hasChildContent && isReadmePresent) {
content.push(`- [${filename}](${filePath}/README.md)`)
} else {
content.push(`- [${filename}](${filePath})`)
}
} else if (path.extname(file) === '.md' &&
file.toLowerCase() !== 'readme.md' &&
ignoreFiles.indexOf(filename) === -1) {
content.push(`- [${filename}](${filePath})`)
}
})
return content
}
function modifyFileName(file) {
const filename = file.split('-')
const fileWithExtension = filename.length > 1 ? filename[1] : filename[0]
return path.basename(fileWithExtension, '.md')
}