-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.js
152 lines (135 loc) · 4.99 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
/**
* Created by Gimm on 7/17/14.
*/
var util = require('util'),
path = require('path'),
spawn = require('child_process').spawn,
merge = require('deepmerge'),
tinylr = require('tiny-lr'),
es = require('event-stream'),
chalk = require('chalk'),
debug = require('debug')('gulp-express');
module.exports = (function () {
var server = undefined, // the server child process
lr = undefined, // tiny-lr server
config = {
args: ['app.js'], // args for child_process.spawn
options: { // options for child_process.options
cwd: undefined
},
livereload: { //option for tiny-lr server
port: 35729
}
},
info = chalk.gray,
error = chalk.bold.red;
config.options.env = process.env;
config.options.env.NODE_ENV = 'development';
var callback = {
processExit: function (code, sig) {
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
if (lr) {
lr.close();
}
}
},
lrServerReady: function () {
console.log(info('livereload[tiny-lr] listening on %s ...'), config.livereload.port);
},
serverLog: function (data) {
console.log(info(data.trim()));
},
serverError: function (data) {
console.log(error(data.trim()));
}
};
return {
/**
* Start/restart a child process by running the script file,
* this process work as the http(s) server;
* And start a livereload(tiny-lr) server if it's not started yet.
*
* @param {array} [args]
* @param {object} [options]
* @param {boolean|number|object} [livereload]
*/
run: function (args, options, livereload) {
//deal with args
if(util.isArray(args) && args.length){
config.args = args;
}
//deal with options
config.options = merge(config.options, options || {});
//deal with livereload
if(livereload === false){ //livereload disabled
config.livereload = false;
}else if(livereload){
if(typeof livereload === 'object'){
config.livereload = livereload;
}else{
config.livereload.port = livereload;
}
}
if (server) { // server already running
debug(info('kill server'));
server.kill('SIGKILL');
//server.removeListener('exit', callback.serverExit);
server = undefined;
} else {
if(config.livereload){
lr = tinylr(config.livereload);
lr.listen(config.livereload.port, callback.lrServerReady);
}
}
server = spawn('node', config.args, config.options);
server.stdout.setEncoding('utf8');
server.stderr.setEncoding('utf8');
server.stdout.on('data', callback.serverLog);
server.stderr.on('data', callback.serverError);
server.once('exit', callback.serverExit);
process.listeners('exit') || process.once('exit', callback.processExit);
},
/**
* Stop the server child process and the livereload server
*/
stop: function () {
if (server) {
debug(info('kill server'));
//use SIGHUP instead of SIGKILL, see issue #34
server.kill('SIGKILL');
//server.removeListener('exit', callback.serverExit);
server = undefined;
}
if(lr){
debug(info('close livereload server'));
lr.close();
//TODO how to stop tiny-lr from hanging the terminal
lr = undefined;
}
},
/**
* Tell the livereload.js to reload the resources that been changed
* @param event
* @returns {*}
*/
notify: function (event) {
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);
});
}
};
})();