|
1 | | -import { Collection, Identifier, JSCodeshift } from "jscodeshift"; |
| 1 | +import { Collection, JSCodeshift } from "jscodeshift"; |
2 | 2 |
|
3 | | -import { CLIENT_NAMES } from "./config"; |
| 3 | +import { containsRequire } from "./containsRequire"; |
| 4 | +import { getV2ClientImportNames } from "./getV2ClientImportNames"; |
| 5 | +import { getV2ClientRequireNames } from "./getV2ClientRequireNames"; |
4 | 6 |
|
5 | | -export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => { |
6 | | - const v2ClientModuleNames = []; |
7 | | - |
8 | | - for (const clientName of CLIENT_NAMES) { |
9 | | - // Add specifier name to v2ClientImportNames if it is imported in the source. |
10 | | - source |
11 | | - .find(j.ImportDeclaration, { |
12 | | - source: { value: `aws-sdk/clients/${clientName.toLowerCase()}` }, |
13 | | - }) |
14 | | - .forEach((declerationPath) => { |
15 | | - declerationPath.value.specifiers.forEach((specifier) => { |
16 | | - if ( |
17 | | - specifier.type === "ImportDefaultSpecifier" || |
18 | | - specifier.type === "ImportNamespaceSpecifier" |
19 | | - ) { |
20 | | - v2ClientModuleNames.push(specifier.local.name); |
21 | | - } |
22 | | - }); |
23 | | - }); |
24 | | - |
25 | | - // Add specifier name to v2ClientImportNames if it is required in the source. |
26 | | - source |
27 | | - .find(j.VariableDeclarator, { |
28 | | - id: { type: "Identifier" }, |
29 | | - init: { |
30 | | - type: "CallExpression", |
31 | | - callee: { type: "Identifier", name: "require" }, |
32 | | - arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }], |
33 | | - }, |
34 | | - }) |
35 | | - .forEach((declerationPath) => { |
36 | | - v2ClientModuleNames.push((declerationPath.value.id as Identifier).name); |
37 | | - }); |
38 | | - } |
39 | | - |
40 | | - return v2ClientModuleNames; |
41 | | -}; |
| 7 | +export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => |
| 8 | + containsRequire(j, source) |
| 9 | + ? getV2ClientRequireNames(j, source) |
| 10 | + : getV2ClientImportNames(j, source); |
0 commit comments