-
Notifications
You must be signed in to change notification settings - Fork 73
/
up
executable file
·242 lines (190 loc) · 4.58 KB
/
up
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env node
/**
* Force debug.
*/
process.env.DEBUG = (process.env.DEBUG ? ',' : '') + 'up,up-cli';
/**
* Module dependencies.
*/
var up = require('../lib/up')
, spawn = require('child_process').spawn
, program = require('commander')
, http = require('http')
, path = require('path')
, os = require('os')
, eq = require('eq')
, ms = require('ms')
, fs = require('fs')
, debug = require('debug')('up-cli')
/**
* Number of CPUs available.
*/
var cpus = require('os').cpus().length;
/**
* Set up CLI.
*/
program
.version(up.version)
.usage('[options] <file>')
.option('-p, --port <port>', 'Port to listen on.', 3000)
.option('-f, --pidfile <pidfile>', 'Write port to pidfile')
.option('-w, --watch', 'Watch the module directory for changes.')
.option('-r, --require <file>', 'Module to require from each worker.')
.option('-n, --number <workers>', 'Number of workers to spawn.'
, 'development' == process.env.NODE_ENV ? 1 : cpus)
.option('-t, --timeout [ms]', 'Worker timeout.')
/**
* Capture requires.
*/
var requires = [];
program.on('require', function (file) {
requires.push(file);
});
/**
* Parse argv.
*/
program.parse(process.argv);
/**
* Helper function to exit with an error.
*
* @api private
*/
function error () {
console.error.apply(console, arguments);
process.exit(1);
};
/**
* Parse module.
*/
var file = program.args[0]
, server
// verify a file is supplied
if (!file) error(program.helpInformation());
// absolutize
if ('/' != file[0]) file = process.cwd() + '/' + file;
// verify we can require
try {
server = require(file);
} catch (e) {
error('\n Error requiring supplied module "%s".\n %s\n'
, file, e.stack);
}
// verify it's a valid server
if (!(server instanceof http.Server)) {
error('\n Module supplied "%s" does not export a valid `http.Server`.\n'
, file);
}
/**
* Parse port
*/
var port;
if (null != program.port) {
port = Number(program.port);
if (!port || isNaN(port)) {
error('\n Invalid port "%s" (%d).\n', program.port, program.port);
}
}
/**
* Parse number of workers.
*/
var numWorkers = eq(program.number, { cpus: cpus });
if (!numWorkers || isNaN(numWorkers)) {
error('\n Supplied number of workers "%s" (%s) is invalid.\n'
, program.number, numWorkers);
}
/**
* Parse timouet
*/
var workerTimeout = program.timeout;
if (null != workerTimeout && isNaN(ms(workerTimeout))) {
error('\n Supplied worker timeout "%s" (%s) is invalid.\n'
, program.timeout, ms(workerTimeout));
}
/**
* Start!
*/
debug('starting cluster with %d workers on port %d', numWorkers, port);
debug('`\033[97mkill -s SIGUSR2 %d\033[90m` to load new code', process.pid);
var httpServer = http.Server().listen(program.port)
, srv = up(httpServer, file, {
numWorkers: numWorkers
, workerTimeout: workerTimeout
, requires: requires
})
/**
* Write pidfile
*/
if (program.pidfile) {
fs.writeFile(program.pidfile, process.pid, function(err) {
if (err) {
error('\n Error writing pidfile: "%s"\n', program.pidfile, err);
}
});
}
/**
* Listen on SIGUSR2 signal.
*/
process.on('SIGUSR2', function () {
debug('\033[97mSIGUSR2\033[90m signal detected - reloading');
srv.reload();
});
/**
* Watch.
*/
if (undefined != program.watch) {
// from mocha/utils - released under MIT - copyright TJ Holowaychuk
/**
* Ignored directories.
*/
var ignore = ['node_modules', '.git'];
/**
* Ignored files.
*/
function ignored (path) {
return !~ignore.indexOf(path);
};
/**
* Lookup files in the given `dir`.
*
* @return {Array}
* @api public
*/
function files (dir, ret) {
ret = ret || [];
fs.readdirSync(dir)
.filter(ignored)
.forEach(function(p){
p = path.join(dir, p);
if (fs.statSync(p).isDirectory()) {
files(p, ret);
} else if (p.match(/\.js$|\.coffee$/)) {
ret.push(p);
}
});
return ret;
};
/**
* Watch the given `files` for changes
* and invoke `fn(file)` on modification.
*
* @param {Array} files
* @param {Function} fn
* @api private
*/
function watch (files, fn){
var options = { interval: 100 };
files.forEach(function (file) {
fs.watchFile(file, options, function (curr, prev) {
if (prev.mtime < curr.mtime) fn(file);
});
});
};
/**
* Watch files in the path of script to run.
*/
debug('watching "%s" for changes', process.cwd());
watch(files(process.cwd()), function (file) {
debug('\033[97m%s\033[90m change detected - reloading', file);
srv.reload();
});
}