Skip to content

Commit feefcce

Browse files
authored
feat: add start command (#14)
1 parent 972a6ec commit feefcce

File tree

8 files changed

+108
-3
lines changed

8 files changed

+108
-3
lines changed

bin/docsify

+23
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,29 @@ var yargs = require('yargs')
6868
}),
6969
handler: (argv) => run.serve(argv.path, argv.open, argv.port)
7070
})
71+
.command({
72+
command: 'start <path>',
73+
desc: chalk.gray(y18n.__('start')),
74+
builder: (yargs) => yargs.options({
75+
'config': {
76+
alias: 'c',
77+
default: false,
78+
desc: chalk.gray(y18n.__('start.config')),
79+
nargs: 0,
80+
requiresArg: false,
81+
type: 'string'
82+
},
83+
'port': {
84+
alias: 'p',
85+
default: 4000,
86+
desc: chalk.gray(y18n.__('start.port')),
87+
nargs: 1,
88+
requiresArg: true,
89+
type: 'number'
90+
}
91+
}),
92+
handler: (argv) => run.start(argv.path, argv.config, argv.port)
93+
})
7194
.help()
7295
.option('help', {
7396
alias: 'h',

lib/commands/init.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ var exists = util.exists
99
var cwd = util.cwd
1010
var pwd = util.pwd
1111
var resolve = util.resolve
12+
var read = util.read
1213

1314
var replace = function (file, tpl, replace) {
14-
fs.writeFileSync(file, fs.readFileSync(file).toString().replace(tpl, replace), 'utf-8')
15+
fs.writeFileSync(file, read(file).replace(tpl, replace), 'utf-8')
1516
}
1617

17-
var PKG = exists(cwd('package.json')) ? require(cwd('package.json')) : {}
18+
var PKG = util.pkg()
1819

1920
module.exports = function (path, local, theme) {
2021
path = path || '.'

lib/commands/start.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict'
2+
3+
const connect = require('connect')
4+
const serveStatic = require('serve-static')
5+
const Renderer = require('docsify-server-renderer')
6+
const fs = require('fs')
7+
const util = require('../util/index')
8+
const chalk = require('chalk')
9+
10+
var defaultConfig = {
11+
template: `<!DOCTYPE html>
12+
<html lang="en">
13+
<head>
14+
<meta charset="UTF-8">
15+
<title>My Doc</title>
16+
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
17+
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
18+
</head>
19+
<body>
20+
<!--inject-app-->
21+
<!--inject-config-->
22+
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
23+
</body>
24+
</html>`,
25+
path: './'
26+
}
27+
28+
function loadConfig (config) {
29+
try {
30+
return require(util.cwd(config))
31+
} catch (e) {
32+
console.log(chalk.red(`Not found ${config}`))
33+
process.exit(1)
34+
}
35+
}
36+
37+
module.exports = function (path, configFile, port) {
38+
let config = defaultConfig
39+
const pkg = util.pkg()
40+
41+
if (configFile) {
42+
config = loadConfig(configFile)
43+
} else if (pkg.docsify) {
44+
config = pkg.docsify
45+
config.template = util.exists(util.cwd(pkg.docsify.template))
46+
? util(pkg.docsify.template)
47+
: defaultConfig.template
48+
}
49+
50+
var renderer = new Renderer(config)
51+
var server = connect()
52+
53+
server.use(function(req, res) {
54+
renderer.renderToString(req.url)
55+
.then(res.end(html))
56+
.catch(res.end(util.read(util.resolve(path, 'index.html'))))
57+
})
58+
server.use(serveStatic(path || '.'))
59+
server.listen(port || 4000)
60+
61+
const msg = '\n'
62+
+ chalk.blue('[SSR]')
63+
+ ' Serving ' + chalk.green(`${path}`) + ' now.\n'
64+
+ 'Listening at ' + chalk.green(`http://localhost:${port}`) + '\n'
65+
66+
console.log(msg)
67+
}

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
init: require('./commands/init'),
3-
serve: require('./commands/serve')
3+
serve: require('./commands/serve'),
4+
start: require('./commands/start')
45
}

lib/util/index.js

+10
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@ exports.exists = function (path) {
1818
}
1919
return undefined
2020
}
21+
22+
exports.pkg = function () {
23+
return exports.exists(exports.cwd('package.json'))
24+
? require(exports.cwd('package.json'))
25+
: {}
26+
}
27+
28+
exports.read = function (path) {
29+
return fs.readFileSync(path, 'utf-8').toString()
30+
}

tools/locales/de.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"gulp.release": "Veröffentliche auf Github.",
1212
"help": "Zeige Hilfe an",
1313
"init": "Erzeuge neue Dokumentation.",
14+
"start": "Server for SSR",
1415
"init.local": "Kopiere docsify Dateien in lokale Ordner. Um explizit --local auf false zu setzen, kannst du --no-local verwenden.",
1516
"init.theme": "Zu verwendende Theme Dateien.",
1617
"serve": "Lasse lokalen Server zur Webseitenvorschau laufen.",

tools/locales/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"gulp.release": "Releases on Github.",
1212
"help": "Show help",
1313
"init": "Creates new docs",
14+
"start": "Server for SSR",
1415
"init.local": "Copy docsify files to local. To explicitly set --local to false you may use --no-local.",
1516
"init.theme": "Theme file to be used.",
1617
"serve": "Run local server to preview site.",

tools/locales/zh.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"gulp.lint": "Runs eslint.",
1111
"gulp.release": "Releases on Github.",
1212
"help": "帮助",
13+
"start": "Server for SSR",
1314
"init": "创建 docs",
1415
"init.local": "拷贝 docsify 到本地",
1516
"init.theme": "选择主题",

0 commit comments

Comments
 (0)