diff --git a/lib/plugins/config.js b/lib/plugins/config.js index f44bf8c722c..d324d63dcfd 100644 --- a/lib/plugins/config.js +++ b/lib/plugins/config.js @@ -1,5 +1,6 @@ 'use strict'; +const BbPromise = require('bluebird'); const config = require('@serverless/utils/config'); const ServerlessError = require('../serverless-error'); const isTabTabCompletionSupported = require('../utils/tabCompletion/isSupported'); @@ -147,51 +148,57 @@ class Config { } async tabtabCompletionInstall() { - const shell = this.serverless.processedInput.options.shell || 'bash'; + return BbPromise.try(() => { + const shell = this.serverless.processedInput.options.shell || 'bash'; - if (!validShells.has(shell)) { - throw new ServerlessError( - `Shell "${shell}" is not supported. Supported shells: ${Array.from(validShells)}.` - ); - } - const location = (() => { - if (this.serverless.processedInput.options.location) { - return this.serverless.processedInput.options.location; - } - const { BASH_LOCATION, FISH_LOCATION, ZSH_LOCATION } = require('tabtab/lib/constants'); - switch (shell) { - case 'bash': - return BASH_LOCATION; - case 'zsh': - return ZSH_LOCATION; - case 'fish': - return FISH_LOCATION; - default: - throw new Error('Unexpected shell choice'); - } - })(); - - const { install } = require('tabtab/lib/installer'); - await muteConsoleLog(async () => { - for (const options of tabtabOptions) { - await install(Object.assign({ location }, options)); + if (!validShells.has(shell)) { + throw new ServerlessError( + `Shell "${shell}" is not supported. Supported shells: ${Array.from(validShells)}.` + ); } + const location = (() => { + if (this.serverless.processedInput.options.location) { + return this.serverless.processedInput.options.location; + } + const { BASH_LOCATION, FISH_LOCATION, ZSH_LOCATION } = require('tabtab/lib/constants'); + switch (shell) { + case 'bash': + return BASH_LOCATION; + case 'zsh': + return ZSH_LOCATION; + case 'fish': + return FISH_LOCATION; + default: + throw new Error('Unexpected shell choice'); + } + })(); + const { install } = require('tabtab/lib/installer'); + return muteConsoleLog(() => + tabtabOptions.reduce( + (previousOperation, options) => + previousOperation.then(() => install(Object.assign({ location }, options))), + BbPromise.resolve() + ) + ).then(() => + this.serverless.cli.log( + `Tab autocompletion setup for ${shell}. Make sure to reload your SHELL.` + ) + ); }); - - this.serverless.cli.log( - `Tab autocompletion setup for ${shell}. Make sure to reload your SHELL.` - ); } async tabtabCompletionUninstall() { - const { uninstall } = require('tabtab/lib/installer'); - await muteConsoleLog(async () => { - for (const options of tabtabOptions) { - await uninstall(options); - } + return BbPromise.try(() => { + const { uninstall } = require('tabtab/lib/installer'); + return muteConsoleLog(() => + tabtabOptions.reduce( + (previousOperation, options) => previousOperation.then(() => uninstall(options)), + BbPromise.resolve() + ) + ).then(() => + this.serverless.cli.log('Tab autocompletion uninstalled (for all configured shells).') + ); }); - - this.serverless.cli.log('Tab autocompletion uninstalled (for all configured shells).'); } } diff --git a/lib/plugins/deploy.js b/lib/plugins/deploy.js index 6441add535b..c91344d4a22 100644 --- a/lib/plugins/deploy.js +++ b/lib/plugins/deploy.js @@ -125,9 +125,8 @@ class Deploy { } }, 'after:deploy:finalize': async () => { - if (!this.deferredBackendNotificationRequest) return; - const notifications = await this.deferredBackendNotificationRequest; - processBackendNotificationRequest(notifications); + if (!this.deferredBackendNotificationRequest) return null; + return this.deferredBackendNotificationRequest.then(processBackendNotificationRequest); }, }; } diff --git a/lib/plugins/install.js b/lib/plugins/install.js index e558b89ecb8..524120308af 100644 --- a/lib/plugins/install.js +++ b/lib/plugins/install.js @@ -1,5 +1,7 @@ 'use strict'; +const BbPromise = require('bluebird'); + const download = require('../utils/downloadTemplateFromRepo'); class Install { @@ -26,23 +28,25 @@ class Install { }; this.hooks = { - 'install:install': async () => this.install(), + 'install:install': async () => BbPromise.bind(this).then(this.install), }; } async install() { - const serviceName = await download.downloadTemplateFromRepo( - this.options.url, - this.options.name - ); - const message = [ - `Successfully installed "${serviceName}" `, - `${ - this.options.name && this.options.name !== serviceName ? `as "${this.options.name}"` : '' - }`, - ].join(''); - - this.serverless.cli.log(message); + return download + .downloadTemplateFromRepo(this.options.url, this.options.name) + .then((serviceName) => { + const message = [ + `Successfully installed "${serviceName}" `, + `${ + this.options.name && this.options.name !== serviceName + ? `as "${this.options.name}"` + : '' + }`, + ].join(''); + + this.serverless.cli.log(message); + }); } } diff --git a/lib/plugins/invoke.js b/lib/plugins/invoke.js index b5d3605516d..a7e3f4fff00 100644 --- a/lib/plugins/invoke.js +++ b/lib/plugins/invoke.js @@ -1,5 +1,6 @@ 'use strict'; +const BbPromise = require('bluebird'); const _ = require('lodash'); class Invoke { @@ -100,9 +101,9 @@ class Invoke { }; this.hooks = { - 'invoke:local:loadEnvVars': this.loadEnvVarsForLocal.bind(this), - 'after:invoke:invoke': this.trackInvoke.bind(this), - 'after:invoke:local:invoke': this.trackInvokeLocal.bind(this), + 'invoke:local:loadEnvVars': async () => BbPromise.bind(this).then(this.loadEnvVarsForLocal), + 'after:invoke:invoke': async () => BbPromise.bind(this).then(this.trackInvoke), + 'after:invoke:local:invoke': async () => BbPromise.bind(this).then(this.trackInvokeLocal), }; } diff --git a/lib/plugins/print.js b/lib/plugins/print.js index 5b1e8ac3b7f..4dc5da4e893 100644 --- a/lib/plugins/print.js +++ b/lib/plugins/print.js @@ -2,6 +2,7 @@ const os = require('os'); const _ = require('lodash'); +const BbPromise = require('bluebird'); const jc = require('json-cycle'); const yaml = require('js-yaml'); const ServerlessError = require('../serverless-error'); diff --git a/test/unit/lib/plugins/config.test.js b/test/unit/lib/plugins/config.test.js index 9ee7e4b4c59..8e68e3f9f7e 100644 --- a/test/unit/lib/plugins/config.test.js +++ b/test/unit/lib/plugins/config.test.js @@ -1,14 +1,17 @@ 'use strict'; -const fs = require('fs').promises; +const fs = require('fs'); const path = require('path'); const os = require('os'); +const BbPromise = require('bluebird'); const { expect } = require('chai'); const config = require('@serverless/utils/config'); const ServerlessError = require('../../../../lib/serverless-error'); const runServerless = require('../../../utils/run-serverless'); const isTabCompletionSupported = require('../../../../lib/utils/tabCompletion/isSupported'); +BbPromise.promisifyAll(fs); + const unexpected = () => { throw new Error('Unexpected'); }; @@ -41,12 +44,12 @@ describe('Config', () => { }).then(() => Promise.all([ fs - .readFile(path.resolve(os.homedir(), '.bashrc'), 'utf8') + .readFileAsync(path.resolve(os.homedir(), '.bashrc'), 'utf8') .then((bashRcContent) => expect(bashRcContent).to.include(' ~/.config/tabtab/__tabtab.bash') ), - fs.readFile(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'), 'utf8'), - fs.readFile(path.resolve(os.homedir(), '.config/tabtab/sls.bash'), 'utf8'), + fs.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'), 'utf8'), + fs.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/sls.bash'), 'utf8'), ]) )); @@ -63,10 +66,10 @@ describe('Config', () => { }).then(() => Promise.all([ fs - .readFile(path.resolve(os.homedir(), '.config/tabtab/serverless.bash')) + .readFileAsync(path.resolve(os.homedir(), '.config/tabtab/serverless.bash')) .then(unexpected, (error) => expect(error.code).to.equal('ENOENT')), fs - .readFile(path.resolve(os.homedir(), '.config/tabtab/sls.bash')) + .readFileAsync(path.resolve(os.homedir(), '.config/tabtab/sls.bash')) .then(unexpected, (error) => expect(error.code).to.equal('ENOENT')), ]) ) diff --git a/test/unit/lib/plugins/deploy.test.js b/test/unit/lib/plugins/deploy.test.js index 56588ce041d..fa162ef7ff7 100644 --- a/test/unit/lib/plugins/deploy.test.js +++ b/test/unit/lib/plugins/deploy.test.js @@ -1,5 +1,6 @@ 'use strict'; +const BbPromise = require('bluebird'); const chai = require('chai'); const Deploy = require('../../../../lib/plugins/deploy'); const Serverless = require('../../../../lib/Serverless'); @@ -70,7 +71,7 @@ describe('Deploy', () => { deploy.serverless.service.package.path = false; return expect(deploy.hooks['before:deploy:deploy']()).to.be.fulfilled.then(() => - Promise.all([ + BbPromise.all([ expect(spawnDeployFunctionStub).to.not.be.called, expect(spawnPackageStub).to.be.calledOnce, expect(spawnPackageStub).to.be.calledWithExactly('package'), @@ -84,7 +85,7 @@ describe('Deploy', () => { deploy.serverless.service.package.path = false; return expect(deploy.hooks['before:deploy:deploy']()).to.be.fulfilled.then(() => - Promise.all([ + BbPromise.all([ expect(spawnPackageStub).to.not.be.called, expect(spawnDeployFunctionStub).to.be.calledOnce, expect(spawnDeployFunctionStub).to.be.calledWithExactly('deploy:function', { diff --git a/test/unit/lib/plugins/invoke.test.js b/test/unit/lib/plugins/invoke.test.js index 794e65443f7..67f36ba1094 100644 --- a/test/unit/lib/plugins/invoke.test.js +++ b/test/unit/lib/plugins/invoke.test.js @@ -46,34 +46,32 @@ describe('Invoke', () => { expect(invoke.commands.invoke.commands.local.lifecycleEvents).to.contain('loadEnvVars'); }); - it('should set IS_LOCAL', () => { - invoke.hooks['invoke:local:loadEnvVars'](); - - expect(process.env.IS_LOCAL).to.equal('true'); - expect(serverless.service.provider.environment.IS_LOCAL).to.equal('true'); - }); + it('should set IS_LOCAL', () => + expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled.then(() => { + expect(process.env.IS_LOCAL).to.equal('true'); + expect(serverless.service.provider.environment.IS_LOCAL).to.equal('true'); + })); it('should leave provider env variable untouched if already defined', () => { serverless.service.provider.environment = { IS_LOCAL: 'false' }; - invoke.hooks['invoke:local:loadEnvVars'](); - - expect(serverless.service.provider.environment.IS_LOCAL).to.equal('false'); + return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled.then(() => { + expect(serverless.service.provider.environment.IS_LOCAL).to.equal('false'); + }); }); it('should accept a single env option', () => { invoke.options = { env: 'NAME=value' }; - invoke.hooks['invoke:local:loadEnvVars'](); - - expect(process.env.NAME).to.equal('value'); + return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled.then(() => + expect(process.env.NAME).to.equal('value') + ); }); it('should accept multiple env options', () => { invoke.options = { env: ['NAME1=val1', 'NAME2=val2'] }; - invoke.hooks['invoke:local:loadEnvVars'](); - - expect(process.env.NAME1).to.equal('val1'); - expect(process.env.NAME2).to.equal('val2'); + return expect(invoke.hooks['invoke:local:loadEnvVars']()) + .to.be.fulfilled.then(() => expect(process.env.NAME1).to.equal('val1')) + .then(() => expect(process.env.NAME2).to.equal('val2')); }); }); });