From c37476fb70c70e65eb6abf24c210f0a6fc1f612f Mon Sep 17 00:00:00 2001 From: Mario Doiron <5252025+doiron@users.noreply.github.com> Date: Wed, 17 May 2023 14:57:44 -0700 Subject: [PATCH] chore: fixed failing cleanup scripts and converted them to ts --- package.json | 2 +- scripts/{ask-clean-up.js => ask-clean-up.ts} | 35 ++++++------ scripts/aws-clean-up.js | 59 -------------------- scripts/aws-clean-up.ts | 57 +++++++++++++++++++ 4 files changed, 74 insertions(+), 79 deletions(-) rename scripts/{ask-clean-up.js => ask-clean-up.ts} (53%) delete mode 100644 scripts/aws-clean-up.js create mode 100644 scripts/aws-clean-up.ts diff --git a/package.json b/package.json index 1d0c8a6d..63e49b38 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "pre-test": "npm run build && chmod +x ./dist/bin/ask.js", "integration-test": "npm run pre-test && mocha --parallel -t 180000 test/integration/run-test.js", "functional-test": "npm run pre-test && mocha -t 600000 -u bdd --require ts-node/register test/functional/run-test.js -R spec 'test/functional/{,**/}!(*.d).{ts,js}'", - "functional-test:clean-up": "node scripts/aws-clean-up.js; node scripts/ask-clean-up.js", + "functional-test:clean-up": "npm run build && tsc scripts/*.ts && node scripts/aws-clean-up.js; node scripts/ask-clean-up.js", "format": "prettier --write .", "pre-release": "standard-version", "prepare": "husky install", diff --git a/scripts/ask-clean-up.js b/scripts/ask-clean-up.ts similarity index 53% rename from scripts/ask-clean-up.js rename to scripts/ask-clean-up.ts index ede9baea..e52810a6 100644 --- a/scripts/ask-clean-up.js +++ b/scripts/ask-clean-up.ts @@ -1,34 +1,31 @@ /* eslint-disable no-await-in-loop */ -const { CustomSmapiClientBuilder } = require('ask-smapi-sdk'); -const AppConfig = require('../lib/model/app-config'); -const AuthorizationController = require('../lib/controllers/authorization-controller'); -const CONSTANTS = require('../lib/utils/constants'); -const DynamicConfig = require('../lib/utils/dynamic-config'); -const profileHelper = require("../lib/utils/profile-helper"); +import { CustomSmapiClientBuilder } from 'ask-smapi-sdk'; +import { PLACEHOLDER } from '../dist/lib/utils/constants'; +import { isEnvProfile, resolveVendorId } from "../dist/lib/utils/profile-helper"; -new AppConfig(); - -const authorizationController = new AuthorizationController({ +const appConfig = new (require('../lib/model/app-config'))(); +const authorizationController = new (require('../dist/lib/controllers/authorization-controller'))({ auth_client_type: 'LWA' }); +const dynamicConfig = require('../dist/lib/utils/dynamic-config'); const profile = _findEnvProfile() || process.env.ASK_DEFAULT_PROFILE || "default";; function _findEnvProfile() { - if (profileHelper.isEnvProfile()) { + if (isEnvProfile()) { // Only when user set every required parameter in ENV, we will treat profile as ENV - return CONSTANTS.PLACEHOLDER.ENVIRONMENT_VAR.PROFILE_NAME; + return PLACEHOLDER.ENVIRONMENT_VAR.PROFILE_NAME; } return null; } const refreshTokenConfig = { - clientId: authorizationController.oauthClient.config.clientId, - clientSecret: authorizationController.oauthClient.config.clientConfirmation, - refreshToken: AppConfig.getInstance().getToken(profile).refresh_token + clientId: authorizationController.oauthClient?.config.clientId, + clientSecret: authorizationController.oauthClient?.config.clientConfirmation, + refreshToken: appConfig.getToken(profile).refresh_token }; -const authEndpoint = DynamicConfig.lwaTokenHost; -const smapiEndpoint = DynamicConfig.smapiBaseUrl; +const authEndpoint = dynamicConfig.lwaTokenHost; +const smapiEndpoint = dynamicConfig.smapiBaseUrl; const cleanUp = async () => { console.log('cleaning ask resources'); @@ -39,11 +36,11 @@ const cleanUp = async () => { .client(); let nextToken; - const skills = []; + const skills: any = []; do { - vendorId = profileHelper.resolveVendorId(profile); + const vendorId = resolveVendorId(profile); const res = await client.listSkillsForVendorV1(vendorId, nextToken); - skills.push(...res.skills); + Array.prototype.push.apply(skills, res.skills); nextToken = res.nextToken; } while (nextToken); diff --git a/scripts/aws-clean-up.js b/scripts/aws-clean-up.js deleted file mode 100644 index b47ca66e..00000000 --- a/scripts/aws-clean-up.js +++ /dev/null @@ -1,59 +0,0 @@ -const { S3Client, DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectVersionsCommand } = require('@aws-sdk/client-s3'); -const { CloudFormationClient, DeleteStackCommand, ListStacksCommand } = require('@aws-sdk/client-cloudformation'); -const { LambdaClient, DeleteFunctionCommand, ListFunctionsCommand } = require('@aws-sdk/client-lambda'); - -const region = 'us-east-1'; - -const s3 = new S3Client({ region }); -const cf = new CloudFormationClient({ region }); -const lambda = new LambdaClient({ region }); - -const prefix = 'ask-'; - -const deleteBucket = async (name) => { - const versions = await s3.send(new ListObjectVersionsCommand({ Bucket: name })); - - let deletePromises = versions.Versions.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))); - await Promise.all(deletePromises); - - deletePromises = versions.DeleteMarkers.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))); - await Promise.all(deletePromises); - - return s3.send(new DeleteBucketCommand({ Bucket: name })); -}; - -const cleanUp = async () => { - console.log(`cleaning aws resources with prefix "${prefix}"`); - - const stackResults = await cf.send(new ListStacksCommand()).then(res => { - const Stacks = res.StackSummaries - .filter(s => s.StackName.startsWith(prefix) && !s.StackStatus.includes('DELETE_COMPLETE')); - console.log('Stacks #:', Stacks.length); - const deletePromises = Stacks.map(i => cf.send(new DeleteStackCommand({ StackName: i.StackName }))); - return Promise.allSettled(deletePromises); - }); - - const functionResults = await lambda.send(new ListFunctionsCommand()).then(res => { - const Functions = res.Functions.filter(i => i.FunctionName.startsWith(prefix)); - console.log('Functions #:', Functions.length); - const deletePromises = Functions.map(i => lambda.send(new DeleteFunctionCommand({ FunctionName: i.FunctionName }))); - return Promise.allSettled(deletePromises); - }); - - const bucketResults = await s3.send(new ListBucketsCommand()).then(async (res) => { - const Buckets = res.Buckets.filter(b => b.Name.startsWith(prefix)); - console.log('Buckets #:', Buckets.length); - const deletePromises = Buckets.map((i) => deleteBucket(i.Name)); - return Promise.allSettled(deletePromises); - }); - - const rejected = [...stackResults, ...functionResults, ...bucketResults].filter(r => r.status === 'rejected'); - if (rejected.length) { - rejected.forEach(r => { - console.error(r.reason); - }); - } - console.log('done'); -}; - -cleanUp(); diff --git a/scripts/aws-clean-up.ts b/scripts/aws-clean-up.ts new file mode 100644 index 00000000..08e89528 --- /dev/null +++ b/scripts/aws-clean-up.ts @@ -0,0 +1,57 @@ +import { S3Client, DeleteBucketCommand, DeleteObjectCommand, ListBucketsCommand, ListObjectVersionsCommand } from '@aws-sdk/client-s3'; +import { CloudFormationClient, DeleteStackCommand, ListStacksCommand, StackSummary } from '@aws-sdk/client-cloudformation'; +import { LambdaClient, DeleteFunctionCommand, ListFunctionsCommand } from '@aws-sdk/client-lambda'; + +const region = 'us-east-1'; +const s3 = new S3Client({ region }); +const cf = new CloudFormationClient({ region }); +const lambda = new LambdaClient({ region }); +const prefix = 'ask-'; + +const deleteBucket = async (name) => { + const versions = await s3.send(new ListObjectVersionsCommand({ Bucket: name })); + + if (versions.Versions) { + const deletePromises = versions.Versions.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))) || []; + await Promise.all(deletePromises); + const deleteMarkersPromises = versions.DeleteMarkers?.map(i => s3.send(new DeleteObjectCommand({ Bucket: name, Key: i.Key, VersionId: i.VersionId }))) || []; + await Promise.all(deleteMarkersPromises); + } + + return s3.send(new DeleteBucketCommand({ Bucket: name })); +}; + +export const cleanUp = async () => { + console.log(`cleaning aws resources with prefix "${prefix}"`); + + const stackResults = await cf.send(new ListStacksCommand({})).then(res => { + const stacks: StackSummary[] = res.StackSummaries?.filter(s => s.StackName?.startsWith(prefix) && !s.StackStatus?.includes('DELETE_COMPLETE')) || []; + console.log('Stacks #:', stacks.length); + const deletePromises = stacks.map(i => cf.send(new DeleteStackCommand({ StackName: i.StackName }))); + return Promise.allSettled(deletePromises); + }); + + const functionResults = await lambda.send(new ListFunctionsCommand({})).then(res => { + const Functions = res.Functions?.filter(i => i.FunctionName?.startsWith(prefix)) || []; + console.log('Functions #:', Functions.length); + const deletePromises = Functions.map(i => lambda.send(new DeleteFunctionCommand({ FunctionName: i.FunctionName }))); + return Promise.allSettled(deletePromises); + }); + + const bucketResults = await s3.send(new ListBucketsCommand({})).then(async (res) => { + const Buckets = res.Buckets?.filter(b => b.Name?.startsWith(prefix)) || []; + console.log('Buckets #:', Buckets.length); + const deletePromises = Buckets.map((i) => deleteBucket(i.Name)); + return Promise.allSettled(deletePromises); + }); + + const rejected = [...stackResults, ...functionResults, ...bucketResults].filter(r => r.status === 'rejected'); + if (rejected.length) { + rejected.forEach(r => { + console.error(JSON.stringify(r, null, 2)); + }); + } + console.log('done'); +}; + +cleanUp();