Skip to content

Update transform file if a custom transform is selected #15

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

Merged
merged 6 commits into from
Mar 3, 2022
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
5 changes: 5 additions & 0 deletions .changeset/rich-ladybugs-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Send updated transform file if a custom transform is selected
14 changes: 13 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { spawn } from "child_process";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: package.json will be imported from dist folders
import { version } from "../package.json"; // eslint-disable-line
import { getHelpParagraph, getTransforms } from "./utils";
import {
getArgsWithUpdatedTransformFile,
getHelpParagraph,
getTransformFileFromArgs,
getTransforms,
getUpdatedTransformFile,
} from "./utils";

export const run = async (args: string[]): Promise<void> => {
const transforms = getTransforms();
Expand All @@ -14,6 +20,12 @@ export const run = async (args: string[]): Promise<void> => {
process.stdout.write(`aws-sdk-js-codemod: ${version}\n\n`);
} else if (args[0] === "--help" || args[0] === "-h") {
process.stdout.write(getHelpParagraph(transforms));
} else if (args.includes("-t") || args.some((arg) => arg.startsWith("--transform="))) {
const transformFile = getTransformFileFromArgs(args);
if (transforms.map(({ name }) => name).includes(transformFile)) {
const updatedTransformFile = getUpdatedTransformFile(transformFile);
args = getArgsWithUpdatedTransformFile(args, updatedTransformFile);
}
}
spawn("npm", ["exec", "jscodeshift", "--", ...args], {
stdio: "inherit",
Expand Down
18 changes: 18 additions & 0 deletions src/utils/getArgsWithUpdatedTransformFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const getArgsWithUpdatedTransformFile = (
args: string[],
updatedTransformFile: string
): string[] => {
if (args.includes("-t")) {
const transformIndex = args.indexOf("-t");
args[transformIndex + 1] = updatedTransformFile;
return args;
}

const transformArg = args.find((arg) => arg.startsWith("--transform="));
if (!transformArg) {
throw new Error("No transform file specified in -t or --transform.");
}
const transformIndex = args.indexOf(transformArg);
args[transformIndex] = `--transform=${updatedTransformFile}`;
return args;
};
12 changes: 12 additions & 0 deletions src/utils/getTransformFileFromArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const getTransformFileFromArgs = (args: string[]): string => {
if (args.includes("-t")) {
const transformIndex = args.indexOf("-t");
return args[transformIndex + 1];
}

const transformArg = args.find((arg) => arg.startsWith("--transform="));
if (!transformArg) {
throw new Error("No transform file specified in -t or --transform.");
}
return transformArg.split("=")[1];
};
4 changes: 4 additions & 0 deletions src/utils/getUpdatedTransformFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { resolve } from "path";

export const getUpdatedTransformFile = (transformFolder: string) =>
resolve(__dirname, "..", "transforms", transformFolder, "transformer.js");
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from "./getArgsWithUpdatedTransformFile";
export * from "./getHelpParagraph";
export * from "./getTransformFileFromArgs";
export * from "./getTransforms";
export * from "./getUpdatedTransformFile";