-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathserve.js
53 lines (44 loc) · 1.33 KB
/
serve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict'
const serveStatic = require('serve-static')
const connect = require('connect')
const livereload = require('connect-livereload')
const lrserver = require('livereload')
const opn = require('opn')
const chalk = require('chalk')
const util = require('../util/index')
var exists = util.exists
var resolve = util.resolve
module.exports = function (path, openInBrowser, port, livereloadPort, basePath) {
path = resolve(path || '.')
var indexFile = resolve(path, 'index.html')
const subPath = normalizePath(basePath)
if (!exists(indexFile)) {
const msg = '\nNo docs found, please run ' + chalk.green('docsify init') + ' first.\n'
console.log(msg)
process.exit(0)
}
var server = connect()
server.use(livereload({
port: livereloadPort
}))
server.use(subPath, serveStatic(path))
server.listen(port)
lrserver.createServer({
extraExts: ['md'],
exclusions: ['node_modules/'],
port: livereloadPort
}).watch(path)
const url = `http://localhost:${port}${subPath}`
if (openInBrowser) {
opn(url)
}
const msg = '\nServing ' + chalk.green(`${path}`) + ' now.\n' +
'Listening at ' + chalk.green(url) + '\n'
console.log(msg)
}
function normalizePath (basePath) {
if (basePath === '.' || basePath === '') {
return ''
}
return basePath[0] === '/' ? basePath : '/' + basePath
}