Skip to content

Commit 577fc33

Browse files
authored
Add generic utility addNamedModule (#630)
1 parent c4ee840 commit 577fc33

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/transforms/v2-to-v3/modules/addClientModules.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ import {
1616
S3,
1717
} from "../config";
1818
import { getV3ClientTypes } from "../ts-type";
19+
import { addNamedModule } from "./addNamedModule";
1920
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
2021
import { getNewExpressionCount } from "./getNewExpressionCount";
2122

22-
import * as importEqualsModule from "./importEqualsModule";
23-
import * as importModule from "./importModule";
24-
import * as requireModule from "./requireModule";
25-
import { ClientModulesOptions, ImportType } from "./types";
23+
import { ClientModulesOptions } from "./types";
2624

2725
export const addClientModules = (
2826
j: JSCodeshift,
@@ -38,13 +36,6 @@ export const addClientModules = (
3836
importType,
3937
} = options;
4038

41-
const { addNamedModule } =
42-
importType === ImportType.REQUIRE
43-
? requireModule
44-
: importType === ImportType.IMPORT_EQUALS
45-
? importEqualsModule
46-
: importModule;
47-
4839
const v3ClientTypes = getV3ClientTypes(j, source, options);
4940
const newExpressionCount = getNewExpressionCount(j, source, options);
5041
const clientTSTypeRefCount = getClientTSTypeRefCount(j, source, options);
@@ -53,13 +44,15 @@ export const addClientModules = (
5344
// Add named import for types, if needed.
5445
for (const v3ClientType of v3ClientTypes) {
5546
addNamedModule(j, source, {
47+
importType,
5648
importedName: v3ClientType,
5749
packageName: v3ClientPackageName,
5850
});
5951
}
6052

6153
if (newExpressionCount > 0 || clientTSTypeRefCount > 0) {
6254
addNamedModule(j, source, {
55+
importType,
6356
importedName: v3ClientName,
6457
localName: v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName,
6558
packageName: v3ClientPackageName,
@@ -69,6 +62,7 @@ export const addClientModules = (
6962
for (const waiterState of waiterStates) {
7063
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
7164
addNamedModule(j, source, {
65+
importType,
7266
importedName: v3WaiterApiName,
7367
packageName: v3ClientPackageName,
7468
});
@@ -77,18 +71,21 @@ export const addClientModules = (
7771
if (v2ClientName === S3) {
7872
if (isS3UploadApiUsed(j, source, clientIdentifiers)) {
7973
addNamedModule(j, source, {
74+
importType,
8075
importedName: "Upload",
8176
packageName: "@aws-sdk/lib-storage",
8277
});
8378
}
8479

8580
if (isS3GetSignedUrlApiUsed(j, source, clientIdentifiers)) {
8681
addNamedModule(j, source, {
82+
importType,
8783
importedName: "getSignedUrl",
8884
packageName: "@aws-sdk/s3-request-presigner",
8985
});
9086
for (const apiName of getS3SignedUrlApiNames(j, source, clientIdentifiers)) {
9187
addNamedModule(j, source, {
88+
importType,
9289
importedName: getCommandName(apiName),
9390
packageName: v3ClientPackageName,
9491
});
@@ -113,13 +110,15 @@ export const addClientModules = (
113110
// Add named import for types, if needed.
114111
for (const docClientType of docClientTypes) {
115112
addNamedModule(j, source, {
113+
importType,
116114
importedName: docClientType,
117115
packageName: "@aws-sdk/lib-dynamodb",
118116
});
119117
}
120118

121119
if (docClientNewExpressionCount > 0) {
122120
addNamedModule(j, source, {
121+
importType,
123122
importedName: DYNAMODB_DOCUMENT,
124123
packageName: "@aws-sdk/lib-dynamodb",
125124
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import * as importEqualsModule from "./importEqualsModule";
4+
import * as importModule from "./importModule";
5+
import * as requireModule from "./requireModule";
6+
import { ImportType, ModulesOptions } from "./types";
7+
8+
export const addNamedModule = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
options: ModulesOptions & { importType: ImportType }
12+
) => {
13+
const { importType, ...addNamedModuleOptions } = options;
14+
15+
const { addNamedModule } =
16+
importType === ImportType.REQUIRE
17+
? requireModule
18+
: importType === ImportType.IMPORT_EQUALS
19+
? importEqualsModule
20+
: importModule;
21+
22+
addNamedModule(j, source, addNamedModuleOptions);
23+
};

0 commit comments

Comments
 (0)