Skip to content

Commit

Permalink
Merge d658c6d into 1a694ae
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Feb 19, 2021
2 parents 1a694ae + d658c6d commit adb697b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 96 deletions.
83 changes: 38 additions & 45 deletions lib/plugins/config.js
@@ -1,6 +1,5 @@
'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 @@ -148,57 +147,51 @@ class Config {
}

async tabtabCompletionInstall() {
return BbPromise.try(() => {
const shell = this.serverless.processedInput.options.shell || 'bash';
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');
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.`
)
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));
}
});

this.serverless.cli.log(
`Tab autocompletion setup for ${shell}. Make sure to reload your SHELL.`
);
}

async tabtabCompletionUninstall() {
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).')
);
const { uninstall } = require('tabtab/lib/installer');
await muteConsoleLog(async () => {
for (const options of tabtabOptions) {
await uninstall(options);
}
});

this.serverless.cli.log('Tab autocompletion uninstalled (for all configured shells).');
}
}

Expand Down
5 changes: 3 additions & 2 deletions lib/plugins/deploy.js
Expand Up @@ -125,8 +125,9 @@ class Deploy {
}
},
'after:deploy:finalize': async () => {
if (!this.deferredBackendNotificationRequest) return null;
return this.deferredBackendNotificationRequest.then(processBackendNotificationRequest);
if (!this.deferredBackendNotificationRequest) return;
const notifications = await this.deferredBackendNotificationRequest;
processBackendNotificationRequest(notifications);
},
};
}
Expand Down
30 changes: 13 additions & 17 deletions lib/plugins/install.js
@@ -1,7 +1,5 @@
'use strict';

const BbPromise = require('bluebird');

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

class Install {
Expand All @@ -28,25 +26,23 @@ class Install {
};

this.hooks = {
'install:install': async () => BbPromise.bind(this).then(this.install),
'install:install': async () => this.install(),
};
}

async install() {
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);
});
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);
}
}

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

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

class Invoke {
Expand Down Expand Up @@ -101,9 +100,9 @@ class Invoke {
};

this.hooks = {
'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),
'invoke:local:loadEnvVars': this.loadEnvVarsForLocal.bind(this),
'after:invoke:invoke': this.trackInvoke.bind(this),
'after:invoke:local:invoke': this.trackInvokeLocal.bind(this),
};
}

Expand Down
3 changes: 1 addition & 2 deletions lib/plugins/print.js
Expand Up @@ -2,7 +2,6 @@

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 Expand Up @@ -32,7 +31,7 @@ class Print {
},
};
this.hooks = {
'print:print': async () => BbPromise.bind(this).then(this.print),
'print:print': () => this.print.bind(this),
};
}

Expand Down
15 changes: 6 additions & 9 deletions test/unit/lib/plugins/config.test.js
@@ -1,17 +1,14 @@
'use strict';

const fs = require('fs');
const fs = require('fs').promises;
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 @@ -44,12 +41,12 @@ describe('Config', () => {
}).then(() =>
Promise.all([
fs
.readFileAsync(path.resolve(os.homedir(), '.bashrc'), 'utf8')
.readFile(path.resolve(os.homedir(), '.bashrc'), 'utf8')
.then((bashRcContent) =>
expect(bashRcContent).to.include(' ~/.config/tabtab/__tabtab.bash')
),
fs.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'), 'utf8'),
fs.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/sls.bash'), 'utf8'),
fs.readFile(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'), 'utf8'),
fs.readFile(path.resolve(os.homedir(), '.config/tabtab/sls.bash'), 'utf8'),
])
));

Expand All @@ -66,10 +63,10 @@ describe('Config', () => {
}).then(() =>
Promise.all([
fs
.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'))
.readFile(path.resolve(os.homedir(), '.config/tabtab/serverless.bash'))
.then(unexpected, (error) => expect(error.code).to.equal('ENOENT')),
fs
.readFileAsync(path.resolve(os.homedir(), '.config/tabtab/sls.bash'))
.readFile(path.resolve(os.homedir(), '.config/tabtab/sls.bash'))
.then(unexpected, (error) => expect(error.code).to.equal('ENOENT')),
])
)
Expand Down
5 changes: 2 additions & 3 deletions test/unit/lib/plugins/deploy.test.js
@@ -1,6 +1,5 @@
'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 @@ -71,7 +70,7 @@ describe('Deploy', () => {
deploy.serverless.service.package.path = false;

return expect(deploy.hooks['before:deploy:deploy']()).to.be.fulfilled.then(() =>
BbPromise.all([
Promise.all([
expect(spawnDeployFunctionStub).to.not.be.called,
expect(spawnPackageStub).to.be.calledOnce,
expect(spawnPackageStub).to.be.calledWithExactly('package'),
Expand All @@ -85,7 +84,7 @@ describe('Deploy', () => {
deploy.serverless.service.package.path = false;

return expect(deploy.hooks['before:deploy:deploy']()).to.be.fulfilled.then(() =>
BbPromise.all([
Promise.all([
expect(spawnPackageStub).to.not.be.called,
expect(spawnDeployFunctionStub).to.be.calledOnce,
expect(spawnDeployFunctionStub).to.be.calledWithExactly('deploy:function', {
Expand Down
30 changes: 16 additions & 14 deletions test/unit/lib/plugins/invoke.test.js
Expand Up @@ -46,32 +46,34 @@ describe('Invoke', () => {
expect(invoke.commands.invoke.commands.local.lifecycleEvents).to.contain('loadEnvVars');
});

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 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 leave provider env variable untouched if already defined', () => {
serverless.service.provider.environment = { IS_LOCAL: 'false' };
return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled.then(() => {
expect(serverless.service.provider.environment.IS_LOCAL).to.equal('false');
});
invoke.hooks['invoke:local:loadEnvVars']();

expect(serverless.service.provider.environment.IS_LOCAL).to.equal('false');
});

it('should accept a single env option', () => {
invoke.options = { env: 'NAME=value' };
return expect(invoke.hooks['invoke:local:loadEnvVars']()).to.be.fulfilled.then(() =>
expect(process.env.NAME).to.equal('value')
);
invoke.hooks['invoke:local:loadEnvVars']();

expect(process.env.NAME).to.equal('value');
});

it('should accept multiple env options', () => {
invoke.options = { env: ['NAME1=val1', 'NAME2=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'));
invoke.hooks['invoke:local:loadEnvVars']();

expect(process.env.NAME1).to.equal('val1');
expect(process.env.NAME2).to.equal('val2');
});
});
});
Expand Down

0 comments on commit adb697b

Please sign in to comment.