Skip to content

Commit

Permalink
feat: add livereload
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 9, 2017
1 parent 3d32be8 commit 661cbe2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 36 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
"license": "MIT",
"dependencies": {
"commander": "^2.9.0",
"connect": "^3.5.0",
"connect-livereload": "^0.6.0",
"cp-file": "^4.1.0",
"docsify": ">=2",
"livereload": "^0.6.0",
"serve-static": "^1.11.1",
"update-notifier": "^1.0.3"
},
Expand Down
45 changes: 9 additions & 36 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
var fs = require('fs')
var http = require('http')
var resolve = require('path').resolve
var cp = require('cp-file').sync
var serveStatic = require('serve-static')
var util = require('./util')

var exist = util.exist
var cwd = util.cwd
var pwd = util.pwd
var resolve = util.resolve
var green = util.green

var cwd = function (path) {
return resolve(process.cwd(), path)
}
var pwd = function (path) {
return resolve(__dirname, path)
}
var exist = function (path) {
if (fs.existsSync(path)) {
return path
}
return undefined
}
var replace = function (file, tpl, replace) {
fs.writeFileSync(file, fs.readFileSync(file).toString().replace(tpl, replace), 'utf-8')
}

var GREEN_OPEN = '\u001B[32m'
var GREEN_CLOSE = '\u001B[39m'
var PKG = exist(cwd('package.json')) ? require(cwd('package.json')) : {}

exports.init = function (path, option) {
path = path || '.'
var msg = `\nCreate succeed! Please run\n
> ${GREEN_OPEN}docsify serve ${path}${GREEN_CLOSE}\n`
> ${green(`docsify serve ${path}`)}\n`

path = cwd(path)
var target = function (file) {
Expand Down Expand Up @@ -66,21 +56,4 @@ exports.init = function (path, option) {
console.log(msg)
}

exports.serve = function (path, option) {
path = path || '.'
var indexFile = resolve(path, 'index.html')

if (!exist(indexFile)) {
console.log(`\nplease run ${GREEN_OPEN}init${GREEN_CLOSE} before.\n`)
process.exit(0)
}

http.createServer(function (req, res) {
serveStatic(path)(req, res, function () {
res.writeHead(404)
res.end()
})
}).listen(option.port)

console.log(`\nListening at ${GREEN_OPEN}http://localhost:${option.port}${GREEN_CLOSE}\n`)
}
exports.serve = require('./serve')
30 changes: 30 additions & 0 deletions src/serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var serveStatic = require('serve-static')
var connect = require('connect')
var livereload = require('connect-livereload')
var lrserver = require('livereload')
var util = require('./util')

var green = util.green
var exist = util.exist
var resolve = util.resolve

module.exports = function (path, option) {
path = resolve(path || '.')
var indexFile = resolve(path, 'index.html')

if (!exist(indexFile)) {
console.log(`\nplease run ${green('init')} before.\n`)
process.exit(0)
}

var server = connect()

server.use(livereload())
server.use(serveStatic(path))
server.listen(option.port)
lrserver.createServer({
exts: ['md']
}).watch(path)

console.log(`\nListening at ${green(`http://localhost:${option.port}`)}\n`)
}
22 changes: 22 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var fs = require('fs')

var resolve = exports.resolve = require('path').resolve

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

exports.pwd = function (path) {
return resolve(__dirname, path)
}

exports.exist = function (path) {
if (fs.existsSync(path)) {
return path
}
return undefined
}

exports.green = function (str) {
return '\u001B[32m' + str + '\u001B[39m'
}

0 comments on commit 661cbe2

Please sign in to comment.