-
Notifications
You must be signed in to change notification settings - Fork 71
/
index.js
218 lines (193 loc) · 6.17 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
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
'use strict';
/**
* Created by gimm on 3/13/2015.
*/
var util = require('util'),
path = require('path'),
assert = require('assert'),
spawn = require('child_process').spawn,
merge = require('deepmerge'),
tinylr = require('tiny-lr'),
es = require('event-stream'),
Q = require('q'),
chalk = require('chalk'),
debug = require('debug')('gulp-live-server');
var info = chalk.gray,
error = chalk.bold.red;
var glsInstanceCounter = 0;
var callback = {
processExit: function (code, sig, server) {
glsInstanceCounter--;
debug(info('Main process exited with [code => %s | sig => %s]'), code, sig);
server && server.kill();
},
serverExit: function (code, sig) {
debug(info('server process exited with [code => %s | sig => %s]'), code, sig);
if(sig !== 'SIGKILL'){
//server stopped unexpectedly
process.exit(0);
}
},
lrServerReady: function () {
console.log(info('livereload[tiny-lr] listening on %s ...'), this.config.livereload.port);
},
serverLog: function (data) {
console.log(data.replace(/\s+$/, ''));
},
serverError: function (data) {
console.log(error(data.replace(/\s+$/, '')));
}
};
/**
* set config data for the new server child process
* @type {Function}
*/
module.exports = exports = (function() {
var defaults = {
options: {
cwd: undefined
},
livereload: {
port: 35729
}
};
defaults.options.env = JSON.parse(JSON.stringify(process.env));
defaults.options.env.NODE_ENV = 'development';
return function(args, options, livereload){
var config = {}
config.args = util.isArray(args) ? args : [args];
//deal with options
config.options = merge(defaults.options, options || {});
//deal with livereload
if (livereload) {
config.livereload = (typeof livereload === 'object' ? livereload : {port: livereload});
}else{
config.livereload = (livereload === false ? false : defaults.livereload);
}
// return exports with its state, the server and livereload instance
// this allows multiple servers at once
return merge({
config: config,
server: undefined, // the server child process
lr: undefined // tiny-lr serverexports;
}, exports);
};
})();
/**
* default server script, the static server
*/
exports.script = path.join(__dirname, 'scripts/static.js');
/**
* create a server child process with the script file
*/
exports.new = function (script) {
if(!script){
return console.log(error('script file not specified.'));
}
var args = util.isArray(script) ? script : [script];
return this(args);
};
/**
* create a server child process with the static server script
*/
exports.static = function (folder, port) {
var script = this.script;
folder = folder || process.cwd();
util.isArray(folder) && (folder = folder.join(','));
port = port || 3000;
return this([script, folder, port]);
};
/**
* start/restart the server
*/
exports.start = function (execPath) {
if (this.server) { // server already running
debug(info('kill server'));
this.server.kill('SIGKILL');
//server.removeListener('exit', callback.serverExit);
this.server = undefined;
} else {
if(this.config.livereload){
this.lr = tinylr(this.config.livereload);
this.lr.listen(this.config.livereload.port, callback.lrServerReady.bind(this));
}
}
// if a executable is specified use that to start the server (e.g. coffeescript)
// otherwise use the currents process executable
this.config.execPath = execPath || this.config.execPath || process.execPath;
var deferred = Q.defer();
this.server = spawn(this.config.execPath, this.config.args, this.config.options);
//stdout and stderr will not be set if using the { stdio: 'inherit' } options for spawn
if (this.server.stdout) {
this.server.stdout.setEncoding('utf8');
this.server.stdout.on('data', function(data) {
deferred.notify(data);
callback.serverLog(data);
});
}
if (this.server.stderr) {
this.server.stderr.setEncoding('utf8');
this.server.stderr.on('data', function (data) {
deferred.notify(data);
callback.serverError(data);
});
}
this.server.once('exit', function (code, sig) {
setTimeout(function() { // yield event loop for stdout/stderr
deferred.resolve({
code: code,
signal: sig
});
if (glsInstanceCounter == 0)
callback.serverExit(code, sig);
}, 0)
});
var exit = function(code, sig) {
callback.processExit(code,sig,server);
}
process.listeners('exit') || process.once('exit', exit);
glsInstanceCounter++;
return deferred.promise;
};
/**
* stop the server
*/
exports.stop = function () {
var deferred = Q.defer();
if (this.server) {
this.server.once('exit', function (code) {
deferred.resolve(code);
});
debug(info('kill server'));
//use SIGHUP instead of SIGKILL, see issue #34
this.server.kill('SIGKILL');
//server.removeListener('exit', callback.serverExit);
this.server = undefined;
}else{
deferred.resolve(0);
}
if(this.lr){
debug(info('close livereload server'));
this.lr.close();
//TODO how to stop tiny-lr from hanging the terminal
this.lr = undefined;
}
return deferred.promise;
};
/**
* tell livereload.js to reload the changed resource(s)
*/
exports.notify = function (event) {
var lr = this.lr;
if(event && event.path){
var filepath = path.relative(__dirname, event.path);
debug(info('file(s) changed: %s'), event.path);
lr.changed({body: {files: [filepath]}});
}
return es.map(function(file, done) {
var filepath = path.relative(__dirname, file.path);
debug(info('file(s) changed: %s'), filepath);
lr.changed({body: {files: [filepath]}});
done(null, file);
}.bind(this));
};