Skip to content

Commit

Permalink
fix: path => context
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed May 29, 2017
1 parent 1227451 commit d83fa69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
25 changes: 18 additions & 7 deletions lib/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,51 @@ var defaultConfig = {
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
</body>
</html>`,
path: './'
context: './'
}

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

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

path = path || './'

if (configFile) {
config = loadConfig(configFile)
ctx = config.context || ctx
config.template = /\.html$/.test(config.template)
? util.read(util.resolve(ctx, config.template))
: config.template
} else if (pkg.docsify) {
config = pkg.docsify
config.template = util.exists(util.cwd(pkg.docsify.template))
config.template = util.exists(util.resolve(ctx, pkg.docsify.template))
? util(pkg.docsify.template)
: defaultConfig.template
}

var renderer = new Renderer(config)
var renderer = new Renderer(Object.assign(defaultConfig, 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'))))
.then(function (html) {
res.end(html)
})
.catch(function (err) {
res.end(util.read(util.resolve(path, 'index.html')))
})
})
server.use(serveStatic(path || '.'))
server.use(serveStatic(path))
server.listen(port || 4000)

const msg = '\n'
Expand Down
2 changes: 1 addition & 1 deletion lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var fs = require('fs')
var resolve = exports.resolve = require('path').resolve

exports.cwd = function (path) {
return resolve(process.cwd(), path)
return resolve(process.cwd(), path || '.')
}

exports.pwd = function (path) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"connect-livereload": "^0.6.0",
"cp-file": "^4.1.1",
"docsify": ">=3",
"docsify-server-renderer": "^4.0.0",
"fs-extra": "^2.1.2",
"livereload": "^0.6.2",
"open": "^0.0.5",
Expand Down

0 comments on commit d83fa69

Please sign in to comment.