-
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathserve.js
More file actions
84 lines (75 loc) · 1.84 KB
/
serve.js
File metadata and controls
84 lines (75 loc) · 1.84 KB
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
79
80
81
82
83
84
'use strict'
const serveStatic = require('serve-static')
const connect = require('connect')
const livereload = require('connect-livereload')
const lrserver = require('livereload')
const open = require('open')
const chalk = require('chalk')
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(_ => {
const server = connect();
server.use(
livereload({
port: livereloadPort
})
);
let indexFileFound = false;
let indexFile;
path.split(",").forEach((path)=> {
path = resolve(path || ".");
indexFile = resolve(path, indexName || "index.html");
if (exists(indexFile)) {
indexFileFound = true
}
server.use(serveStatic(path, { index: indexName }));
});
if (!indexFileFound) {
const msg =
"\nNo docs found " +
indexFile +
"\nPlease run " +
chalk.green("docsify init") +
" first.\n";
console.log(msg);
process.exit(0);
}
server.listen(port);
lrserver
.createServer({
extraExts: ["md"],
exclusions: ["node_modules/"],
port: livereloadPort
})
.watch(path.split(","));
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 => {
console.error(err.message)
})
}