forked from 418sec/launchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.js
163 lines (139 loc) · 4.86 KB
/
instance.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
var path = require('path');
var spawn = require("child_process").spawn;
var exec = require("child_process").exec;
var execFile = require("child_process").execFile;
var EventEmitter = require('events').EventEmitter;
var debug = require('debug')('launchpad:local:instance');
var rimraf = require('rimraf');
var safe = function (str) {
// Avoid quotes makes impossible escape the `multi command` scenario
return str.replace(/['"]+/g, '');
}
var getProcessId = function (name, callback) {
name = safe(name);
var commands = {
darwin: "ps -clx | grep '" + name + "$' | awk '{print $2}' | head -1",
linux: "ps -ax | grep '" + name + "$' | awk '{print $2}' | head -1",
freebsd: "ps -clx | grep '" + name + "$' | awk '{print $2}' | head -1",
sunos: "ps -ax | grep '" + name + "$' | awk '{print $2}' | head -1" // Don't actually know about this
};
// Get the process with the given name if it is running
exec(commands[process.platform], function (err, stdout) {
var pid = stdout.trim();
debug('Got process ID', name, pid);
if (!pid) {
return callback(new Error('There does not seem to be a ' + name + ' process running'));
}
callback(null, pid);
});
};
var Instance = function (cmd, args, settings, options) {
this.options = options || {};
var self = this;
var childProcess = args === null ? exec(cmd, settings || {}) : spawn(cmd, args, settings || {});
debug( (args === null ? 'exec' : 'spawn') + ' child process with process id',
childProcess.pid, cmd, args);
childProcess.on('exit', function (code, signal) {
self.emit('stop', {
code: code,
signal: signal
});
});
if (settings && settings.timeout) {
var timeout = settings.timeout * 1000;
setTimeout(function () {
debug('Intance timed out', self.id, timeout);
self.stop();
}, timeout);
}
this.stdout = childProcess.stdout;
this.stderr = childProcess.stderr;
this.id = childProcess.pid;
this.process = childProcess;
this.cmd = cmd;
this.args = args;
};
Instance.prototype = new EventEmitter();
Instance.prototype.getPid = function (callback) {
if (this.options.process) {
getProcessId(this.options.process, callback);
} else {
callback(null, this.process.pid);
}
};
Instance.prototype.stop = function (callback) {
var self = this;
var command;
if(self.running) {
debug('Instance was open already, not stopping', this.id);
return callback(null, {});
}
if (callback) {
this.once('stop', function (data) {
debug('Instance stopped');
callback(null, data);
});
}
if (this.options.clean) {
try {
debug('Killing process', this.id);
process.kill(-this.id);
} catch (error) {}
} else {
if (this.options.command.indexOf('open') === 0) {
command = 'osascript -e \'tell application "' + safe(self.options.process) + '" to quit\'';
debug('Executing shutdown AppleScript', command);
command = command.split(' ');
execFile(command[0], command.slice(1));
} else if (process.platform === 'win32') {
//Adding `"` wasn't safe/functional on Win systems
command = 'taskkill /IM ' + (this.options.imageName || path.basename(this.cmd));
debug('Executing shutdown taskkil', command);
command = command.split(' ');
execFile(command[0], command.slice(1)).once('exit', function(data) {
self.emit('stop', data);
});
} else {
debug('Killing process', this.id);
this.process.kill();
}
}
if (this.options.tmpdir) {
debug('Removing tmpdir', this.options.tmpdir);
rimraf(this.options.tmpdir, function() {});
}
};
exports.Instance = Instance;
/**
* Starts a new process.
*
* @param cmd The process command line
* @param args The process arugments
* @param settings The process environment settings
* @param callback function(error, instance) Callback after the instance is started
* @see http://nodejs.org/api/child_process.html#child_process_child_process
*/
exports.start = function (cmd, args, settings, options, callback) {
var getInstance = function () {
debug('Starting instance', cmd, args);
return new Instance(cmd, args, settings, options);
};
// Check if the process is already running but only if it's a browser we
// can only launch once
if (options.process && !options.multi) {
getProcessId(options.process, function (err, pid) {
if (!err && !options.opensTab) {
return callback(new Error(options.process + ' seems already running with process id ' + pid));
}
var instance = getInstance();
// Add a `running` flag if the browser is already running but can open a new tab
if(!err && options.opensTab && !options.clean) {
debug('Marking instance as already running (but is able to open a tab)');
instance.running = true;
}
return callback(null, instance);
});
} else {
callback(null, getInstance());
}
};