Load config earlier in pluginManager and store as instance property to prevent duplicate calls to getServerlessConfigFile.

This commit is contained in:
Shane Handley 2018-12-30 18:33:18 +11:00
parent 6c861055af
commit 7cbcac0bb2
3 changed files with 42 additions and 31 deletions

View File

@ -58,6 +58,7 @@ class Serverless {
this.processedInput = this.cli.processInput();
// set the options and commands which were processed by the CLI
this.pluginManager.loadConfig();
this.pluginManager.setCliOptions(this.processedInput.options);
this.pluginManager.setCliCommands(this.processedInput.commands);

View File

@ -29,6 +29,7 @@ class TerminateHookChain extends Error {
class PluginManager {
constructor(serverless) {
this.serverless = serverless;
this.config = null;
this.cliOptions = {};
this.cliCommands = [];
@ -40,6 +41,12 @@ class PluginManager {
this.deprecatedEvents = {};
}
loadConfig() {
getServerlessConfigFile(this.serverless.config.servicePath).then((serverlessConfigFile) => {
this.config = serverlessConfigFile;
});
}
setCliOptions(options) {
this.cliOptions = options;
}
@ -436,19 +443,12 @@ class PluginManager {
*/
validateServerlessConfigDependency(command) {
if ('configDependent' in command && command.configDependent) {
return getServerlessConfigFile().then((serverlessConfigFile) => {
if (!serverlessConfigFile) {
const error = new this.serverless.classes.Error(
'This command can only be run in a Serverless service directory'
);
return BbPromise.reject(error);
}
return BbPromise.resolve();
});
if (!this.config) {
throw new this.serverless.classes.Error(
'This command can only be run in a Serverless service directory'
);
}
}
return BbPromise.resolve();
}
validateOptions(command) {

View File

@ -1184,7 +1184,7 @@ describe('PluginManager', () => {
});
it('should continue loading if the configDependent property is absent', () => {
getServerlessConfigFileStub.resolves({ service: 'my-service' });
pluginManagerInstance.config = null;
pluginManagerInstance.commands = {
foo: {},
@ -1192,11 +1192,11 @@ describe('PluginManager', () => {
const foo = pluginManagerInstance.commands.foo;
return expect(pluginManagerInstance.validateServerlessConfigDependency(foo)).to.be.fulfilled;
expect(pluginManagerInstance.validateServerlessConfigDependency(foo)).to.be.undefined;
});
it('should load if the configDependent property has a false value', () => {
getServerlessConfigFileStub.resolves('');
it('should load if the configDependent property has a false value and config is null', () => {
pluginManagerInstance.config = null;
pluginManagerInstance.commands = {
foo: {
@ -1206,11 +1206,11 @@ describe('PluginManager', () => {
const foo = pluginManagerInstance.commands.foo;
return expect(pluginManagerInstance.validateServerlessConfigDependency(foo)).to.be.fillfilled;
expect(pluginManagerInstance.validateServerlessConfigDependency(foo)).to.be.undefined;
});
it('should load if the configDependent property has a true value, and config is found', () => {
getServerlessConfigFileStub.resolves({ service: 'my-service' });
it('should throw an error if configDependent has a true value and no config is found', () => {
pluginManagerInstance.config = null;
pluginManagerInstance.commands = {
foo: {
@ -1220,15 +1220,11 @@ describe('PluginManager', () => {
const foo = pluginManagerInstance.commands.foo;
return expect(
pluginManagerInstance.validateServerlessConfigDependency(foo)
).to.be.fulfilled.then(() => {
expect(getServerlessConfigFileStub).to.have.been.calledOnce;
});
expect(() => { pluginManager.validateServerlessConfigDependency(foo); }).to.throw(Error);
});
it('should error if configDependent has a true value and no config is found', () => {
getServerlessConfigFileStub.resolves('');
it('should throw an error if configDependent has a true value and config was returned as an empty string', () => {
pluginManagerInstance.config = '';
pluginManagerInstance.commands = {
foo: {
@ -1238,12 +1234,26 @@ describe('PluginManager', () => {
const foo = pluginManagerInstance.commands.foo;
return expect(
pluginManagerInstance.validateServerlessConfigDependency(foo)
).to.be.rejected.then(() => {
expect(getServerlessConfigFileStub).to.have.been.calledOnce;
});
expect(() => { pluginManager.validateServerlessConfigDependency(foo); }).to.throw(Error);
});
it('should load if the configDependent property has a true value, and config exists', () => {
pluginManagerInstance.config = {
servicePath: 'foo'
};
pluginManagerInstance.commands = {
foo: {
configDependent: true,
},
};
const foo = pluginManagerInstance.commands.foo;
expect(pluginManagerInstance.validateServerlessConfigDependency(foo)).to.be.undefined;
});
});
describe('#validateOptions()', () => {