Skip to content

Commit

Permalink
feat: Enable to config to specify forced flags in app launch
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk authored and phated committed Nov 22, 2021
1 parent 927a1d1 commit 0f4dd50
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 18 deletions.
31 changes: 16 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const parseOptions = require('./lib/parse_options');
const silentRequire = require('./lib/silent_require');
const buildConfigName = require('./lib/build_config_name');
const registerLoader = require('./lib/register_loader');
const getNodeFlags = require('./lib/get_node_flags');


function Liftoff (opts) {
Expand Down Expand Up @@ -185,21 +186,21 @@ Liftoff.prototype.launch = function (opts, fn) {
this.handleFlags(function (err, flags) {
if (err) {
throw err;
} else {
if (flags) {
flaggedRespawn(flags, process.argv, function (ready, child) {
if (child !== process) {
this.emit('respawn', process.argv.filter(function (arg) {
var flag = arg.split('=')[0];
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));
}
flags = flags || [];

var env = this.buildEnvironment(opts);

var nodeFlags = getNodeFlags.arrayOrFunction(this.nodeFlags, env);
flaggedRespawn(flags, process.argv, nodeFlags, execute.bind(this));

function execute(ready, child, argv) {
if (child !== process) {
var execArgv = getNodeFlags.fromReorderedArgv(argv);
this.emit('respawn', execArgv, child);
}
if (ready) {
fn.call(this, env, argv);
}
}
}.bind(this));
Expand Down
30 changes: 30 additions & 0 deletions lib/get_node_flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function arrayOrFunction(arrayOrFunc, env) {
if (typeof arrayOrFunc === 'function') {
return arrayOrFunc.call(this, env);
}
if (Array.isArray(arrayOrFunc)) {
return arrayOrFunc;
}
if (typeof arrayOrFunc === 'string') {
return [arrayOrFunc];
}
return [];
}

function fromReorderedArgv(reorderedArgv) {
var nodeFlags = [];
for (var i = 1, n = reorderedArgv.length; i < n; i++) {
var arg = reorderedArgv[i];
if (!/^-/.test(arg) || arg === '--') {
break;
}
nodeFlags.push(arg);
}
return nodeFlags;
}

module.exports = {
arrayOrFunction: arrayOrFunction,
fromReorderedArgv: fromReorderedArgv,
};

16 changes: 16 additions & 0 deletions test/fixtures/nodeflags_only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Liftoff = require('../..');

const Test = new Liftoff({
name: 'test',
nodeFlags: function(opts, env) {
return ['--lazy'];
},
});

Test.on('respawn', function(execArgv) {
console.log('saw respawn', execArgv);
});

Test.launch({}, function(env, argv) {
console.error(argv.slice(1).join(' '));
});
2 changes: 1 addition & 1 deletion test/fixtures/v8flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Test = new Liftoff({
name: 'test',
v8flags: ['--lazy']
});
Test.on('respawn', function (proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/v8flags_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Liftoff = require('../..');

const Test = new Liftoff({
name: 'test',
v8flags: ['--harmony'],
nodeFlags: ['--lazy'],
});

Test.on('respawn', function(flags, proc) {
console.log('saw respawn', flags);
});

Test.launch({}, function(env, argv) {
console.error(argv.slice(1).join(' '));
});
2 changes: 1 addition & 1 deletion test/fixtures/v8flags_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Test = new Liftoff({
})
}
});
Test.on('respawn', function (proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/v8flags_function.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const Test = new Liftoff({
})
}
});
Test.on('respawn', function (proc) {
Test.on('respawn', function (flags, proc) {
console.log('saw respawn');
});

Expand Down
45 changes: 45 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,35 @@ describe('Liftoff', function () {
});
});

it('should respawn if v8flag is set by opts.nodeFlags', function(done) {
exec('node test/fixtures/v8flags_config.js 123', cb);

function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal([
path.resolve('test/fixtures/v8flags_config.js'),
'123',
].join(' ') + '\n');
expect(stdout).to.equal('saw respawn [ \'--lazy\' ]\n');
done();
}
});

it('should respawn if v8flag is set by both cli flag and opts.nodeFlags', function(done) {
exec('node test/fixtures/v8flags_config.js 123 --harmony abc', cb);

function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal([
path.resolve('test/fixtures/v8flags_config.js'),
'123',
'abc',
].join(' ') + '\n');
expect(stdout).to.equal('saw respawn [ \'--lazy\', \'--harmony\' ]\n');
done();
}
});

it('should emit a respawn event if a respawn is required', function (done) {
exec('node test/fixtures/v8flags.js', function (err, stdout) {
expect(stdout).to.be.empty;
Expand All @@ -262,6 +291,21 @@ describe('Liftoff', function () {
});
});

it('should respawn if v8flags is empty but nodeFlags are specified',
function(done) {
exec('node test/fixtures/nodeflags_only.js 123', cb);

function cb(err, stdout, stderr) {
expect(err).to.equal(null);
expect(stderr).to.equal([
path.resolve('test/fixtures/nodeflags_only.js'),
'123',
].join(' ') + '\n');
expect(stdout).to.equal('saw respawn [ \'--lazy\' ]\n');
done();
}
});

});

describe('requireLocal', function () {
Expand Down Expand Up @@ -508,3 +552,4 @@ require('./lib/find_cwd');
require('./lib/parse_options');
require('./lib/silent_require');
require('./lib/register_loader');
require('./lib/get_node_flags');
72 changes: 72 additions & 0 deletions test/lib/get_node_flags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const expect = require('chai').expect;
const getNodeFlags = require('../../lib/get_node_flags');

describe('getNodeFlags', function() {

describe('arrayOrFunction', function() {
it('should return the first argument when it is an array', function() {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction([], env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction([
'--lazy', '--use_strict', '--harmony'
], env)).to.has.members([
'--lazy', '--harmony', '--use_strict'
]);
});

it('should return the exection result of the first argument when it is a function', function() {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction(function(env) {
return [];
}, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(function(arg) {
expect(arg).to.equal(env);
return ['--lazy', '--harmony'];
}, env)).to.has.members(['--lazy', '--harmony']);
});

it('should return an array which has an element of the first argument when the first argument is a string', function() {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction('--lazy', env)).to.has.members(['--lazy']);
});

it('should return an empty array when the first argument is neither an array, a function nor a string', function() {
var env = { cwd: 'aaa' };
expect(getNodeFlags.arrayOrFunction(undefined, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(null, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(true, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(false, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(0, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction(123, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction({}, env)).to.has.members([]);
expect(getNodeFlags.arrayOrFunction({ length: 1 }, env)).to.has.members([]);
});
});

describe('fromReorderedArgv', function() {
it('should return only node flags from respawning arguments', function() {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--harmony', '--use_strict', './aaa/bbb/app.js', '--ccc', 'ddd', '-e', 'fff'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).to.deep.equal(['--lazy', '--harmony', '--use_strict']);
});

it('should end node flags before "--"', function() {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--', '--harmony', '--use_strict', './aaa/bbb/app.js', '--ccc', 'ddd', '-e', 'fff'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).to.deep.equal(['--lazy']);

});

it('should return node flags when arguments are only node flags', function() {
var env = { cwd: 'aaa' };
var cmd = ['node', '--lazy', '--harmony', '--use_strict'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).to.deep.equal(['--lazy', '--harmony', '--use_strict']);
});

it('should return an empty array when no node flags', function() {
var env = { cwd: 'aaa' };
var cmd = ['node', './aaa/bbb/app.js', '--aaa', 'bbb', '-c', 'd'];
expect(getNodeFlags.fromReorderedArgv(cmd, env)).to.deep.equal([]);
});
});
});

0 comments on commit 0f4dd50

Please sign in to comment.