Skip to content

Commit

Permalink
feat!: Rework nodeFlags, renaming to v8flags, which might be a callback
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Kellen authored and phated committed Nov 22, 2021
1 parent 7e4bbc7 commit 10a3940
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
50 changes: 36 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,55 @@ Liftoff.prototype.buildEnvironment = function (opts) {
};
};

Liftoff.prototype.handleFlags = function (cb) {
if (typeof this.v8flags === 'function') {
this.v8flags(function (err, flags) {
if (err) {
cb(err);
} else {
cb(null, flags);
}
});
} else {
process.nextTick(function () {
cb(null, this.v8flags);
}.bind(this));
}
};

Liftoff.prototype.launch = function (opts, fn) {
if (typeof fn !== 'function') {
throw new Error('You must provide a callback function.');
}

process.title = this.processTitle;

var completion = opts.completion;
if (completion && this.completions) {
return this.completions(completion);
}

if (this.nodeFlags) {
flaggedRespawn(this.nodeFlags, process.argv, function (ready, child) {
if (child !== process) {
this.emit('respawn', process.argv.filter(function (flag) {
return this.nodeFlags.indexOf(flag) !== -1;
}.bind(this)), child);
}
if (ready) {
this.handleFlags(function (err, flags) {
if (err) {
throw new Error(err);
} else {
if (flags) {
flaggedRespawn(flags, process.argv, function (ready, child) {
if (child !== process) {
this.emit('respawn', process.argv.filter(function (flag) {
return flags.indexOf(flag) !== -1;
}.bind(this)), child);
}
if (ready) {
fn.call(this, this.buildEnvironment(opts));
}
}.bind(this));
} else {
fn.call(this, this.buildEnvironment(opts));
}
}.bind(this));
} else {
fn.call(this, this.buildEnvironment(opts));
}

}
}.bind(this));
};



module.exports = Liftoff;
2 changes: 1 addition & 1 deletion test/fixtures/node_flags.js → test/fixtures/v8flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Liftoff = require('../..');

const Test = new Liftoff({
name: 'test',
nodeFlags: ['--lazy']
v8flags: ['--lazy']
});
Test.on('respawn', function (proc) {
console.log('saw respawn');
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/v8flags_function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Liftoff = require('../..');

const Test = new Liftoff({
name: 'test',
v8flags: function (cb) {
process.nextTick(function () {
cb(null, ['--lazy']);
})
}
});
Test.on('respawn', function (proc) {
console.log('saw respawn');
});

Test.launch({}, function (env) {
console.error(process.execArgv.join(''));
});
23 changes: 16 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ describe('Liftoff', function () {
expect(app.buildEnvironment().cwd).to.equal(path.resolve('test/fixtures/search_path'));
});


it('should resolve symlinks if config is one', function () {
var env = app.buildEnvironment({
cwd: 'test/fixtures/symlink'
Expand Down Expand Up @@ -138,22 +137,32 @@ describe('Liftoff', function () {
});
});

it('should use nodeFlags to re-spawn node with defined flags applied', function (done) {
exec('node test/fixtures/node_flags.js', function (err, stdout, stderr) {
it('should skip respawning if process.argv has no values from v8flags in it', function (done) {
exec('node test/fixtures/v8flags.js', function (err, stdout, stderr) {
expect(stderr).to.equal('\n');
exec('node test/fixtures/node_flags.js --lazy', function (err, stdout, stderr) {
expect(stderr).to.equal("--lazy\n");
exec('node test/fixtures/v8flags_function.js', function (err, stdout, stderr) {
expect(stderr).to.equal('\n');
done();
});
});

});

it('should respawn if process.argv has values from v8flags in it', function (done) {
exec('node test/fixtures/v8flags.js --lazy', function (err, stdout, stderr) {
expect(stderr).to.equal("--lazy\n");
exec('node test/fixtures/v8flags_function.js --lazy', function (err, stdout, stderr) {
expect(stderr).to.equal("--lazy\n");
done();
});
});
});

it('should emit a respawn event if a respawn is required', function (done) {
exec('node test/fixtures/node_flags.js', function (err, stdout) {
exec('node test/fixtures/v8flags.js', function (err, stdout) {
expect(stdout).to.be.empty;
done();
exec('node test/fixtures/node_flags.js --lazy', function (err, stdout) {
exec('node test/fixtures/v8flags_function.js --lazy', function (err, stdout) {
expect(stdout).to.equal('saw respawn\n');
done();
});
Expand Down

0 comments on commit 10a3940

Please sign in to comment.