forked from docsifyjs/docsify-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
78 lines (72 loc) · 1.82 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict'
const serveStatic = require('serve-static')
const connect = require('connect')
const livereload = require('connect-livereload')
const history = require('connect-history-api-fallback')
const lrserver = require('livereload')
const open = require('open')
const chalk = require('chalk')
const logger = require('../util/logger')
const {exists, resolve} = require('../util')
const getPort = require('get-port')
module.exports = function (
path,
openInBrowser,
port,
livereloadPort,
indexName
) {
getPort({port})
.then(p => {
port = p
return getPort({port: livereloadPort})
})
.then(p => {
livereloadPort = p
})
.then(_ => {
path = resolve(path || '.')
const indexFileName = indexName || 'index.html'
const indexFile = resolve(path, indexFileName)
if (!exists(indexFile)) {
const msg =
'\nNo docs found ' +
indexFile +
'\nPlease run ' +
chalk.green('docsify init') +
' first.\n'
console.log(msg)
process.exit(0)
}
const server = connect()
server.use(
livereload({
port: livereloadPort
})
)
server.use(history({index: '/' + indexFileName}))
server.use(serveStatic(path, {index: indexName}))
server.listen(port)
lrserver
.createServer({
extraExts: ['md'],
exclusions: ['node_modules/'],
port: livereloadPort
})
.watch(path)
if (openInBrowser) {
open(`http://localhost:${port}`)
}
const msg =
'\nServing ' +
chalk.green(`${path}`) +
' now.\n' +
'Listening at ' +
chalk.green(`http://localhost:${port}`) +
'\n'
console.log(msg)
})
.catch(err => {
logger.error(err.message)
})
}