Skip to content

Commit

Permalink
refactor(CLI): Modern logs for remove command
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Oct 4, 2021
1 parent 2af95c0 commit 3934cad
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/lib/monitorStack.js
Expand Up @@ -110,7 +110,7 @@ module.exports = {
const operationPhrase = (() => {
if (action === 'create') return 'Creating';
if (action === 'update') return 'Updating';
if (action === 'delete') return 'Deleting';
if (action === 'delete') return 'Removing';
return '';
})();

Expand Down
27 changes: 27 additions & 0 deletions lib/plugins/aws/remove/index.js
Expand Up @@ -6,6 +6,9 @@ const emptyS3Bucket = require('./lib/bucket');
const removeStack = require('./lib/stack');
const removeEcrRepository = require('./lib/ecr');
const checkIfEcrRepositoryExists = require('../lib/checkIfEcrRepositoryExists');
const { log, style, progress } = require('@serverless/utils/log');

const mainProgress = progress.get('main');

class AwsRemove {
constructor(serverless, options) {
Expand All @@ -24,16 +27,40 @@ class AwsRemove {
);

this.hooks = {
'initialize': async () => {
if (this.serverless.processedInput.commands.join(' ') === 'remove') {
log.notice(
`Removing ${this.serverless.service.service} from stage ${this.serverless
.getProvider('aws')
.getStage()} ${style.aside(`(${this.serverless.getProvider('aws').getRegion()})`)}`
);
}
log.info(); // Ensure gap between verbose logging
},
'remove:remove': async () => {
const doesEcrRepositoryExistPromise = this.checkIfEcrRepositoryExists();
await this.validate();
mainProgress.notice('Removing objects from S3 bucket', { isMainEvent: true });
await this.emptyS3Bucket();
mainProgress.notice('Removing CloudFormation stack', { isMainEvent: true });
const cfData = await this.removeStack();
await this.monitorStack('delete', cfData);
if (await doesEcrRepositoryExistPromise) {
mainProgress.notice('Removing ECR repository', { isMainEvent: true });
await this.removeEcrRepository();
}
},
'finalize': () => {
if (this.serverless.processedInput.commands.join(' ') !== 'remove') return;
log.notice();
log.notice.success(
`Service ${this.serverless.service.service} has been successfully removed ${style.aside(
`(${Math.floor(
(Date.now() - this.serverless.pluginManager.commandRunStartTime) / 1000
)}s)`
)}`
);
},
};
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/plugins/aws/remove/lib/bucket.js
@@ -1,6 +1,7 @@
'use strict';

const BbPromise = require('bluebird');
const { legacy } = require('@serverless/utils/log');

module.exports = {
async setServerlessDeploymentBucketName() {
Expand All @@ -12,7 +13,7 @@ module.exports = {
async listObjects() {
this.objectsInBucket = [];

this.serverless.cli.log('Getting all objects in S3 bucket...');
legacy.log('Getting all objects in S3 bucket...');
const serviceStage = `${this.serverless.service.service}/${this.provider.getStage()}`;

return this.provider
Expand All @@ -33,7 +34,7 @@ module.exports = {
},

async deleteObjects() {
this.serverless.cli.log('Removing objects in S3 bucket...');
legacy.log('Removing objects in S3 bucket...');
if (this.objectsInBucket.length) {
return this.provider.request('S3', 'deleteObjects', {
Bucket: this.bucketName,
Expand Down
4 changes: 3 additions & 1 deletion lib/plugins/aws/remove/lib/ecr.js
@@ -1,8 +1,10 @@
'use strict';

const { legacy } = require('@serverless/utils/log');

module.exports = {
async removeEcrRepository() {
this.serverless.cli.log('Removing ECR repository...');
legacy.log('Removing ECR repository...');
const registryId = await this.provider.getAccountId();
const repositoryName = this.provider.naming.getEcrRepositoryName();
const params = {
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/aws/remove/lib/stack.js
@@ -1,10 +1,11 @@
'use strict';

const BbPromise = require('bluebird');
const { legacy } = require('@serverless/utils/log');

module.exports = {
async remove() {
this.serverless.cli.log('Removing Stack...');
legacy.log('Removing Stack...');
const stackName = this.provider.naming.getStackName();
const params = {
StackName: stackName,
Expand Down

0 comments on commit 3934cad

Please sign in to comment.