diff --git a/lib/plugins/aws/configCredentials/awsConfigCredentials.js b/lib/plugins/aws/configCredentials/awsConfigCredentials.js index 2f9b27076..3679d63e3 100644 --- a/lib/plugins/aws/configCredentials/awsConfigCredentials.js +++ b/lib/plugins/aws/configCredentials/awsConfigCredentials.js @@ -57,51 +57,53 @@ class AwsConfigCredentials { fse.ensureFileSync(this.credentialsFilePath); this.hooks = { - 'config:credentials:config': () => BbPromise.bind(this).then(this.configureCredentials), + 'config:credentials:config': () => this.configureCredentials(), }; } configureCredentials() { - // sanitize - this.options.provider = this.options.provider.toLowerCase(); - this.options.profile = this.options.profile ? this.options.profile : 'default'; + return BbPromise.try(() => { + // sanitize + this.options.provider = this.options.provider.toLowerCase(); + this.options.profile = this.options.profile ? this.options.profile : 'default'; - // resolve if provider option is not 'aws' - if (this.options.provider !== 'aws') { - return BbPromise.resolve(); - } + // resolve if provider option is not 'aws' + if (this.options.provider !== 'aws') return null; - // validate - if (!this.options.key || !this.options.secret) { - throw new this.serverless.classes.Error('Please include --key and --secret options for AWS.'); - } - - this.serverless.cli.log('Setting up AWS...'); - - this.credentials = this.getCredentials(); - - // Get the profile start line and end line numbers inside the credentials array - const profileBoundaries = this.getProfileBoundaries(); - - // Check if the profile exists - const isNewProfile = profileBoundaries.start === -1; - if (isNewProfile) { - this.addProfile(); - } else { - // Only update the profile if the overwrite flag was set - if (!this.options.overwrite) { - const message = [ - `Failed! ~/.aws/credentials already has a "${this.options.profile}" profile.`, - ' Use the overwrite flag ("-o" or "--overwrite") to force the update', - ].join(''); - this.serverless.cli.log(message); - return BbPromise.resolve(); + // validate + if (!this.options.key || !this.options.secret) { + throw new this.serverless.classes.Error( + 'Please include --key and --secret options for AWS.' + ); } - this.updateProfile(profileBoundaries); - } + this.serverless.cli.log('Setting up AWS...'); - return this.saveCredentialsFile(); + this.credentials = this.getCredentials(); + + // Get the profile start line and end line numbers inside the credentials array + const profileBoundaries = this.getProfileBoundaries(); + + // Check if the profile exists + const isNewProfile = profileBoundaries.start === -1; + if (isNewProfile) { + this.addProfile(); + } else { + // Only update the profile if the overwrite flag was set + if (!this.options.overwrite) { + const message = [ + `Failed! ~/.aws/credentials already has a "${this.options.profile}" profile.`, + ' Use the overwrite flag ("-o" or "--overwrite") to force the update', + ].join(''); + this.serverless.cli.log(message); + return null; + } + + this.updateProfile(profileBoundaries); + } + + return this.saveCredentialsFile(); + }); } getCredentials() { diff --git a/lib/plugins/aws/configCredentials/awsConfigCredentials.test.js b/lib/plugins/aws/configCredentials/awsConfigCredentials.test.js index da6d095a8..0628e3c01 100644 --- a/lib/plugins/aws/configCredentials/awsConfigCredentials.test.js +++ b/lib/plugins/aws/configCredentials/awsConfigCredentials.test.js @@ -121,8 +121,13 @@ describe('AwsConfigCredentials', () => { it('should throw an error if the "key" and "secret" options are not given', () => { awsConfigCredentials.options.key = false; awsConfigCredentials.options.secret = false; - - expect(() => awsConfigCredentials.configureCredentials()).to.throw(Error); + return awsConfigCredentials.configureCredentials().then( + () => { + throw new Error('Unexpected'); + }, + error => + expect(error.message).to.include('Please include --key and --secret options for AWS') + ); }); it('should not update the profile if the overwrite flag is not set', () => {