Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isForced argument, which forces respawn even without node flags #11

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ flaggedRespawn(v8flags, process.argv, function (ready, child) {

## API

### <u>flaggedRespawn(flags, argv, [ forcedFlags, ] callback) : Void</u>
### <u>flaggedRespawn(flags, argv, [ forcedFlags, ] [ isForced , ] callback) : Void</u>

Respawns the script itself when *argv* has special flag contained in *flags* and/or *forcedFlags* is not empty. Because members of *flags* and *forcedFlags* are passed to `node` command, each of them needs to be a node flag or a V8 flag.

In addition, this function respawns also when *isForced* is specified and its value is true.

#### Forbid respawning

If `--no-respawning` flag is given in *argv*, this function does not respawned even if *argv* contains members of flags or *forcedFlags* is not empty. (This flag is also used internally to prevent from respawning more than once).
Expand All @@ -66,7 +68,8 @@ If `--no-respawning` flag is given in *argv*, this function does not respawned e
|:--------------|:------:|:----------------------------------------------------|
| *flags* | Array | An array of node flags and V8 flags which are available when present in *argv*. |
| *argv* | Array | Command line arguments to respawn. |
| *forcedFlags* | Array or String | An array of node flags or a string of a single flag and V8 flags for respawning forcely. |
| *forcedFlags* | Array or String | An array of node flags or a string of a single flag and V8 flags for respawning forcely. (Optional) |
| *isForced* | boolean | A flag to force respawn even if *forcedFlags* is empty or not specified. (Optional) |
| *callback* | function | A called function when not respawning or after respawned. |

* **<u><i>callback</i>(ready, proc, argv) : Void</u>**
Expand Down
17 changes: 16 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const remover = require('./lib/remover');

const FORBID_RESPAWNING_FLAG = '--no-respawning';

module.exports = function (flags, argv, forcedFlags, execute) {
module.exports = function (flags, argv, forcedFlags, isForced, execute) {
if (!flags) {
throw new Error('You must specify flags to respawn with.');
}
Expand All @@ -15,6 +15,17 @@ module.exports = function (flags, argv, forcedFlags, execute) {
if (typeof forcedFlags === 'function') {
execute = forcedFlags;
forcedFlags = [];
isForced = false;

} else if (typeof isForced === 'function') {
execute = isForced;

if (typeof forcedFlags === 'boolean') {
isForced = forcedFlags;
forcedFlags = [];
} else {
isForced = false;
}
}

if (typeof forcedFlags === 'string') {
Expand Down Expand Up @@ -44,6 +55,10 @@ module.exports = function (flags, argv, forcedFlags, execute) {
ready = false;
}

if (isForced) {
ready = false;
}

if (!ready) {
reordered.push(FORBID_RESPAWNING_FLAG);
proc = respawn(reordered);
Expand Down
23 changes: 23 additions & 0 deletions test/bin/force-and-forbid-respawning-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

var argv = process.argv.concat('--no-respawning');

flaggedRespawn(flags, argv, ['--trace-deprecation'], true, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

23 changes: 23 additions & 0 deletions test/bin/force-and-forbid-respawning-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

var argv = process.argv.concat('--no-respawning');

flaggedRespawn(flags, argv, true, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

21 changes: 21 additions & 0 deletions test/bin/force-respawning-isforced-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

flaggedRespawn(flags, process.argv, [], true, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

21 changes: 21 additions & 0 deletions test/bin/force-respawning-isforced-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

flaggedRespawn(flags, process.argv, [], false, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

21 changes: 21 additions & 0 deletions test/bin/force-respawning-isforced-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

flaggedRespawn(flags, process.argv, true, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

21 changes: 21 additions & 0 deletions test/bin/force-respawning-isforced-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

const flaggedRespawn = require('../../');
const v8flags = require('v8flags');

// get a list of all possible v8 flags for the running version of node
v8flags(function(err, flags) {
if (err) {
console.error(err);
return;
}

flaggedRespawn(flags, process.argv, false, function (ready, child) {
if (ready) {
console.log('Running!');
} else {
console.log('Respawning!');
}
});
});

79 changes: 79 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,85 @@ describe('flaggedRespawn', function () {
done();
}
});

it('should force respawning when isForced is true and forcedFlags is empty', function(done) {
var cmd = [
'node',
path.resolve(__dirname, 'bin/force-respawning-isforced-0.js'),
].join(' ');;

exec(cmd, function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Respawning!\nRunning!\n');
done();
});
});

it('should not force respawning when isForced is false and forcedFlags is empty', function(done) {
var cmd = [
'node',
path.resolve(__dirname, 'bin/force-respawning-isforced-1.js'),
].join(' ');;

exec(cmd, function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Running!\n');
done();
});
});

it('should force respawning when isForced is true and no forcedFlags', function(done) {
var cmd = [
'node',
path.resolve(__dirname, 'bin/force-respawning-isforced-2.js'),
].join(' ');;

exec(cmd, function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Respawning!\nRunning!\n');
done();
});
});

it('should not force respawning when isForced is false and no forcedFlags', function(done) {
var cmd = [
'node',
path.resolve(__dirname, 'bin/force-respawning-isforced-3.js'),
].join(' ');;

exec(cmd, function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Running!\n');
done();
});
});

it('should take priority to forbidding than forcing with isForced and forcedFlags', function(done) {
exec('node ./test/bin/force-and-forbid-respawning-1.js', cb);

function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Running!\n');
done();
}
});

it('should take priority to forbidding than forcing with isForced and no forcedFlags', function(done) {
exec('node ./test/bin/force-and-forbid-respawning-2.js', cb);

function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal('');
expect(stdout).to.equal('Running!\n');
done();
}
});

});

describe('cli args which are passed to app', function() {
Expand Down