Skip to content

Commit

Permalink
refactor: foregroundChild arguments normalization
Browse files Browse the repository at this point in the history
# Why

`foregroundChild` has a complex signature. Using a dedicated function for arguments normalization would improve clarity.

# What

Document the signature of `foregroundChild`, move arguments normalization to a separate function, small simplification to the normalization.
  • Loading branch information
demurgos committed Sep 20, 2018
1 parent 68ad56a commit afc1466
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,51 @@ if (process.platform === 'win32') {
spawn = require('cross-spawn')
}

module.exports = function (program, args, cb) {
var arrayIndex = arguments.length

if (typeof args === 'function') {
cb = args
args = undefined
/**
* Normalizes the arguments passed to `foregroundChild`.
*
* See the signature of `foregroundChild` for the supported arguments.
*
* @param fgArgs Array of arguments passed to `foregroundChild`.
* @return Normalized arguments
* @internal
*/
function normalizeFgArgs(fgArgs) {
var program, args, cb;
var processArgsEnd = fgArgs.length;
var lastFgArg = fgArgs[fgArgs.length - 1];
if (typeof lastFgArg === "function") {
cb = lastFgArg;
processArgsEnd -= 1;
} else {
cb = Array.prototype.slice.call(arguments).filter(function (arg, i) {
if (typeof arg === 'function') {
arrayIndex = i
return true
}
})[0]
cb = function(done) { done(); };
}

cb = cb || function (done) {
return done()
if (Array.isArray(fgArgs[0])) {
program = fgArgs[0][0];
args = fgArgs[0].slice(1);
} else {
program = fgArgs[0];
args = Array.isArray(fgArgs[1]) ? fgArgs[1] : fgArgs.slice(1, processArgsEnd);
}

if (Array.isArray(program)) {
args = program.slice(1)
program = program[0]
} else if (!Array.isArray(args)) {
args = [].slice.call(arguments, 1, arrayIndex)
}
return {program: program, args: args, cb: cb};
}

/**
*
* Signatures:
* ```
* (program: string | string[], cb?: CloseHandler);
* (program: string, args: string[], cb?: CloseHandler);
* (program: string, ...args: string[], cb?: CloseHandler);
* ```
*/
module.exports = function (/* program, args, cb */) {
var fgArgs = normalizeFgArgs([].slice.call(arguments, 0));
var program = fgArgs.program;
var args = fgArgs.args;
var cb = fgArgs.cb;

var spawnOpts = { stdio: [0, 1, 2] }

Expand Down

0 comments on commit afc1466

Please sign in to comment.