Skip to content

Commit dbd8346

Browse files
authored
Placeholder for transforms with information in help (#12)
1 parent cb56b1b commit dbd8346

File tree

11 files changed

+143
-2
lines changed

11 files changed

+143
-2
lines changed

.changeset/pink-teachers-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Adds a placeholder for transforms with information under help option

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"dependencies": {
3636
"jscodeshift": "0.13.1",
37-
"jscodeshift-find-imports": "2.0.4"
37+
"jscodeshift-find-imports": "2.0.4",
38+
"table": "6.8.0"
3839
},
3940
"devDependencies": {
4041
"@changesets/cli": "2.21.0",

src/cli.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import { spawn } from "child_process";
55
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
66
// @ts-ignore: package.json will be imported from dist folders
77
import { version } from "../package.json"; // eslint-disable-line
8+
import { getHelpParagraph, getTransforms } from "./utils";
9+
10+
export const run = async (args: string[]): Promise<void> => {
11+
const transforms = getTransforms();
812

9-
export const run = async (args): Promise<void> => {
1013
if (args[0] === "--version") {
1114
process.stdout.write(`aws-sdk-js-codemod: ${version}\n\n`);
15+
} else if (args[0] === "--help" || args[0] === "-h") {
16+
process.stdout.write(getHelpParagraph(transforms));
1217
}
1318
spawn("npm", ["exec", "jscodeshift", "--", ...args], {
1419
stdio: "inherit",

src/transforms/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./types";

src/transforms/types.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export type PositionalOptionsType = "boolean" | "number" | "string";
2+
3+
export interface AwsSdkJsCodemodTransformOption {
4+
/** array of values, limit valid option arguments to a predefined set */
5+
choices?: ReadonlyArray<boolean> | ReadonlyArray<number> | ReadonlyArray<string>;
6+
7+
/** value, set a default value for the option */
8+
default?: boolean | number | string;
9+
10+
/** string, the option description for help content */
11+
description: string;
12+
13+
/** string, the type of the option */
14+
type: PositionalOptionsType;
15+
}
16+
17+
export interface AwsSdkJsCodemodTransform {
18+
/** string, the name of the transform */
19+
name: string;
20+
21+
/** string, the description of the transform for help content */
22+
description: string;
23+
24+
/** array of TransformOption, the which can be passed to the transform */
25+
options: AwsSdkJsCodemodTransformOption[];
26+
}

src/transforms/v2-to-v3/transform.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const name = "v2-to-v3";
2+
3+
export const description =
4+
"Converts AWS SDK for JavaScript APIs in a Javascript/TypeScript codebase" +
5+
" from version 2 (v2) to version 3 (v3).";
6+
7+
// Additional options will come here, like running on specific clients.
8+
export const options = [];
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { API, FileInfo } from "jscodeshift";
2+
3+
export default function transformer(file: FileInfo, api: API) {
4+
const j = api.jscodeshift;
5+
const root = j(file.source);
6+
7+
// transform `root` here
8+
9+
return root.toSource();
10+
}

src/utils/getHelpParagraph.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { getBorderCharacters, table } from "table";
2+
3+
import { AwsSdkJsCodemodTransform } from "../transforms";
4+
5+
export const getHelpParagraph = (transforms: AwsSdkJsCodemodTransform[]) =>
6+
`----------------------------------------------------------------------------------------------------
7+
aws-sdk-js-codemod is a lightweight wrapper over jscodeshift.
8+
It processes --help, --version and --transform options before passing them downstream.
9+
10+
You can provide names of the custom transforms instead of a local path or url:
11+
12+
${table(
13+
transforms.map(({ name, description }) => [name, description]),
14+
{
15+
border: getBorderCharacters("void"),
16+
columns: [
17+
{ width: 12, alignment: "right" },
18+
{ width: 88, wrapWord: true },
19+
],
20+
}
21+
)}Example: aws-sdk-js-codemod -t v2-to-v3 example.js
22+
23+
----------------------------------------------------------------------------------------------------\n\n`;

src/utils/getTransforms.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { readdirSync } from "fs";
2+
import { join } from "path";
3+
4+
import { AwsSdkJsCodemodTransform } from "../transforms";
5+
6+
export const getTransforms = () =>
7+
readdirSync(join(__dirname, "..", "transforms"), { withFileTypes: true })
8+
.filter((dirent) => dirent.isDirectory())
9+
.map((dirent) => dirent.name)
10+
.map((dirName) => {
11+
const { name, description, options } = require(`../transforms/${dirName}/transform`); // eslint-disable-line
12+
return { name, description, options } as AwsSdkJsCodemodTransform;
13+
});

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./getHelpParagraph";
2+
export * from "./getTransforms";

0 commit comments

Comments
 (0)