/
cli.js
256 lines (226 loc) · 6.96 KB
/
cli.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* cli.js: Handlers for the forever CLI commands.
*
* (C) 2010 Charlie Robbins
* MIT LICENCE
*
*/
var sys = require('sys'),
path = require('path'),
eyes = require('eyes'),
winston = require('winston'),
forever = require('forever');
var cli = exports;
var reserved = ['root', 'pidPath'];
var inspect = eyes.inspector({ stream: null,
styles: { // Styles applied to stdout
all: null, // Overall style applied to everything
label: 'underline', // Inspection labels, like 'array' in `array: [1, 2, 3]`
other: 'inverted', // Objects which don't have a literal representation, such as functions
key: 'grey', // The keys in object literals, like 'a' in `{a: 1}`
special: 'grey', // null, undefined...
number: 'blue', // 1, 2, 3, etc
bool: 'magenta', // true false
regexp: 'green', // /\d+/
string: 'yellow'
}
});
//
// ### function exec (action, file, options)
// #### @action {string} CLI action to execute
// #### @file {string} Location of the target forever script or process.
// #### @options {Object} Options to pass to forever for the `action`.
// Executes the `action` in forever with the specified `file` and `options`.
//
cli.exec = function (action, file, options) {
winston.info('Running action: ' + action.yellow);
winston.silly('Tidying ' + forever.config.get('root'));
var tidy = forever.cleanUp(action === 'cleanlogs');
tidy.on('cleanUp', function () {
winston.silly(forever.config.get('root') + ' tidied.');
if (file && action !== 'set' && action !== 'clear') {
winston.info('Forever processing file: ' + file.grey);
}
if (options && action !== 'set') {
winston.silly('Forever using options', options);
}
//
// If there is no action then start in the current
// process with the specified `file` and `options`.
//
if (!action) {
return cli.start(file, options);
}
else if (action === 'cleanlogs') {
return;
}
var daemon = true;
cli[action](file, options, daemon);
});
};
//
// ### function start (file, options, daemon)
// #### @file {string} Location of the script to spawn with forever
// #### @options {Object} Options to spawn the script `file` with.
// #### @daemon {boolean} Value indicating if we should spawn as a daemon
// Starts a forever process for the script located at `file` with the
// specified `options`. If `daemon` is true, then the script will be
// started as a daemon process.
//
cli.start = function (file, options, daemon) {
tryStart(file, options, function () {
return daemon
? forever.startDaemon(file, options)
: forever.start(file, options);
});
};
//
// ### function stop (file)
// #### @file {string} Target forever process to stop
// Stops the forever process specified by `file`.
//
cli.stop = function (file) {
var runner = forever.stop(file, true);
runner.on('stop', function (process) {
winston.info('Forever stopped process:');
sys.puts(process);
});
runner.on('error', function (err) {
winston.error('Forever cannot find process with index: ' + file)
});
};
//
// ### function stopall ()
// Stops all currently running forever processes.
//
cli.stopall = function () {
var runner = forever.stopAll(true);
runner.on('stopAll', function (processes) {
if (processes) {
winston.info('Forever stopped processes:');
sys.puts(processes);
}
else {
winston.info('No forever processes running');
}
});
};
//
// ### function restart (file)
// #### @file {string} Target process to restart
// Restarts the forever process specified by `file`.
//
cli.restart = function (file) {
var runner = forever.restart(file, true);
runner.on('restart', function (processes) {
if (processes) {
winston.info('Forever restarted processes:');
sys.puts(processes);
}
else {
winston.info('No forever processes running');
}
});
};
//
// ### function list ()
// Lists all currently running forever processes.
//
cli.list = function () {
var processes = forever.list(true);
if (processes) {
winston.info('Forever processes running');
sys.puts(processes);
}
else {
winston.info('No forever processes running');
}
};
//
// ### function config ()
// Lists all of the configuration in `~/.forever/config.json`.
//
cli.config = function () {
var keys = Object.keys(forever.config.store),
conf = inspect(forever.config.store);
if (keys.length <= 2) {
conf = conf.replace(/\{\s/, '{ \n')
.replace(/\}/, '\n}')
.replace('\033[90m', ' \033[90m')
.replace(/, /ig, ',\n ')
}
else {
conf = conf.replace(/\n\s{4}/ig, '\n ');
}
conf.split('\n').forEach(function (line) {
winston.info(line);
});
};
//
// ### function set (key, value)
// #### @key {string} Key to set in forever config
// #### @value {string} Value to set for `key`
// Sets the specified `key` / `value` pair in the
// forever user config.
//
cli.set = function (key, value) {
if (!key || !value) {
return winston.error('Both <key> and <value> are required.');
}
updateConfig(function () {
winston.info('Setting forever config: ' + key.grey);
forever.config.set(key, value);
});
};
//
// ### function clear (key)
// #### @key {string} Key to remove from `~/.forever/config.json`
// Removes the specified `key` from the forever user config.
//
cli.clear = function (key) {
if (reserved.indexOf(key) !== -1) {
winston.warn('Cannot clear reserved config: ' + key.grey);
winston.warn('Use `forever set ' + key + '` instead');
return;
}
updateConfig(function () {
winston.info('Clearing forever config: ' + key.grey);
forever.config.clear(key);
});
};
//
// ### function (file, options, callback)
// #### @file {string} Target script to start
// #### @options {Object} Options to start the script with
// #### @callback {function} Continuation to respond to when complete.
// Helper function that sets up the pathing for the specified `file`
// then stats the appropriate files and responds.
//
function tryStart (file, options, callback) {
var fullLog, fullScript
fullLog = forever.logFilePath(options.logFile, options.uid);
fullScript = path.join(options.sourceDir, file);
forever.stat(fullLog, fullScript, options.appendLog, function (err) {
if (err) {
winston.error('Cannot start forever: ' + err.message);
process.exit(-1);
}
callback();
});
}
//
// ### function updateConfig (updater)
// #### @updater {function} Function which updates the forever config
// Helper which runs the specified `updater` and then saves the forever
// config to `forever.config.get('root')`.
//
function updateConfig (updater) {
updater();
forever.config.save(function (err) {
if (err) {
return winston.error('Error saving config: ' + err.message);
}
cli.config();
winston.info('Forever config saved: ' + path.join(forever.config.get('root'), 'config.json').yellow);
});
}