Skip to content

Commit a91d283

Browse files
committed
Add generators
1 parent f618634 commit a91d283

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

lib/cli.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ require('yargs')
109109
}),
110110
handler: argv => run.start(argv.path, argv.config, argv.port)
111111
})
112+
.command({
113+
command: 'gen <path>',
114+
aliases: 't',
115+
desc: chalk.gray(y18n.__('gen')),
116+
builder: yargs =>
117+
yargs.options({
118+
sidebar: {
119+
alias: 's',
120+
default: '_sidebar.md',
121+
desc: chalk.gray(y18n.__('gen.sidebar')),
122+
nargs: 1,
123+
requiresArg: true,
124+
type: 'string'
125+
}
126+
}),
127+
handler: argv => run.gen(argv.path, argv.sidebar)
128+
})
112129
.help()
113130
.option('help', {
114131
alias: 'h',

lib/commands/gen.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict'
2+
3+
const fs = require('fs')
4+
const {cwd, exists} = require('../util')
5+
const chalk = require('chalk')
6+
const path = require('path')
7+
8+
// eslint-disable-next-line
9+
module.exports = function (path = '', sidebar) {
10+
const cwdPath = cwd(path || '.')
11+
12+
if (exists(cwdPath)) {
13+
if (sidebar) {
14+
console.log(sidebar)
15+
const sidebarPath = cwdPath + '/' + sidebar || '_sidebar.md'
16+
17+
if (!exists(sidebarPath)) {
18+
genSidebar(cwdPath, sidebarPath)
19+
return true
20+
}
21+
22+
console.log(chalk.red(`${sidebarPath}`) + ' already exists.')
23+
return false
24+
}
25+
}
26+
27+
console.log(chalk.red(`${cwdPath}`) + ' directory does not exist.')
28+
}
29+
30+
function genSidebar(cwdPath, sidebarPath) {
31+
let node = ''
32+
travel(cwdPath, function (pathname) {
33+
path.relative(pathname, cwdPath)
34+
pathname = pathname.replace(cwdPath + '/', '')
35+
node += '* [' + path.basename(pathname, '.md') + '](' + pathname + ')\n'
36+
})
37+
fs.writeFile(sidebarPath, node, 'utf8', err => {
38+
if (err) {
39+
throw err
40+
}
41+
42+
console.log('gen success')
43+
})
44+
}
45+
46+
function travel(dir, callback) {
47+
fs.readdirSync(dir).forEach(function (file) {
48+
let pathname = path.join(dir, file)
49+
50+
if (fs.statSync(pathname).isDirectory()) {
51+
travel(pathname, callback)
52+
} else if (path.extname(file) === '.md') {
53+
callback(pathname)
54+
}
55+
})
56+
}

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
init: require('./commands/init'),
33
serve: require('./commands/serve'),
4-
start: require('./commands/start')
4+
start: require('./commands/start'),
5+
gen: require('./commands/gen')
56
}

tools/locales/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"serve.open": "Open docs in default browser. To explicitly set --open to false you may use --no-open.",
1111
"serve.port": "Listen port.",
1212
"serve.indexname": "Custom filename instead of index.html to serve by default",
13+
"gen": "Docsify's generators",
14+
"gen.sidebar": "Generate sidebar file",
1315
"livereload.port": "livereload Listen port.",
1416
"usage": "Usage",
1517
"version": "Show version number"

tools/locales/zh.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"serve.open": "自动打开浏览器",
1111
"serve.port": "设置端口",
1212
"serve.indexname": "Custom filename instead of index.html to serve by default",
13+
"gen": "Docsify的生成器",
14+
"gen.sidebar": "生成侧边栏文件",
1315
"livereload.port": "设置livereload端口",
1416
"usage": "例子",
1517
"version": "当前版本号"

0 commit comments

Comments
 (0)