-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·174 lines (118 loc) · 3.83 KB
/
index.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
"use strict";
//
// Dependencies
//
var fs = require('fs');
var path = require('path');
var tinylr = require('tiny-lr');
var program = require('commander');
var colors = require('colors/safe');
var pkg = require('./package.json');
//
// Command Line Options
//
program
.version(pkg.version)
.usage('[options] <directory>')
.option('--host [host]', 'the host to bind to [localhost]')
.option('--port [port]', 'the port to bind to [8080]')
.option('--live-reload-port [port]', 'the port to start LiveReload on [35729]')
.option('--no-color', 'disable colored output')
.option('--ssl', 'enable ssl/https')
.option('--key [key]', 'path to secure key [~/.ssh/dev/key.pem]')
.option('--cert [cert]', 'path to cert key [~/.ssh/dev/cert.pem]')
.parse(process.argv);
//
// Define Variables
//
var httpPort = (typeof program.port != 'undefined') ? program.port : 8080;
var liveReloadPort = (typeof program.liveReloadPort != 'undefined') ? program.liveReloadPort : 35729;
//
// Define Directories
//
var directories = (program.args.length == 0) ? ['./'] : program.args;
console.info();
console.info(colors.cyan.underline('Watching these directories:'));
for (var key in directories) {
console.info(" ", colors.white(directories[key]));
}
//
// Setup File Serving via Express
//
var express = require('express');
var app = express();
// Add directories
for (var key in directories) {
app.use(express.static(directories[key]));
}
var server = app;
//
// SSL/HTTPS Support (--ssl option)
//
if (program.ssl) {
var keyPath = (typeof program.key != 'undefined') ? program.key : process.env.HOME + '/.ssh/dev.key.pem';
var certPath = (typeof program.cert != 'undefined') ? program.cert : process.env.HOME + '/.ssh/dev.cert.pem';
//
// openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 3001
// openssl req -nodes -new -x509 -keyout key.pem -out cert.pem -days 365
//
var options = {
key: fs.readFileSync(keyPath).toString(),
cert: fs.readFileSync(certPath).toString(),
passphrase: 'test'
};
var https = require('https');
server = https.createServer(options, app);
}
server
.listen(httpPort, program.host)
.on('error', function (err) {
if (err.code == 'EADDRINUSE') {
console.error();
console.error(colors.red.bold('You already have a server listening on port [%s]'), httpPort);
console.error(colors.yellow((program.ssl ? 'HTTPS' : 'HTTP') + ' server has been disabled'));
console.error();
return;
}
console.error();
console.error(colors.red(err.stack));
console.error();
});
console.info();
console.info(colors.green((program.ssl ? 'HTTPS' : 'HTTP') + ' server listening on port [%d]'), httpPort);
//
// Create LiveReload Server
//
var reloadServer = new tinylr.Server();
reloadServer.error = function (err) {
if (err.code == 'EADDRINUSE') {
console.error();
console.error(colors.red.bold('You already have a server listening on port [%s]'), liveReloadPort);
console.error(colors.yellow('LiveReload has been disabled'));
console.error();
return;
}
console.error();
console.error(colors.red(err.stack));
console.error();
};
reloadServer.listen(liveReloadPort, function () {
console.info(colors.green('Livereload listening on port [%d]'), liveReloadPort);
console.info();
});
//
// Watch Directories
//
var watchers = [];
for (var key in directories) {
var watcher = (function (directory) {
return fs.watch(directory, function (event, filename) {
var filepath = path.join(directory, filename);
console.info(' ', colors.grey(event), colors.white(filepath));
// Signal LiveReload server that file(s) have changed
reloadServer.changed({ body: { files: [filepath] } });
});
})(directories[key]);
watchers.push(watcher);
}