forked from tapio/live-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive-server.js
More file actions
executable file
·138 lines (130 loc) · 3.85 KB
/
live-server.js
File metadata and controls
executable file
·138 lines (130 loc) · 3.85 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env node
var path = require('path');
var fs = require('fs');
var assign = require('object-assign');
var liveServer = require("./index");
var opts = {
port: process.env.PORT,
open: true,
mount: [],
logLevel: 2
};
var homeDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
var configPath = path.join(homeDir, '.live-server.json');
if (fs.existsSync(configPath)) {
var userConfig = fs.readFileSync(configPath, 'utf8');
assign(opts, JSON.parse(userConfig));
}
for (var i = process.argv.length - 1; i >= 2; --i) {
var arg = process.argv[i];
if (arg.indexOf("--port=") > -1) {
var portString = arg.substring(7);
var portNumber = parseInt(portString, 10);
if (portNumber == portString) {
opts.port = portNumber;
process.argv.splice(i, 1);
}
}
else if (arg.indexOf("--host=") > -1) {
opts.host = arg.substring(7);
process.argv.splice(i, 1);
}
else if (arg.indexOf("--open=") > -1) {
var open = arg.substring(7);
if (open.indexOf('/') !== 0) {
open = '/' + open;
}
opts.open = open;
process.argv.splice(i, 1);
}
else if (arg.indexOf("--watch=") > -1) {
// Will be modified later when cwd is known
opts.watch = arg.substring(8).split(",");
process.argv.splice(i, 1);
}
else if (arg.indexOf("--ignore=") > -1) {
// Will be modified later when cwd is known
opts.ignore = arg.substring(9).split(",");
process.argv.splice(i, 1);
}
else if (arg.indexOf("--ignorePattern=") > -1) {
opts.ignorePattern = new RegExp(arg.substring(16));
process.argv.splice(i, 1);
}
else if (arg == "--no-browser") {
opts.open = false;
process.argv.splice(i, 1);
}
else if (arg.indexOf("--browser=") > -1) {
opts.browser = arg.substring(10).split(",");
process.argv.splice(i, 1);
}
else if (arg.indexOf("--entry-file=") > -1) {
var file = arg.substring(13);
if (file.length) {
opts.file = file;
process.argv.splice(i, 1);
}
}
else if (arg == "--quiet" || arg == "-q") {
opts.logLevel = 0;
process.argv.splice(i, 1);
}
else if (arg.indexOf("--mount=") > -1) {
// e.g. "--mount=/components:./node_modules" will be ['/components', '<process.cwd()>/node_modules']
var mountRule = arg.substring(8).split(":");
mountRule[1] = path.resolve(process.cwd(), mountRule[1]);
opts.mount.push(mountRule);
process.argv.splice(i, 1);
}
else if (arg.indexOf("--wait=") > -1) {
var waitString = arg.substring(7);
var waitNumber = parseInt(waitString, 10);
if (waitNumber == waitString) {
opts.wait = waitNumber;
process.argv.splice(i, 1);
}
}
else if (arg == "--version" || arg == "-v") {
var packageJson = require('./package.json');
console.log(packageJson.name, packageJson.version);
process.exit();
}
else if (arg.indexOf("--htpasswd=") > -1) {
opts.htpasswd = arg.substring(11);
process.argv.splice(i, 1);
}
else if (arg == "--help" || arg == "-h") {
console.log('Usage: live-server [-v|--version] [-h|--help] [-q|--quiet] [--port=PORT] [--host=HOST] [--open=PATH] [--no-browser] [--browser=BROWSER] [--ignore=PATH] [--ignorePattern=RGXP] [--entry-file=PATH] [--mount=ROUTE:PATH] [--wait=MILLISECONDS] [--htpasswd=PATH] [PATH]');
process.exit();
}
else if (arg == "--test") {
// Hidden param for tests to exit automatically
setTimeout(liveServer.shutdown, 500);
process.argv.splice(i, 1);
}
else if (arg == "--init") {
var cfgTmpl = fs.readFileSync(path.join(__dirname, 'live-server-cfg-tmpl.json'));
process.argv.splice(i, 1);
}
}
if (process.argv[2]) {
process.chdir(process.argv[2]);
}
// Patch paths
var cwd = process.cwd();
if (cfgTmpl) {
fs.writeFileSync(path.join(cwd, '.live-server.json'), cfgTmpl);
process.exit();
}
if (opts.watch) {
opts.watch = opts.watch.map(function(relativePath) {
return path.join(cwd, relativePath);
});
}
if (opts.ignore) {
opts.ignore = opts.ignore.map(function(relativePath) {
return path.join(cwd, relativePath);
});
}
liveServer.start(opts);