Skip to content

Commit

Permalink
Add initial support for waiter API transformation (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Feb 21, 2023
1 parent 1dc8390 commit a04403b
Show file tree
Hide file tree
Showing 27 changed files with 311 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/rotten-buckets-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Add initial support for waiter API transformation
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS = require("aws-sdk");

const Bucket = "BUCKET_NAME";
const client = new AWS.S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AWS_S3 = require("@aws-sdk/client-s3");

const {
S3,
waitUntilBucketExists
} = AWS_S3;

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

const Bucket = "BUCKET_NAME";
const client = new AWS.S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const S3 = require("aws-sdk").S3;

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {
S3,
waitUntilBucketExists
} = require("@aws-sdk/client-s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const AWS = require("aws-sdk");

const Bucket = "BUCKET_NAME";
const client = new AWS.S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {
S3,
waitUntilBucketExists
} = require("@aws-sdk/client-s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import S3 from "aws-sdk/clients/s3";

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import S3 = require("aws-sdk/clients/s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AWS_S3 = require("@aws-sdk/client-s3");

const {
S3,
waitUntilBucketExists
} = AWS_S3;

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { S3 } from "aws-sdk";

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const S3 = require("aws-sdk/clients/s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {
S3,
waitUntilBucketExists
} = require("@aws-sdk/client-s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { S3 } = require("aws-sdk");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket }).promise();
await client.waitFor("bucketExists", { Bucket }).promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const {
S3,
waitUntilBucketExists
} = require("@aws-sdk/client-s3");

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });

await client.createBucket({ Bucket });
await waitUntilBucketExists({
client,
maxWaitTime: 180
}, { Bucket });
37 changes: 37 additions & 0 deletions src/transforms/v2-to-v3/apis/getClientWaiterStates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";

export interface GetClientWaiterStatesOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

export const getClientWaiterStates = (
j: JSCodeshift,
source: Collection<unknown>,
options: GetClientWaiterStatesOptions
): string[] => {
const waiterStates: string[] = [];

const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);

for (const v2ClientId of v2ClientIdentifiers) {
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: v2ClientId,
property: { type: "Identifier", name: "waitFor" },
},
})
.forEach((waiterCallExpression) => {
// @ts-expect-error arguments[0] is Literal or StringLiteral
const waiterState = waiterCallExpression.value.arguments[0].value;
waiterStates.push(waiterState);
});
}

return waiterStates;
};
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/apis/getV3ClientWaiterApiName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getV3ClientWaiterApiName = (waiterState: string): string =>
`waitUntil${waiterState[0].toUpperCase()}${waiterState.slice(1)}`;
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from "./getClientWaiterStates";
export * from "./getV3ClientWaiterApiName";
export * from "./removePromiseCalls";
export * from "./replaceWaiterApi";
54 changes: 54 additions & 0 deletions src/transforms/v2-to-v3/apis/replaceWaiterApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientWaiterStates } from "./getClientWaiterStates";
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName";

export interface ReplaceWaiterApiOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

// Updates .waitFor() API with waitUntil* API.
export const replaceWaiterApi = (
j: JSCodeshift,
source: Collection<unknown>,
options: ReplaceWaiterApiOptions
): void => {
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);

for (const v2ClientId of v2ClientIdentifiers) {
const waiterStates = getClientWaiterStates(j, source, options);

for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
source
.find(j.CallExpression, {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: v2ClientId,
property: { type: "Identifier", name: "waitFor" },
},
})
.replaceWith((callExpression) => {
return j.callExpression(j.identifier(v3WaiterApiName), [
j.objectExpression([
j.objectProperty.from({
key: j.identifier("client"),
value: v2ClientId,
shorthand: true,
}),
// ToDo: Read maxWaitTime from the waiter configuration
j.objectProperty.from({
key: j.identifier("maxWaitTime"),
value: j.numericLiteral(180),
}),
]),
callExpression.node.arguments[1],
]);
});
}
}
};
11 changes: 11 additions & 0 deletions src/transforms/v2-to-v3/modules/addV3ClientImportEquals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientWaiterStates, getV3ClientWaiterApiName } from "../apis";
import { getV3ClientTypesCount } from "../ts-type";
import { addV3ClientDefaultImportEquals } from "./addV3ClientDefaultImportEquals";
import { addV3ClientNamedImportEquals } from "./addV3ClientNamedImportEquals";
Expand All @@ -15,6 +16,7 @@ export const addV3ClientImportEquals = (
const v3ClientTypesCount = getV3ClientTypesCount(j, source, options);
const newExpressionCount = getNewExpressionCount(j, source, options);
const clientTSTypeRefCount = getClientTSTypeRefCount(j, source, options);
const waiterStates = getClientWaiterStates(j, source, options);

if (v3ClientTypesCount > 0) {
addV3ClientDefaultImportEquals(j, source, options);
Expand All @@ -27,4 +29,13 @@ export const addV3ClientImportEquals = (
valueName: options.v2ClientLocalName,
});
}

for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
addV3ClientNamedImportEquals(j, source, {
...options,
keyName: v3WaiterApiName,
valueName: v3WaiterApiName,
});
}
};
11 changes: 11 additions & 0 deletions src/transforms/v2-to-v3/modules/addV3ClientImports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientWaiterStates, getV3ClientWaiterApiName } from "../apis";
import { getV3ClientTypesCount } from "../ts-type";
import { addV3ClientDefaultImport } from "./addV3ClientDefaultImport";
import { addV3ClientNamedImport } from "./addV3ClientNamedImport";
Expand All @@ -15,6 +16,7 @@ export const addV3ClientImports = (
const v3ClientTypesCount = getV3ClientTypesCount(j, source, options);
const newExpressionCount = getNewExpressionCount(j, source, options);
const clientTSTypeRefCount = getClientTSTypeRefCount(j, source, options);
const waiterStates = getClientWaiterStates(j, source, options);

// Add default import for types, if needed.
if (v3ClientTypesCount > 0) {
Expand All @@ -28,4 +30,13 @@ export const addV3ClientImports = (
localName: options.v2ClientLocalName,
});
}

for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
addV3ClientNamedImport(j, source, {
...options,
importedName: v3WaiterApiName,
localName: v3WaiterApiName,
});
}
};
Loading

0 comments on commit a04403b

Please sign in to comment.