Skip to content

Commit

Permalink
refactor: Revert remove bluebird from lib/plugins
Browse files Browse the repository at this point in the history
This reverts commit 8fead7f.
  • Loading branch information
pgrzesik committed Mar 4, 2021
1 parent f62fc2e commit 7a012d8
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 81 deletions.
83 changes: 45 additions & 38 deletions lib/plugins/config.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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).');
}
}

Expand Down
5 changes: 2 additions & 3 deletions lib/plugins/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
};
}
Expand Down
30 changes: 17 additions & 13 deletions lib/plugins/install.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const BbPromise = require('bluebird');

const download = require('../utils/downloadTemplateFromRepo');

class Install {
Expand All @@ -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);
});
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/plugins/invoke.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const BbPromise = require('bluebird');
const _ = require('lodash');

class Invoke {
Expand Down Expand Up @@ -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),
};
}

Expand Down
1 change: 1 addition & 0 deletions lib/plugins/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
15 changes: 9 additions & 6 deletions test/unit/lib/plugins/config.test.js
Original file line number Diff line number Diff line change
@@ -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');
};
Expand Down Expand Up @@ -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'),
])
));

Expand All @@ -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')),
])
)
Expand Down
5 changes: 3 additions & 2 deletions test/unit/lib/plugins/deploy.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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'),
Expand All @@ -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', {
Expand Down
30 changes: 14 additions & 16 deletions test/unit/lib/plugins/invoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
});
Expand Down

0 comments on commit 7a012d8

Please sign in to comment.