From 741223235449628484982cc8506c5ced4b74bbf8 Mon Sep 17 00:00:00 2001 From: Hiroyuki Ikezoe Date: Fri, 16 Mar 2018 07:12:16 +0900 Subject: [PATCH 1/2] feat: Added --detached option --- README.md | 1 + bin/fx-runner | 12 +++++++++++- lib/run.js | 18 +++++++++++------- test/run/test.run.js | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f3afe8f..b948a31 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,5 @@ Options: --no-remote Do not allow remote calls --foreground Bring Firefox to the foreground -l, --listen Start the debugger server on a specific port. +--detached Run as background process ``` diff --git a/bin/fx-runner b/bin/fx-runner index a2340dc..d77ebc1 100755 --- a/bin/fx-runner +++ b/bin/fx-runner @@ -14,6 +14,7 @@ program .option("--no-remote", "Do not allow remote calls") .option("--foreground", "Bring Firefox to the foreground") .option("-l, --listen ", "Start the debugger server on a specific port.") + .option("--detached", "Run as background process.") program .command("start") @@ -27,13 +28,22 @@ 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) { firefox.stdout.pipe(process.stdout) } + + if (program.detached) { + firefox.stdout.destroy(); + firefox.stdin.destroy(); + firefox.stderr.destroy(); + firefox.unref(); + } + }, console.exception); }); diff --git a/lib/run.js b/lib/run.js index cfaab75..543ccf5 100644 --- a/lib/run.js +++ b/lib/run.js @@ -47,6 +47,8 @@ function runFirefox (options) { args = args.concat(parse(options["binary-args"])); } } + var detached = options["detached"] ? true : false; + // support for starting the remote debugger server if (options["listen"]) { args.unshift(options.listen); @@ -56,18 +58,20 @@ function runFirefox (options) { 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 }); - - firefox.on("close", function () { - process.removeListener("exit", killFirefox); - }); + var firefox = spawn(binary, args, { env: env, detached: detached }); function killFirefox () { firefox.kill(); } - // Kill child process when main process is killed - process.once("exit", killFirefox); + if (!detached) { + firefox.on("close", function () { + process.removeListener("exit", killFirefox); + }); + + // Kill child process when main process is killed + process.once("exit", killFirefox); + } return { process: firefox, diff --git a/test/run/test.run.js b/test/run/test.run.js index 9b07e81..08d9d75 100644 --- a/test/run/test.run.js +++ b/test/run/test.run.js @@ -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(); + }); + }); + }); }); From 1c884e963af38c26c04b01574bfd6282f947f850 Mon Sep 17 00:00:00 2001 From: Hiroyuki Ikezoe Date: Wed, 4 Apr 2018 19:35:30 +0900 Subject: [PATCH 2/2] Add two new options to specify stdout/stderr files --- bin/fx-runner | 5 +--- lib/run.js | 81 +++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 23 deletions(-) diff --git a/bin/fx-runner b/bin/fx-runner index d77ebc1..e62685e 100755 --- a/bin/fx-runner +++ b/bin/fx-runner @@ -33,14 +33,11 @@ program }) .then(function(results) { var firefox = results.process; - if (program.verbose) { + if (!program.detached && program.verbose) { firefox.stdout.pipe(process.stdout) } if (program.detached) { - firefox.stdout.destroy(); - firefox.stdin.destroy(); - firefox.stderr.destroy(); firefox.unref(); } diff --git a/lib/run.js b/lib/run.js index 543ccf5..b520b71 100644 --- a/lib/run.js +++ b/lib/run.js @@ -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, @@ -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) { @@ -49,35 +53,74 @@ function runFirefox (options) { } 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, detached: detached }); + 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); - function killFirefox () { - firefox.kill(); - } + function killFirefox () { + firefox.kill(); + } - if (!detached) { - firefox.on("close", function () { - process.removeListener("exit", killFirefox); - }); + 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;