-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathserver.js
166 lines (144 loc) · 5.04 KB
/
server.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
/*
* grunt-express-server
* https://github.com/ericclemmons/grunt-express-server
*
* Copyright (c) 2013 Eric Clemmons
* Licensed under the MIT license.
*/
'use strict';
var spawn = require('child_process').spawn;
module.exports = function(grunt, target) {
if (!process._servers) {
process._servers = {};
}
var backup = null;
var done = null;
var server = process._servers[target]; // Store server between live reloads to close/restart express
var finished = function() {
if (done) {
done();
done = null;
}
};
return {
start: function start(options) {
if (server) {
this.stop(options);
if (grunt.task.current.flags.stop) {
finished();
return;
}
}
backup = JSON.parse(JSON.stringify(process.env)); // Clone process.env
// For some weird reason, on Windows the process.env stringify produces a "Path"
// member instead of a "PATH" member, and grunt chokes when it can't find PATH.
if (!backup.PATH) {
if (backup.Path) {
backup.PATH = backup.Path;
delete backup.Path;
}
}
grunt.log.writeln('Starting '.cyan + (options.background ? 'background' : 'foreground') + ' Express server');
done = grunt.task.current.async();
// Set PORT for new processes
process.env.PORT = options.port;
// Set NODE_ENV for new processes
if (options.node_env) {
process.env.NODE_ENV = options.node_env;
}
if (options.cmd === 'coffee') {
grunt.log.writeln('You are using cmd: coffee'.red);
grunt.log.writeln('coffee does not allow a restart of the server'.red);
grunt.log.writeln('use opts: ["path/to/your/coffee"] instead'.red);
}
// Set debug mode for node-inspector
// Based on https://github.com/joyent/node/blob/master/src/node.cc#L2903
if (Number(process.version.match(/^v(\d+\.\d+)/)[1]) >= 8) {
if (options.debug === true) {
options.opts.unshift('--inspect');
} else if (!isNaN(parseInt(options.debug, 10))) {
options.opts.unshift('--inspect=' + options.debug);
} else if (options.breakOnFirstLine === true) {
options.opts.unshift('--inspect-brk');
} else if (!isNaN(parseInt(options.breakOnFirstLine, 10))) {
options.opts.unshift('--inspect-brk=' + options.breakOnFirstLine);
}
} else {
if (options.debug === true) {
options.opts.unshift('--debug');
} else if (!isNaN(parseInt(options.debug, 10))) {
options.opts.unshift('--debug=' + options.debug);
} else if (options.breakOnFirstLine === true) {
options.opts.unshift('--debug-brk');
} else if (!isNaN(parseInt(options.breakOnFirstLine, 10))) {
options.opts.unshift('--debug-brk=' + options.breakOnFirstLine);
}
}
if ((options.debug || options.breakOnFirstLine) && options.cmd === 'coffee') {
options.opts.unshift('--nodejs');
}
if (options.background) {
var errtype = process.stderr;
if(options.logs && options.logs.err) {
errtype = 'pipe';
}
server = process._servers[target] = spawn(
options.cmd,
options.opts.concat(options.args),
{
env: process.env,
stdio: ['ignore', 'pipe', errtype]
}
);
if (options.delay) {
setTimeout(finished, options.delay);
}
if (options.output) {
server.stdout.on('data', function(data) {
var message = "" + data;
var regex = new RegExp(options.output, "gi");
if (message.match(regex)) {
finished();
}
});
}
var out = process.stdout;
if(options.logs) {
var fs = require('fs'), path = require('path');
if(options.logs.out) {
out = fs.createWriteStream(path.resolve(options.logs.out), {flags: 'a'});
}
if(options.logs.err && errtype === 'pipe') {
server.stderr.pipe(fs.createWriteStream(path.resolve(options.logs.err), {flags: 'a'}));
}
}
server.stdout.pipe(out);
server.on('close',this.stop);
} else {
// Server is ran in current process
server = process._servers[target] = require(options.script);
}
process.on('exit', this.stop);
},
stop: function stop(options) {
if (server && server.kill) {
grunt.log.writeln('Stopping'.red + ' Express server');
server.removeAllListeners('close');
if (options.hardStop) {
grunt.log.writeln('Using ' + 'SIGKILL'.red);
server.kill('SIGKILL');
} else {
server.kill('SIGTERM');
}
process.removeListener('exit', finished);
process.removeListener('exit', stop);
server = process._servers[target] = null;
}
// Restore original process.env
if (backup) {
process.env = JSON.parse(JSON.stringify(backup));
}
finished();
}
};
};