Skip to content

Commit 661cbe2

Browse files
committed
feat: add livereload
1 parent 3d32be8 commit 661cbe2

File tree

4 files changed

+64
-36
lines changed

4 files changed

+64
-36
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
"license": "MIT",
3535
"dependencies": {
3636
"commander": "^2.9.0",
37+
"connect": "^3.5.0",
38+
"connect-livereload": "^0.6.0",
3739
"cp-file": "^4.1.0",
3840
"docsify": ">=2",
41+
"livereload": "^0.6.0",
3942
"serve-static": "^1.11.1",
4043
"update-notifier": "^1.0.3"
4144
},

src/index.js

+9-36
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
11
var fs = require('fs')
2-
var http = require('http')
3-
var resolve = require('path').resolve
42
var cp = require('cp-file').sync
5-
var serveStatic = require('serve-static')
3+
var util = require('./util')
4+
5+
var exist = util.exist
6+
var cwd = util.cwd
7+
var pwd = util.pwd
8+
var resolve = util.resolve
9+
var green = util.green
610

7-
var cwd = function (path) {
8-
return resolve(process.cwd(), path)
9-
}
10-
var pwd = function (path) {
11-
return resolve(__dirname, path)
12-
}
13-
var exist = function (path) {
14-
if (fs.existsSync(path)) {
15-
return path
16-
}
17-
return undefined
18-
}
1911
var replace = function (file, tpl, replace) {
2012
fs.writeFileSync(file, fs.readFileSync(file).toString().replace(tpl, replace), 'utf-8')
2113
}
2214

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

2717
exports.init = function (path, option) {
2818
path = path || '.'
2919
var msg = `\nCreate succeed! Please run\n
30-
> ${GREEN_OPEN}docsify serve ${path}${GREEN_CLOSE}\n`
20+
> ${green(`docsify serve ${path}`)}\n`
3121

3222
path = cwd(path)
3323
var target = function (file) {
@@ -66,21 +56,4 @@ exports.init = function (path, option) {
6656
console.log(msg)
6757
}
6858

69-
exports.serve = function (path, option) {
70-
path = path || '.'
71-
var indexFile = resolve(path, 'index.html')
72-
73-
if (!exist(indexFile)) {
74-
console.log(`\nplease run ${GREEN_OPEN}init${GREEN_CLOSE} before.\n`)
75-
process.exit(0)
76-
}
77-
78-
http.createServer(function (req, res) {
79-
serveStatic(path)(req, res, function () {
80-
res.writeHead(404)
81-
res.end()
82-
})
83-
}).listen(option.port)
84-
85-
console.log(`\nListening at ${GREEN_OPEN}http://localhost:${option.port}${GREEN_CLOSE}\n`)
86-
}
59+
exports.serve = require('./serve')

src/serve.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var serveStatic = require('serve-static')
2+
var connect = require('connect')
3+
var livereload = require('connect-livereload')
4+
var lrserver = require('livereload')
5+
var util = require('./util')
6+
7+
var green = util.green
8+
var exist = util.exist
9+
var resolve = util.resolve
10+
11+
module.exports = function (path, option) {
12+
path = resolve(path || '.')
13+
var indexFile = resolve(path, 'index.html')
14+
15+
if (!exist(indexFile)) {
16+
console.log(`\nplease run ${green('init')} before.\n`)
17+
process.exit(0)
18+
}
19+
20+
var server = connect()
21+
22+
server.use(livereload())
23+
server.use(serveStatic(path))
24+
server.listen(option.port)
25+
lrserver.createServer({
26+
exts: ['md']
27+
}).watch(path)
28+
29+
console.log(`\nListening at ${green(`http://localhost:${option.port}`)}\n`)
30+
}

src/util.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var fs = require('fs')
2+
3+
var resolve = exports.resolve = require('path').resolve
4+
5+
exports.cwd = function (path) {
6+
return resolve(process.cwd(), path)
7+
}
8+
9+
exports.pwd = function (path) {
10+
return resolve(__dirname, path)
11+
}
12+
13+
exports.exist = function (path) {
14+
if (fs.existsSync(path)) {
15+
return path
16+
}
17+
return undefined
18+
}
19+
20+
exports.green = function (str) {
21+
return '\u001B[32m' + str + '\u001B[39m'
22+
}

0 commit comments

Comments
 (0)