Permalink
Browse files

Fix bogus arg-passing to spawned node instances (fixes #12)

  • Loading branch information...
1 parent 8a197b7 commit 59a4c7d8781ede98236895a77a0a3866b317fc11 @mamacdon committed Dec 11, 2012
Showing with 27 additions and 18 deletions.
  1. +27 −18 lib/node_apps.js
View
@@ -99,11 +99,10 @@ AppTable.prototype = /** @lends orionode.AppTable.prototype */ {
}
};
-function spawnNode(modulePath, args) {
- var child = child_process.spawn(PATH_TO_NODE, [modulePath].concat(Array.isArray(args) ? args : []), {
- // TODO: cwd should be a parameter, not assumed to be the module's parent directory.
- cwd: path.dirname(modulePath)
- });
+function spawnNode(args, options) {
+ args = Array.isArray(args) ? args : null;
+ options = options || {};
+ var child = child_process.spawn(PATH_TO_NODE, args, options);
child.on('error', function(e) {
console.log('PID ' + child.pid + ' error: ' + e);
});
@@ -123,7 +122,7 @@ var AppContext = function(options) {
// }
/** @throws {Error} If modulePath is unsafe */
- function safeModulePath(fileRoot, workspaceDir, modulePath) {
+ function resolveModulePath(fileRoot, workspaceDir, modulePath) {
var filePath = api.rest(fileRoot, modulePath);
return fileUtil.safeFilePath(workspaceDir, filePath);
}
@@ -143,15 +142,19 @@ var AppContext = function(options) {
get: function() { return appTable; }
});
- var _startApp = function(modulePath, args, hidden) {
+ /**
+ * @param {String[]} [args]
+ * @param {String} cwd
+ * @param {Boolean} [hidden=false]
+ */
+ var _startApp = function(args, cwd, hidden) {
hidden = typeof hidden === 'boolean' ? hidden : false;
var app;
try {
- var proc = spawnNode(modulePath, args);
+ var proc = spawnNode(args, {cwd: cwd});
app = new App(proc.pid, proc, hidden);
appTable.put(proc.pid, app);
app.on('exit', function() {
- console.log('Exit app # ' + proc.pid);
appTable.remove(proc.pid);
});
return app;
@@ -165,10 +168,10 @@ var AppContext = function(options) {
* @function
* @private
*/
- var _startInspectorApp = function(inspectorPath, args, headers, requestUrl, restartOnExit){
+ var _startInspectorApp = function(args, cwd, headers, requestUrl, restartOnExit){
var app;
try {
- app = _startApp(inspectorPath, args, true /*hidden*/);
+ app = _startApp(args, cwd, true /*hidden*/);
var parsedRequestUrl = url.parse(requestUrl);
app.inspect = url.format({
protocol: parsedRequestUrl.protocol,
@@ -178,8 +181,7 @@ var AppContext = function(options) {
app.on('exit', function() {
console.log('Exit inspector, PID: ' + app.pid);
if(restartOnExit){
- // TODO: this is too eager-- causes spam when there's only 1 debuggee and it exits immediately
- _startInspectorApp(inspectorPath, args, headers, requestUrl, restartOnExit);
+ _startInspectorApp(args, cwd, headers, requestUrl, restartOnExit);
}
});
console.log('Started inspector, PID: ' + app.pid);
@@ -193,21 +195,29 @@ var AppContext = function(options) {
/**
* @name orionode.AppContext#startApp
* @function
- * @param {String} modulePath
+ * @param {String} modulePath The Orionode-filesystem path (ie. starting with '/file') of the module to execute
* @param {Array} [args]
* @param {Boolean} [hidden]
*/
this.startApp = function(modulePath, args, hidden) {
- return _startApp(safeModulePath(fileRoot, workspaceDir, modulePath), args, hidden);
+ modulePath = resolveModulePath(fileRoot, workspaceDir, modulePath);
+ // TODO cwd should be passed in, not assumed to be the module's parent folder.
+ var app = _startApp([modulePath].concat(args || []), path.dirname(modulePath), hidden);
+ app.on('exit', function(code) {
+ console.log('App # ' + app.pid + ' exited, code=' + code);
+ });
+ return app;
};
/**
* @name orion.AppContext#debugApp
* @param {String} modulePath
* @param {Number} port
*/
this.debugApp = function(modulePath, port, headers, requestUrl) {
+ var resolvedPath = resolveModulePath(fileRoot, workspaceDir, modulePath);
+ // TODO cwd should be passed in, not assumed to be the module's parent folder.
+ var app = _startApp(["--debug-brk=" + port, resolvedPath], path.dirname(resolvedPath));
var parsedRequestUrl = url.parse(requestUrl);
- var app = this.startApp(modulePath, ["--debug-brk=" + port].concat(modulePath));
app.debug = url.format({
protocol: parsedRequestUrl.protocol,
// TODO this is bizarre. Can we host node-inspector on the same port as Orionode?
@@ -218,8 +228,7 @@ var AppContext = function(options) {
//Lazy spawn the node inspector procees for the first time when user wants to debug an app.
if(app && !inspector) {
var inspectorPath = require.resolve(PATH_TO_NODE_INSPECTOR);
- var inspectorArg = [inspectorPath].concat("--web-port=" + INSPECT_PORT);
- inspector = _startInspectorApp(inspectorPath, inspectorArg, headers, requestUrl, true);
+ inspector = _startInspectorApp([inspectorPath, "--web-port=" + INSPECT_PORT], path.dirname(inspectorPath), headers, requestUrl, true);
}
return app;
}.bind(this);

0 comments on commit 59a4c7d

Please sign in to comment.