Skip to content

feat: add start command #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions bin/docsify
Original file line number Diff line number Diff line change
@@ -68,6 +68,29 @@ var yargs = require('yargs')
}),
handler: (argv) => run.serve(argv.path, argv.open, argv.port)
})
.command({
command: 'start <path>',
desc: chalk.gray(y18n.__('start')),
builder: (yargs) => yargs.options({
'config': {
alias: 'c',
default: false,
desc: chalk.gray(y18n.__('start.config')),
nargs: 0,
requiresArg: false,
type: 'string'
},
'port': {
alias: 'p',
default: 4000,
desc: chalk.gray(y18n.__('start.port')),
nargs: 1,
requiresArg: true,
type: 'number'
}
}),
handler: (argv) => run.start(argv.path, argv.config, argv.port)
})
.help()
.option('help', {
alias: 'h',
5 changes: 3 additions & 2 deletions lib/commands/init.js
Original file line number Diff line number Diff line change
@@ -9,12 +9,13 @@ var exists = util.exists
var cwd = util.cwd
var pwd = util.pwd
var resolve = util.resolve
var read = util.read

var replace = function (file, tpl, replace) {
fs.writeFileSync(file, fs.readFileSync(file).toString().replace(tpl, replace), 'utf-8')
fs.writeFileSync(file, read(file).replace(tpl, replace), 'utf-8')
}

var PKG = exists(cwd('package.json')) ? require(cwd('package.json')) : {}
var PKG = util.pkg()

module.exports = function (path, local, theme) {
path = path || '.'
67 changes: 67 additions & 0 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const connect = require('connect')
const serveStatic = require('serve-static')
const Renderer = require('docsify-server-renderer')
const fs = require('fs')
const util = require('../util/index')
const chalk = require('chalk')

var defaultConfig = {
template: `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My Doc</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<link rel="stylesheet" href="//unpkg.com/docsify/lib/themes/vue.css" title="vue">
</head>
<body>
<!--inject-app-->
<!--inject-config-->
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
</body>
</html>`,
path: './'
}

function loadConfig (config) {
try {
return require(util.cwd(config))
} catch (e) {
console.log(chalk.red(`Not found ${config}`))
process.exit(1)
}
}

module.exports = function (path, configFile, port) {
let config = defaultConfig
const pkg = util.pkg()

if (configFile) {
config = loadConfig(configFile)
} else if (pkg.docsify) {
config = pkg.docsify
config.template = util.exists(util.cwd(pkg.docsify.template))
? util(pkg.docsify.template)
: defaultConfig.template
}

var renderer = new Renderer(config)
var server = connect()

server.use(function(req, res) {
renderer.renderToString(req.url)
.then(res.end(html))
.catch(res.end(util.read(util.resolve(path, 'index.html'))))
})
server.use(serveStatic(path || '.'))
server.listen(port || 4000)

const msg = '\n'
+ chalk.blue('[SSR]')
+ ' Serving ' + chalk.green(`${path}`) + ' now.\n'
+ 'Listening at ' + chalk.green(`http://localhost:${port}`) + '\n'

console.log(msg)
}
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
init: require('./commands/init'),
serve: require('./commands/serve')
serve: require('./commands/serve'),
start: require('./commands/start')
}
10 changes: 10 additions & 0 deletions lib/util/index.js
Original file line number Diff line number Diff line change
@@ -18,3 +18,13 @@ exports.exists = function (path) {
}
return undefined
}

exports.pkg = function () {
return exports.exists(exports.cwd('package.json'))
? require(exports.cwd('package.json'))
: {}
}

exports.read = function (path) {
return fs.readFileSync(path, 'utf-8').toString()
}
1 change: 1 addition & 0 deletions tools/locales/de.json
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
"gulp.release": "Veröffentliche auf Github.",
"help": "Zeige Hilfe an",
"init": "Erzeuge neue Dokumentation.",
"start": "Server for SSR",
"init.local": "Kopiere docsify Dateien in lokale Ordner. Um explizit --local auf false zu setzen, kannst du --no-local verwenden.",
"init.theme": "Zu verwendende Theme Dateien.",
"serve": "Lasse lokalen Server zur Webseitenvorschau laufen.",
1 change: 1 addition & 0 deletions tools/locales/en.json
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
"gulp.release": "Releases on Github.",
"help": "Show help",
"init": "Creates new docs",
"start": "Server for SSR",
"init.local": "Copy docsify files to local. To explicitly set --local to false you may use --no-local.",
"init.theme": "Theme file to be used.",
"serve": "Run local server to preview site.",
1 change: 1 addition & 0 deletions tools/locales/zh.json
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
"gulp.lint": "Runs eslint.",
"gulp.release": "Releases on Github.",
"help": "帮助",
"start": "Server for SSR",
"init": "创建 docs",
"init.local": "拷贝 docsify 到本地",
"init.theme": "选择主题",