Skip to content

Commit

Permalink
Compute variable name for import equals from v2ClientName (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 1, 2023
1 parent 962e179 commit 06a92c7
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-pears-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Compute variable name for import equals from v2ClientName
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CLIENTS_TO_TEST } from "./config";
import { getClientDeepImportPath } from "./getClientDeepImportPath";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";
import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode";

export const getServiceImportEqualsWithNameInput = (codegenComment: string) => {
let content = `${codegenComment}\n`;

for (const clientName of CLIENTS_TO_TEST) {
const importName = getClientNameWithLocalSuffix(clientName);
content += `import ${importName} = require("${getClientDeepImportPath(clientName)}");\n`;
}
content += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix));

return content;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CLIENTS_TO_TEST } from "./config";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode";

export const getServiceImportEqualsWithNameOutput = (codegenComment: string) => {
let content = `${codegenComment};\n`;

content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST, { useLocalSuffix: true });
content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix));

return content;
};
17 changes: 15 additions & 2 deletions scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@ import {
CLIENT_PACKAGE_NAMES_MAP,
} from "../../src/transforms/v2-to-v3/config";
import { getV3DefaultLocalName } from "../../src/transforms/v2-to-v3/utils";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";

export const getV3PackageImportEqualsCode = (clientsToTest: typeof CLIENT_NAMES) => {
export interface V3PackageImportEqualsCodeOptions {
useLocalSuffix?: boolean;
}

export const getV3PackageImportEqualsCode = (
clientsToTest: typeof CLIENT_NAMES,
options?: V3PackageImportEqualsCodeOptions
) => {
let content = ``;
const { useLocalSuffix = false } = options || {};

for (const v2ClientName of clientsToTest) {
const v3ClientDefaultLocalName = getV3DefaultLocalName(v2ClientName);
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`;
content += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n\n`;

const v3ClientName = CLIENT_NAMES_MAP[v2ClientName];
const v2ClientLocalName = useLocalSuffix
? getClientNameWithLocalSuffix(v2ClientName)
: v2ClientName;

const v3ObjectPattern =
v3ClientName === v2ClientName ? v3ClientName : `${v3ClientName}: ${v2ClientName}`;
v3ClientName === v2ClientLocalName ? v3ClientName : `${v3ClientName}: ${v2ClientLocalName}`;
content +=
`const {\n` + ` ${v3ObjectPattern}\n` + `} = ${getV3DefaultLocalName(v2ClientName)};\n\n`;
}
Expand Down
4 changes: 4 additions & 0 deletions scripts/generateNewClientTests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { getServiceImportDeepWithNameInput } from "./getServiceImportDeepWithNam
import { getServiceImportDeepWithNameOutput } from "./getServiceImportDeepWithNameOutput";
import { getServiceImportEqualsInput } from "./getServiceImportEqualsInput";
import { getServiceImportEqualsOutput } from "./getServiceImportEqualsOutput";
import { getServiceImportEqualsWithNameInput } from "./getServiceImportEqualsWithNameInput";
import { getServiceImportEqualsWithNameOutput } from "./getServiceImportEqualsWithNameOutput";
import { getServiceImportInput } from "./getServiceImportInput";
import { getServiceImportOutput } from "./getServiceImportOutput";
import { getServiceImportWithNameInput } from "./getServiceImportWithNameInput";
Expand Down Expand Up @@ -64,6 +66,8 @@ const newClientTestsPath = join(__dirname, "..", "..", newClientsTestsFolder);
["service-import.output.js", getServiceImportOutput],
["service-import-equals.input.ts", getServiceImportEqualsInput],
["service-import-equals.output.ts", getServiceImportEqualsOutput],
["service-import-equals-with-name.input.ts", getServiceImportEqualsWithNameInput],
["service-import-equals-with-name.output.ts", getServiceImportEqualsWithNameOutput],
["service-import-deep.input.js", getServiceImportDeepInput],
["service-import-deep.output.js", getServiceImportDeepOutput],
["service-import-deep-with-name.input.js", getServiceImportDeepWithNameInput],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This file is generated by scripts/generateNewClientTests/index.ts
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.
"use strict";
import ACMClient = require("aws-sdk/clients/acm");
import AccessAnalyzerClient = require("aws-sdk/clients/accessanalyzer");
import DiscoveryClient = require("aws-sdk/clients/discovery");
new ACMClient();
new AccessAnalyzerClient();
new DiscoveryClient();
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// This file is generated by scripts/generateNewClientTests/index.ts
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.
"use strict";;
import AWS_ACM = require("@aws-sdk/client-acm");

const {
ACM: ACMClient
} = AWS_ACM;

import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");

const {
AccessAnalyzer: AccessAnalyzerClient
} = AWS_AccessAnalyzer;

import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");

const {
ApplicationDiscoveryService: DiscoveryClient
} = AWS_Discovery;

new ACMClient();
new AccessAnalyzerClient();
new DiscoveryClient();
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Collection, JSCodeshift } from "jscodeshift";

import { getV3DefaultLocalName } from "../utils";
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";
import { getImportEqualsLocalNameSuffix } from "./getImportEqualsLocalNameSuffix";
import { getV2ImportEqualsDeclaration } from "./getV2ImportEqualsDeclaration";
import { V3ClientModulesOptions } from "./types";

Expand All @@ -10,9 +11,7 @@ export const addV3ClientDefaultImportEquals = (
source: Collection<unknown>,
{ v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: V3ClientModulesOptions
) => {
const localNameSuffix = v3ClientPackageName.startsWith("@aws-sdk/client-")
? v2ClientLocalName
: v3ClientPackageName.substring(9).replace(/-/g, "_");
const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName);
const v3ClientDefaultLocalName = getV3DefaultLocalName(localNameSuffix);
const existingImportEquals = source.find(
j.TSImportEqualsDeclaration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Collection, JSCodeshift } from "jscodeshift";
import { getV3DefaultLocalName } from "../utils";
import { addV3ClientDefaultImportEquals } from "./addV3ClientDefaultImportEquals";
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";
import { getImportEqualsLocalNameSuffix } from "./getImportEqualsLocalNameSuffix";
import { getV3ClientRequireProperty } from "./getV3ClientRequireProperty";
import { objectPatternPropertyCompareFn } from "./objectPatternPropertyCompareFn";
import { V3ClientModulesOptions, V3ClientRequirePropertyOptions } from "./types";
Expand All @@ -13,11 +14,9 @@ export const addV3ClientNamedImportEquals = (
options: V3ClientModulesOptions & V3ClientRequirePropertyOptions
) => {
const { keyName, valueName, ...v3ClientModulesOptions } = options;
const { v2ClientLocalName, v3ClientPackageName } = v3ClientModulesOptions;
const { v2ClientName, v3ClientPackageName } = v3ClientModulesOptions;

const localNameSuffix = v3ClientPackageName.startsWith("@aws-sdk/client-")
? v2ClientLocalName
: v3ClientPackageName.substring(9).replace(/-/g, "_");
const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName);
const v3ClientDefaultLocalName = getV3DefaultLocalName(localNameSuffix);
const namedImportObjectProperty = getV3ClientRequireProperty(j, { keyName, valueName });

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const getImportEqualsLocalNameSuffix = (v2ClientName: string, v3ClientPackageName: string) =>
v3ClientPackageName.startsWith("@aws-sdk/client-")
? v2ClientName
: v3ClientPackageName.substring(9).replace(/-/g, "_");

0 comments on commit 06a92c7

Please sign in to comment.