Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Options:
--no-remote Do not allow remote calls
--foreground Bring Firefox to the foreground
-l, --listen <port> Start the debugger server on a specific port.
--detached Run as background process
```
11 changes: 9 additions & 2 deletions bin/fx-runner
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ program
.option("--no-remote", "Do not allow remote calls")
.option("--foreground", "Bring Firefox to the foreground")
.option("-l, --listen <port>", "Start the debugger server on a specific port.")
.option("--detached", "Run as background process.")

program
.command("start")
Expand All @@ -27,13 +28,19 @@ program
"foreground": !!program.foreground ? true : false,
"no-remote": !program.remote ? true : false,
"binary-args": program.binaryArgs || "",
"listen": program.listen || 6000
"listen": program.listen || 6000,
"detached": !!program.detached ? true : false
})
.then(function(results) {
var firefox = results.process;
if (program.verbose) {
if (!program.detached && program.verbose) {
firefox.stdout.pipe(process.stdout)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least with the current implementation, if the 'detached' option has been passed, the firefox.stdout stream has already been destroyed here and so we could probably also skip this firefox.stdout.pipe(process.stdout).

Otherwise, we could destroy the child_process stdio streams at line 41, right before the firefox.unref() call and, when node-fx-runner is used as a library (as in the web-ext CLI tool), the caller is going to decide on its own when it is ready to disconnect the streams and unref the child process.

}

if (program.detached) {
firefox.unref();
}

}, console.exception);
});

Expand Down
81 changes: 64 additions & 17 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var spawn = require("child_process").spawn;
var extend = require("lodash").extend;
var normalizeBinary = require("./utils").normalizeBinary;
var parse = require("shell-quote").parse;
var fs = require("fs");
var when = require("when");

/**
* Takes a manifest object (from package.json) and options,
Expand All @@ -15,6 +17,8 @@ var parse = require("shell-quote").parse;
* @param {Object} options
* - `binary` path to Firefox binary to use
* - `profile` path to profile or profile name to use
* - `stdout-file-path` path for redirecting stdout of Firefox process
* - `stderr-file-path` path for redirecting stderr of Firefox process
* @return {Object} results
*/
function runFirefox (options) {
Expand Down Expand Up @@ -47,33 +51,76 @@ function runFirefox (options) {
args = args.concat(parse(options["binary-args"]));
}
}
var detached = options["detached"] ? true : false;

var stdout;
var stdoutPromise = when.promise(function(resolve, reject) {
if (options["stdout-file-path"]) {
fs.open(options["stdout-file-path"], "a", function(err, fd) {
if (err) {
reject(err);
}
stdout = fd;
resolve();
});
} else {
stdout = detached ? "ignore" : "pipe"
resolve();
}
});

var stderr;
var stderrPromise = when.promise(function(resolve, reject) {
if (options["stderr-file-path"]) {
fs.open(options["stderr-file-path"], "a", function(err, fd) {
if (err) {
reject(err);
}
stderr = fd;
resolve();
});
} else {
stderr = detached ? "ignore" : "pipe";
resolve();
}
});

// support for starting the remote debugger server
if (options["listen"]) {
args.unshift(options.listen);
args.unshift("-start-debugger-server");
}

return normalizeBinary(options.binary).then(function(binary) {
// Using `spawn` so we can stream logging as they come in, rather than
// buffer them up until the end, which can easily hit the max buffer size.
var firefox = spawn(binary, args, { env: env });
return when.all([stdoutPromise, stderrPromise]).then(function() {
return normalizeBinary(options.binary).then(function(binary) {
// Using `spawn` so we can stream logging as they come in, rather than
// buffer them up until the end, which can easily hit the max buffer size.
var spawnOptions = {
env: env,
detached: detached,
stdio: [ "ignore", stdout, stderr ]
}
var firefox = spawn(binary, args, spawnOptions);

firefox.on("close", function () {
process.removeListener("exit", killFirefox);
});
function killFirefox () {
firefox.kill();
}

function killFirefox () {
firefox.kill();
}
if (!detached) {
firefox.on("close", function () {
process.removeListener("exit", killFirefox);
});

// Kill child process when main process is killed
process.once("exit", killFirefox);
// Kill child process when main process is killed
process.once("exit", killFirefox);
}

return {
process: firefox,
binary: binary,
args: args
};
return {
process: firefox,
binary: binary,
args: args
};
});
});
}
module.exports = runFirefox;
Expand Down
14 changes: 14 additions & 0 deletions test/run/test.run.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ describe("fx-runner start", function () {
done();
});
});

it("--detached", function (done) {
var proc = exec("start -v -b " + fakeBinary + " --detached", {}, function (err, stdout, stderr) {
expect(err).to.not.be.ok;
expect(stderr).to.not.be.ok;
expect(stdout).to.contain("detached");
expect(stdout).to.not.contain("--detached");
expect(stdout).to.not.contain("-P");
expect(stdout).to.not.contain("-foreground");
expect(stdout).to.not.contain("-no-remote");
done();
});
});

});
});

Expand Down