Skip to content

Commit

Permalink
Add generic utility addNamedModule (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Oct 20, 2023
1 parent c4ee840 commit 577fc33
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/transforms/v2-to-v3/modules/addClientModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ import {
S3,
} from "../config";
import { getV3ClientTypes } from "../ts-type";
import { addNamedModule } from "./addNamedModule";
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
import { getNewExpressionCount } from "./getNewExpressionCount";

import * as importEqualsModule from "./importEqualsModule";
import * as importModule from "./importModule";
import * as requireModule from "./requireModule";
import { ClientModulesOptions, ImportType } from "./types";
import { ClientModulesOptions } from "./types";

export const addClientModules = (
j: JSCodeshift,
Expand All @@ -38,13 +36,6 @@ export const addClientModules = (
importType,
} = options;

const { addNamedModule } =
importType === ImportType.REQUIRE
? requireModule
: importType === ImportType.IMPORT_EQUALS
? importEqualsModule
: importModule;

const v3ClientTypes = getV3ClientTypes(j, source, options);
const newExpressionCount = getNewExpressionCount(j, source, options);
const clientTSTypeRefCount = getClientTSTypeRefCount(j, source, options);
Expand All @@ -53,13 +44,15 @@ export const addClientModules = (
// Add named import for types, if needed.
for (const v3ClientType of v3ClientTypes) {
addNamedModule(j, source, {
importType,
importedName: v3ClientType,
packageName: v3ClientPackageName,
});
}

if (newExpressionCount > 0 || clientTSTypeRefCount > 0) {
addNamedModule(j, source, {
importType,
importedName: v3ClientName,
localName: v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName,
packageName: v3ClientPackageName,
Expand All @@ -69,6 +62,7 @@ export const addClientModules = (
for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
addNamedModule(j, source, {
importType,
importedName: v3WaiterApiName,
packageName: v3ClientPackageName,
});
Expand All @@ -77,18 +71,21 @@ export const addClientModules = (
if (v2ClientName === S3) {
if (isS3UploadApiUsed(j, source, clientIdentifiers)) {
addNamedModule(j, source, {
importType,
importedName: "Upload",
packageName: "@aws-sdk/lib-storage",
});
}

if (isS3GetSignedUrlApiUsed(j, source, clientIdentifiers)) {
addNamedModule(j, source, {
importType,
importedName: "getSignedUrl",
packageName: "@aws-sdk/s3-request-presigner",
});
for (const apiName of getS3SignedUrlApiNames(j, source, clientIdentifiers)) {
addNamedModule(j, source, {
importType,
importedName: getCommandName(apiName),
packageName: v3ClientPackageName,
});
Expand All @@ -113,13 +110,15 @@ export const addClientModules = (
// Add named import for types, if needed.
for (const docClientType of docClientTypes) {
addNamedModule(j, source, {
importType,
importedName: docClientType,
packageName: "@aws-sdk/lib-dynamodb",
});
}

if (docClientNewExpressionCount > 0) {
addNamedModule(j, source, {
importType,
importedName: DYNAMODB_DOCUMENT,
packageName: "@aws-sdk/lib-dynamodb",
});
Expand Down
23 changes: 23 additions & 0 deletions src/transforms/v2-to-v3/modules/addNamedModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Collection, JSCodeshift } from "jscodeshift";

import * as importEqualsModule from "./importEqualsModule";
import * as importModule from "./importModule";
import * as requireModule from "./requireModule";
import { ImportType, ModulesOptions } from "./types";

export const addNamedModule = (
j: JSCodeshift,
source: Collection<unknown>,
options: ModulesOptions & { importType: ImportType }
) => {
const { importType, ...addNamedModuleOptions } = options;

const { addNamedModule } =
importType === ImportType.REQUIRE
? requireModule
: importType === ImportType.IMPORT_EQUALS
? importEqualsModule
: importModule;

addNamedModule(j, source, addNamedModuleOptions);
};

0 comments on commit 577fc33

Please sign in to comment.