Skip to content

Commit 079137c

Browse files
mmaleckiindexzero
authored andcommitted
[refactor] Refactor Forever service CLI
1 parent c01abef commit 079137c

File tree

2 files changed

+63
-186
lines changed

2 files changed

+63
-186
lines changed

bin/foreverd

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#!/usr/bin/env node
22

3-
var Clip = require('clip');
3+
require('../lib/forever/service/cli').startCLI();
44

5-
var app = new Clip();
6-
7-
require('../lib/forever/service/cli')(app);
8-
9-
app.run();

lib/forever/service/cli.js

+62-180
Original file line numberDiff line numberDiff line change
@@ -7,205 +7,87 @@
77
*/
88

99
var utile = require('utile'),
10+
flatiron = require('flatiron'),
1011
optimist = require('optimist'),
1112
forever = require('../../forever'),
1213
Service = require('./service'),
1314
argv;
1415

15-
var mappings = {
16-
'c': 'command',
17-
'e': 'errFile',
18-
'l': 'logFile',
19-
'a': 'appendLog',
20-
'append': 'appendLog',
21-
'm': 'max',
22-
'o': 'outFile',
23-
'p': 'path',
24-
'pidfile': 'pidFile',
25-
's': 'silent',
26-
'silent': 'silent',
27-
'sourceDir': 'sourceDir',
28-
'minUptime': 'minUptime',
29-
'spinSleepTime': 'spinSleepTime',
30-
'v': 'verbose',
31-
'verbose': 'verbose',
32-
'd': 'debug',
33-
'debug': 'debug'
34-
};
35-
36-
function processArgs(cmd) {
37-
38-
}
39-
40-
var router = module.exports = function router(app) {
41-
app.use(function (cmd, tty, next) {
42-
cmd.flags._.shift();
43-
cmd.service = new Service({
44-
adapter: cmd.flags.adapter
45-
});
46-
47-
cmd.argv = cmd.flags._;
48-
var file = cmd.argv[0],
49-
options = {},
50-
config,
51-
uid;
52-
53-
cmd.file = file;
54-
cmd.options = options;
55-
56-
if (file) {
57-
//
58-
// Setup pass-thru options for child-process
59-
//
60-
options.options = cmd.argv.splice(cmd.argv.indexOf(file)).splice(1);
61-
}
62-
else if (cmd.flags.c || cmd.flags.command) {
63-
options.options = cmd.argv.splice(cmd.argv.indexOf(cmd.flags.c || cmd.flags.command) + 1);
64-
}
16+
var cli = exports;
6517

66-
//
67-
// Now that we've removed the target script options
68-
// reparse the options and configure the forever settings
69-
//
70-
argv = optimist(cmd.argv).boolean(['v', 'verbose', 'a', 'append', 's', 'silent']).argv;
71-
Object.keys(argv).forEach(function (key) {
72-
if (mappings[key] && argv[key]) {
73-
options[mappings[key]] = argv[key];
74-
}
75-
});
18+
var app = flatiron.app;
7619

77-
if (typeof options['max'] === 'undefined') {
78-
//
79-
// If max isn't specified set it to run forever
80-
//
81-
options.forever = true;
82-
}
20+
app.use(flatiron.plugins.cli, {
21+
usage: forever.cli.usage
22+
});
8323

84-
if (typeof options['minUptime'] !== 'undefined') {
85-
options['minUptime'] = parseFloat(options['minUptime']);
86-
}
87-
if (typeof options['spinSleepTime'] !== 'undefined') {
88-
options['spinSleepTime'] = parseFloat(options['spinSleepTime']);
89-
}
24+
app.config.argv().env();
9025

91-
if (!options.sourceDir) {
92-
//
93-
// Set the sourceDir of the options for graceful
94-
// restarting outside of the main directory
95-
//
96-
options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/';
97-
}
98-
99-
uid = options.uid || utile.randomString(4);
100-
options.uid = uid;
101-
options.pidFile = options.pidFile || uid + '.pid';
102-
options.logFile = argv.l || uid + '.log';
103-
104-
//
105-
// Check for existing global config and set each
106-
// key appropriately if it exists.
107-
//
108-
['append', 'silent', 'verbose'].forEach(function (key) {
109-
var target = mappings[key],
110-
value = forever.config.get(key);
111-
112-
if (value) {
113-
options[target] = options[target] || value === 'true';
114-
}
115-
});
26+
var service = new Service({
27+
adapter: optimist.argv.adapter
28+
});
11629

117-
//
118-
// Pass the source dir to spawn
119-
//
120-
options.spawnWith = {
121-
cwd: options.sourceDir
122-
};
123-
124-
//
125-
// Configure winston for forever based on the CLI options
126-
//
127-
if (options.verbose) {
128-
forever.log.transports.console.level = 'silly';
30+
app.cmd('install', cli.install = function () {
31+
app.service.install(function onInstall(err) {
32+
if (err) {
33+
forevee.log.error(err);
12934
}
130-
131-
//
132-
// Setup configurations for forever
133-
//
134-
config = {
135-
root: cmd.flags.p
136-
};
137-
138-
//
139-
// Only call `forever.load()` if the root path is different than
140-
// the default root exposed by forever.
141-
//
142-
if ((config.root && config.root !== forever.root)) {
143-
forever.log.silly('Loading forever with config: ', config);
144-
forever.load(config);
145-
forever.log.silly('Loaded forever successfully.');
35+
else {
36+
forever.log.info('foreverd installed');
14637
}
147-
148-
next();
14938
});
39+
});
15040

151-
app.cli('/install', function (cmd, tty) {
152-
cmd.service.install(function onInstall(err) {
153-
if (err) {
154-
tty.error(err);
155-
}
156-
else {
157-
tty.info('foreverd installed');
158-
}
159-
});
41+
//TODO
42+
app.cmd('run', cli.run = function () {
43+
service.load(function () {
44+
service.run();
16045
});
161-
162-
//TODO
163-
app.cli('/run', function (cmd, tty) {
164-
cmd.service.load(function () {
165-
cmd.service.run();
46+
});
47+
48+
app.cmd('uninstall', cli.uninstall = function () {
49+
service.uninstall();
50+
});
51+
52+
app.cmd(/add (.*)/, cli.add = function (file) {
53+
service.add(file, forever.cli.getOptions(file));
54+
});
55+
56+
//TODO
57+
app.cmd(/remove (.*)/, cli.remove = function (file) {
58+
service.remove(file, forever.cli.getOptions(file));
59+
});
60+
61+
app.cmd('start', cli.start = function () {
62+
service.start();
63+
});
64+
65+
//TODO
66+
app.cmd('stop', cli.stop = function () {
67+
service.stop();
68+
});
69+
70+
app.cmd('restart', cli.restart = function () {
71+
service.restart();
72+
});
73+
74+
app.cmd('list', cli.list = function () {
75+
service.list(function (err, applications) {
76+
applications.forEach(function printApplication(application) {
77+
console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile);
16678
});
16779
});
80+
});
16881

169-
app.cli('/uninstall', function (cmd, tty) {
170-
cmd.service.uninstall();
171-
});
172-
173-
app.cli('/add/*', function (cmd, tty) {
174-
cmd.service.add(cmd.file, cmd.options);
175-
});
176-
177-
//TODO
178-
app.cli('/remove', function (cmd, tty) {
179-
cmd.service.remove(cmd.file, cmd.options);
180-
});
181-
182-
app.cli('/start', function (cmd, tty) {
183-
cmd.service.start();
184-
});
82+
app.cmd('pause', cli.pause = function () {
83+
service.pause();
84+
});
18585

186-
//TODO
187-
app.cli('/stop', function (cmd, tty) {
188-
cmd.service.stop();
189-
});
86+
app.cmd('resume', cli.resume = function () {
87+
service.resume();
88+
});
19089

191-
app.cli('/restart', function (cmd, tty) {
192-
cmd.service.restart();
193-
});
194-
195-
app.cli('/list', function (cmd, tty) {
196-
cmd.service.list(function (err, applications) {
197-
applications.forEach(function printApplication(application) {
198-
console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile);
199-
});
200-
});
201-
});
202-
203-
app.cli('/pause', function (cmd, tty) {
204-
cmd.service.pause();
205-
});
206-
207-
app.cli('/resume', function (cmd, tty) {
208-
cmd.service.resume();
209-
});
90+
cli.startCLI = function () {
91+
app.init(app.start);
21092
};
21193

0 commit comments

Comments
 (0)