Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic utility addNamedModule #630

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};