|
1 | 1 | import { API, FileInfo } from "jscodeshift"; |
| 2 | +import findImports from "jscodeshift-find-imports"; |
| 3 | + |
| 4 | +import { addV3ClientImport, getV2ClientNames, replaceClientCreation } from "./helpers"; |
| 5 | +import { getV3ClientName, getV3ClientPackageName } from "./utils"; |
2 | 6 |
|
3 | 7 | export default function transformer(file: FileInfo, api: API) { |
4 | 8 | const j = api.jscodeshift; |
5 | | - const root = j(file.source); |
| 9 | + const { statement } = j.template; |
| 10 | + const source = j(file.source); |
| 11 | + |
| 12 | + const imports = findImports(source, statement`import AWS from 'aws-sdk'`); |
| 13 | + for (const importObj of Object.values(imports)) { |
| 14 | + if (importObj.type === "Identifier") { |
| 15 | + const v2ClientNames = getV2ClientNames(j, source, importObj); |
6 | 16 |
|
7 | | - // transform `root` here |
| 17 | + for (const v2ClientName of v2ClientNames) { |
| 18 | + const v3ClientName = getV3ClientName(v2ClientName); |
| 19 | + const v3ClientPackageName = getV3ClientPackageName(v2ClientName); |
| 20 | + addV3ClientImport(j, source, { v3ClientName, v3ClientPackageName }); |
| 21 | + replaceClientCreation(j, source, { importObj, v2ClientName, v3ClientName }); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
8 | 25 |
|
9 | | - return root.toSource(); |
| 26 | + return source.toSource(); |
10 | 27 | } |
0 commit comments