From dea68609a669195f8d59df2164a5f4ba6e680004 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Sat, 20 Apr 2019 18:30:16 +0200 Subject: [PATCH] chore!: Remove launch API --- README.md | 31 -- index.js | 13 - package.json | 3 +- .../developing_yourself/app1/index.js | 10 +- .../developing_yourself/app2/index.js | 10 +- test/fixtures/developing_yourself/main.js | 10 +- test/fixtures/nodeflags_only.js | 17 - test/fixtures/respawn_and_require.js | 10 +- test/fixtures/v8flags.js | 13 - test/fixtures/v8flags_config.js | 16 - test/fixtures/v8flags_error.js | 17 - test/fixtures/v8flags_function.js | 17 - test/fixtures/v8flags_value.js | 13 - test/launch.js | 456 ------------------ 14 files changed, 25 insertions(+), 611 deletions(-) delete mode 100644 test/fixtures/nodeflags_only.js delete mode 100644 test/fixtures/v8flags.js delete mode 100644 test/fixtures/v8flags_config.js delete mode 100644 test/fixtures/v8flags_error.js delete mode 100644 test/fixtures/v8flags_function.js delete mode 100644 test/fixtures/v8flags_value.js delete mode 100644 test/launch.js diff --git a/README.md b/README.md index af830c4..684d127 100644 --- a/README.md +++ b/README.md @@ -444,37 +444,6 @@ A function called after your application is executed. When invoked, `this` will - `modulePackage`: the contents of the local module's package.json (if found) - `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found) -### launch(opts, callback(env, argv)) - -**Deprecated:** Please use `prepare` followed by `execute`. That's all this module does internally but those give you more control. - -Launches your application with provided options, builds an environment, and invokes your callback, passing the calculated environment and command-line arguments (minus node & v8 flags) as the arguments. - -Accepts any options that `prepare` allows, plus `opt.forcedFlags`. - -#### opts.forcedFlags - -**Deprecated:** If using `prepare`/`execute`, pass forcedFlags as the 2nd argument instead of using this option. - -Allows you to force node or V8 flags during the launch. This is useful if you need to make sure certain flags will always be enabled or if you need to enable flags that don't show up in `opts.v8flags` (as these flags aren't validated against `opts.v8flags`). - -If this is specified as a function, it will receive the built `env` as its only argument and must return a string or array of flags to force. - -Type: `String|Array|Function` -Default: `null` - -**Example Configuration:** -```js -MyApp.launch({ - forcedFlags: ['--trace-deprecation'] -}, invoke); -``` - -**Matching CLI Invocation:** -```js -myapp --trace-deprecation -``` - ### events #### require(name, module) diff --git a/index.js b/index.js index 9330c92..b847487 100644 --- a/index.js +++ b/index.js @@ -204,19 +204,6 @@ Liftoff.prototype.execute = function(env, forcedFlags, fn) { }.bind(this)); }; -Liftoff.prototype.launch = function(opts, fn) { - if (typeof fn !== 'function') { - throw new Error('You must provide a callback function.'); - } - - var self = this; - - self.prepare(opts, function(env) { - var forcedFlags = getNodeFlags.arrayOrFunction(opts.forcedFlags, env); - self.execute(env, forcedFlags, fn); - }); -}; - function preloadModules(inst, env) { var basedir = env.cwd; env.require.filter(toUnique).forEach(function(module) { diff --git a/package.json b/package.json index 2391471..889500e 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ ], "scripts": { "pretest": "eslint .", - "test": "mocha -t 5000 -b -R spec test/index && npm run legacy-test", - "legacy-test": "mocha -t 5000 -b -R spec test/launch", + "test": "mocha -t 5000 -b -R spec test/index", "cover": "nyc --reporter=lcov --reporter=text-summary npm test" }, "dependencies": { diff --git a/test/fixtures/developing_yourself/app1/index.js b/test/fixtures/developing_yourself/app1/index.js index 913d553..4c9a9af 100644 --- a/test/fixtures/developing_yourself/app1/index.js +++ b/test/fixtures/developing_yourself/app1/index.js @@ -4,8 +4,10 @@ var app1 = new Liftoff({ name: 'app1' }); -app1.launch({}, function(env) { - console.log(env.modulePackage); - console.log(env.modulePath); - console.log(env.cwd); +app1.prepare({}, function(env) { + app1.execute(env, function(env) { + console.log(env.modulePackage); + console.log(env.modulePath); + console.log(env.cwd); + }); }); diff --git a/test/fixtures/developing_yourself/app2/index.js b/test/fixtures/developing_yourself/app2/index.js index a40c8ac..87d9d81 100644 --- a/test/fixtures/developing_yourself/app2/index.js +++ b/test/fixtures/developing_yourself/app2/index.js @@ -4,8 +4,10 @@ var app2 = new Liftoff({ name: 'app2' }); -app2.launch({}, function(env) { - console.log(JSON.stringify(env.modulePackage)); - console.log(env.modulePath); - console.log(env.cwd); +app2.prepare({}, function(env) { + app2.execute(env, function(env) { + console.log(JSON.stringify(env.modulePackage)); + console.log(env.modulePath); + console.log(env.cwd); + }); }); diff --git a/test/fixtures/developing_yourself/main.js b/test/fixtures/developing_yourself/main.js index 663d5f2..12bbf17 100644 --- a/test/fixtures/developing_yourself/main.js +++ b/test/fixtures/developing_yourself/main.js @@ -4,8 +4,10 @@ var app0 = new Liftoff({ name: 'app0' }); -app0.launch({}, function(env) { - console.log(JSON.stringify(env.modulePackage)); - console.log(env.modulePath); - console.log(env.cwd); +app0.prepare({}, function(env) { + app0.execute(env, function(env) { + console.log(JSON.stringify(env.modulePackage)); + console.log(env.modulePath); + console.log(env.cwd); + }); }); diff --git a/test/fixtures/nodeflags_only.js b/test/fixtures/nodeflags_only.js deleted file mode 100644 index 762d34e..0000000 --- a/test/fixtures/nodeflags_only.js +++ /dev/null @@ -1,17 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', -}); - -Test.on('respawn', function(execArgv) { - console.log('saw respawn', execArgv); -}); - -Test.launch({ - forcedFlags: function(env) { - return ['--lazy']; - }, -}, function(env, argv) { - console.error(argv.slice(1).join(' ')); -}); diff --git a/test/fixtures/respawn_and_require.js b/test/fixtures/respawn_and_require.js index ee2a722..60c6fef 100644 --- a/test/fixtures/respawn_and_require.js +++ b/test/fixtures/respawn_and_require.js @@ -13,9 +13,11 @@ Test.on('require', function(name) { console.log('require', name); }); -Test.launch({ +Test.prepare({ require: 'coffeescript/register', - forcedFlags: ['--lazy'], -}, function(env, argv) { - console.log('execute'); +}, function(env) { + var forcedFlags = ['--lazy']; + Test.execute(env, forcedFlags, function() { + console.log('execute'); + }); }); diff --git a/test/fixtures/v8flags.js b/test/fixtures/v8flags.js deleted file mode 100644 index d3f6c74..0000000 --- a/test/fixtures/v8flags.js +++ /dev/null @@ -1,13 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', - v8flags: ['--lazy'] -}); -Test.on('respawn', function(flags, proc) { - console.log('saw respawn'); -}); - -Test.launch({}, function(env) { - console.error(process.execArgv.join('')); -}); diff --git a/test/fixtures/v8flags_config.js b/test/fixtures/v8flags_config.js deleted file mode 100644 index b8cd77a..0000000 --- a/test/fixtures/v8flags_config.js +++ /dev/null @@ -1,16 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', - v8flags: ['--harmony'], -}); - -Test.on('respawn', function(flags, proc) { - console.log('saw respawn', flags); -}); - -Test.launch({ - forcedFlags: ['--lazy'], -}, function(env, argv) { - console.error(argv.slice(1).join(' ')); -}); diff --git a/test/fixtures/v8flags_error.js b/test/fixtures/v8flags_error.js deleted file mode 100644 index 338232b..0000000 --- a/test/fixtures/v8flags_error.js +++ /dev/null @@ -1,17 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', - v8flags: function(cb) { - process.nextTick(function() { - cb(new Error('v8flags error!'), ['--lazy']); - }) - } -}); -Test.on('respawn', function(flags, proc) { - console.log('saw respawn'); -}); - -Test.launch({}, function(env) { - console.error(process.execArgv.join('')); -}); diff --git a/test/fixtures/v8flags_function.js b/test/fixtures/v8flags_function.js deleted file mode 100644 index dd7301b..0000000 --- a/test/fixtures/v8flags_function.js +++ /dev/null @@ -1,17 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', - v8flags: function(cb) { - process.nextTick(function() { - cb(null, ['--lazy']); - }) - } -}); -Test.on('respawn', function(flags, proc) { - console.log('saw respawn'); -}); - -Test.launch({}, function(env) { - console.error(process.execArgv.join('')); -}); diff --git a/test/fixtures/v8flags_value.js b/test/fixtures/v8flags_value.js deleted file mode 100644 index ddc7917..0000000 --- a/test/fixtures/v8flags_value.js +++ /dev/null @@ -1,13 +0,0 @@ -var Liftoff = require('../..'); - -var Test = new Liftoff({ - name: 'test', - v8flags: ['--stack_size'] -}); - -Test.on('respawn', function(flags) { - console.error(flags.join(' ')); -}); - -Test.launch({}, function() { -}); diff --git a/test/launch.js b/test/launch.js deleted file mode 100644 index a71ab40..0000000 --- a/test/launch.js +++ /dev/null @@ -1,456 +0,0 @@ -var Liftoff = require('../'); -var path = require('path'); -var expect = require('chai').expect; -var exec = require('child_process').exec; - -var NAME = 'mocha'; -var app = new Liftoff({ - processTitle: NAME, - configName: NAME + 'file', - moduleName: NAME, - extensions: { - '.js': null, - '.json': null, - '.coffee': 'coffee-script/register', - '.coffee.md': 'coffee-script/register', - }, - searchPaths: ['test/fixtures/search_path'], -}); - -describe('Liftoff (legacy)', function() { - - describe('launch', function() { - // These are legacy tests to ensure launch does everything it used to - // They have also been duplicated into the `prepare` and `execute` suites for future testing - it('should set the process.title to the moduleName', function() { - app.launch({}, function() {}); - expect(process.title).to.equal(app.moduleName); - }); - - it('should return early if completions are available and requested', function(done) { - var test = new Liftoff({ - name: 'whatever', - completions: function() { - done(); - }, - }); - test.launch({ completion: true }, function() {}); - }); - - it('should call launch with liftoff instance as context', function(done) { - app.launch({}, function() { - expect(this).to.equal(app); - done(); - }); - }); - - it('should pass environment to first argument of launch callback', function(done) { - app.launch({}, function(env) { - expect(env).to.deep.equal(app.buildEnvironment()); - done(); - }); - }); - - it('should throw if 2nd arg is not a function', function() { - expect(function() { - app.launch({}); - }).to.throw(); - }); - - 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/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 throw if v8flags is a function and it causes an error', function(done) { - exec('node test/fixtures/v8flags_error.js --lazy', function(err, stdout, stderr) { - expect(err).not.to.equal(null); - expect(stdout).to.equal(''); - expect(stderr).to.include('v8flags error!'); - done(); - }); - }); - - it('should respawn if v8flag is set by opts.forcedFlags', 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.forcedFlags', 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; - exec('node test/fixtures/v8flags_function.js --lazy', function(err, stdout) { - expect(stdout).to.equal('saw respawn\n'); - done(); - }); - }); - }); - - it('should respawn if process.argv has v8flags with values in it', function(done) { - exec('node test/fixtures/v8flags_value.js --stack_size=2048', function(err, stdout, stderr) { - expect(stderr).to.equal('--stack_size=2048\n'); - done(); - }); - }); - - it('should respawn if v8flags is empty but forcedFlags 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() { - - it('should attempt pre-loading local modules if they are requested', function(done) { - var app = new Liftoff({ name: 'test' }); - var logs = []; - app.on('require', function(moduleName, module) { - expect(moduleName).to.equal('coffeescript/register'); - expect(module).to.equal(require('coffeescript/register')); - logs.push('require'); - }); - app.on('requireFail', function(moduleName, err) { - done(err); - }); - app.launch({ require: ['coffeescript/register'] }, function(env) { - expect(env.require).to.deep.equal(['coffeescript/register']); - expect(logs).to.deep.equal(['require']); - done(); - }); - }); - - it('should attempt pre-loading a local module if it is requested', function(done) { - var app = new Liftoff({ name: 'test' }); - var logs = []; - app.on('require', function(moduleName, module) { - expect(moduleName).to.equal('coffeescript/register'); - expect(module).to.equal(require('coffeescript/register')); - logs.push('require'); - }); - app.on('requireFail', function(moduleName, err) { - done(err); - }); - app.launch({ require: 'coffeescript/register' }, function(env) { - expect(env.require).to.deep.equal(['coffeescript/register']); - expect(logs).to.deep.equal(['require']); - done(); - }); - }); - - it('should attempt pre-loading local modules but fail', function(done) { - var app = new Liftoff({ name: 'test' }); - var logs = []; - app.on('require', function(/* moduleName, module */) { - done(); - }); - app.on('requireFail', function(moduleName, err) { - expect(moduleName).to.equal('badmodule'); - expect(err).to.not.equal(null); - logs.push('requireFail'); - }); - app.launch({ require: 'badmodule' }, function(env) { - expect(env.require).to.deep.equal(['badmodule']); - expect(logs).to.deep.equal(['requireFail']); - done(); - }); - }); - - it('should pre-load a local module only once even if be respawned', function(done) { - var fixturesDir = path.resolve(__dirname, 'fixtures'); - - exec('cd ' + fixturesDir + ' && node respawn_and_require.js', cb); - function cb(err, stdout, stderr) { - expect(err).to.equal(null); - expect(stderr).to.equal(''); - expect(stdout).to.equal( - 'saw respawn [ \'--lazy\' ]\n' + - 'require coffeescript/register\n' + - 'execute\n' + - ''); - done(); - } - }); - - it('should emit `require` with the name of the module and the required module', function(done) { - var requireTest = new Liftoff({ name: 'require' }); - requireTest.on('require', function(name, module) { - expect(name).to.equal('mocha'); - expect(module).to.equal(require('mocha')); - done(); - }); - requireTest.requireLocal('mocha', __dirname); - }); - - it('should emit `requireFail` with an error if a module can\'t be found.', function(done) { - var requireFailTest = new Liftoff({ name: 'requireFail' }); - requireFailTest.on('requireFail', function(name) { - expect(name).to.equal('badmodule'); - done(); - }); - requireFailTest.requireLocal('badmodule', __dirname); - }); - - }); - - describe('configFiles', function() { - it('should be empty if not specified', function(done) { - var app = new Liftoff({ - name: 'myapp', - }); - app.launch({}, function(env) { - expect(env.configFiles).to.deep.equal({}); - done(); - }); - }); - - it('should find multiple files if specified', function(done) { - var app = new Liftoff({ - name: 'myapp', - configFiles: { - index: { - currentdir: '.', - test: { - path: 'test/fixtures/configfiles', - }, - findingup: { - path: 'test', - cwd: 'test/fixtures/configfiles', - findUp: true, - }, - }, - package: { - currentdir: '.', - test: { - path: 'test/fixtures/configfiles', - }, - findingup: { - path: 'test', - cwd: 'test/fixtures/configfiles', - findUp: true, - }, - }, - }, - }); - app.launch({}, function(env) { - expect(env.configFiles).to.deep.equal({ - index: { - currentdir: path.resolve('./index.js'), - test: path.resolve('./test/fixtures/configfiles/index.json'), - findingup: path.resolve('./test/index.js'), - }, - package: { - currentdir: path.resolve('./package.json'), - test: null, - findingup: null, - }, - }); - done(); - }); - }); - - it('should use default cwd if not specified', function(done) { - var app = new Liftoff({ - name: 'myapp', - configFiles: { - index: { - cwd: { - path: '.', - extensions: ['.js', '.json'], - }, - }, - }, - }); - app.launch({ - cwd: 'test/fixtures/configfiles', - }, function(env) { - expect(env.configFiles).to.deep.equal({ - index: { - cwd: path.resolve('./test/fixtures/configfiles/index.json'), - }, - }); - done(); - }); - }); - - it('should use default extensions if not specified', function(done) { - var app = new Liftoff({ - extensions: { '.md': null, '.txt': null }, - name: 'myapp', - configFiles: { - README: { - markdown: { - path: '.', - }, - text: { - path: 'test/fixtures/configfiles', - }, - markdown2: { - path: '.', - extensions: ['.json', '.js'], - }, - text2: { - path: 'test/fixtures/configfiles', - extensions: ['.json', '.js'], - }, - }, - }, - }); - app.launch({}, function(env) { - expect(env.configFiles).to.deep.equal({ - README: { - markdown: path.resolve('./README.md'), - text: path.resolve('./test/fixtures/configfiles/README.txt'), - markdown2: null, - text2: null, - }, - }); - done(); - }); - }); - - it('should use specified loaders', function(done) { - var logRequire = []; - var logFailure = []; - - var app = new Liftoff({ - extensions: { - '.md': './test/fixtures/configfiles/require-md', - }, - name: 'myapp', - configFiles: { - README: { - text_null: { - path: 'test/fixtures/configfiles', - }, - text_err: { - path: 'test/fixtures/configfiles', - extensions: { - '.txt': './test/fixtures/configfiles/require-non-exist', - }, - }, - text: { - path: 'test/fixtures/configfiles', - extensions: { - '.txt': './test/fixtures/configfiles/require-txt', - }, - }, - markdown: { - path: '.', - }, - markdown_badext: { - path: '.', - extensions: { - '.txt': './test/fixtures/configfiles/require-txt', - }, - }, - markdown_badext2: { - path: '.', - extensions: { - '.txt': './test/fixtures/configfiles/require-non-exist', - }, - }, - }, - // Intrinsic extension-loader mappings are prioritized. - index: { - test: { - path: 'test/fixtures/configfiles', - extensions: { // ignored - '.js': './test/fixtures/configfiles/require-js', - '.json': './test/fixtures/configfiles/require-json', - }, - }, - }, - }, - }); - app.on('requireFail', function(moduleName, error) { - logFailure.push({ moduleName: moduleName, error: error }); - }); - app.on('require', function(moduleName, module) { - logRequire.push({ moduleName: moduleName, module: module }); - }); - app.launch({}, function(env) { - expect(env.configFiles).to.deep.equal({ - README: { - text: path.resolve('./test/fixtures/configfiles/README.txt'), - text_null: null, - text_err: path.resolve('./test/fixtures/configfiles/README.txt'), - markdown: path.resolve('./README.md'), - markdown_badext: null, - markdown_badext2: null, - }, - index: { - test: path.resolve('./test/fixtures/configfiles/index.json'), - }, - }); - - expect(logRequire.length).to.equal(2); - expect(logRequire[0].moduleName) - .to.equal('./test/fixtures/configfiles/require-txt'); - expect(logRequire[1].moduleName) - .to.equal('./test/fixtures/configfiles/require-md'); - - expect(logFailure.length).to.equal(1); - expect(logFailure[0].moduleName) - .to.equal('./test/fixtures/configfiles/require-non-exist'); - - expect(require(env.configFiles.README.markdown)) - .to.equal('Load README.md by require-md'); - expect(require(env.configFiles.README.text)).to - .to.equal('Load README.txt by require-txt'); - expect(require(env.configFiles.index.test)) - .to.deep.equal({ aaa: 'AAA' }); - done(); - }); - }); - }); - -});