Skip to content

Commit baafe94

Browse files
authored
Write a basic wrapper over jscodeshift (#4)
1 parent 91ec885 commit baafe94

File tree

9 files changed

+66
-28
lines changed

9 files changed

+66
-28
lines changed

.changeset/tall-games-knock.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+
Add a basic wrapper over jscodeshift

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"files": [
1919
"dist"
2020
],
21-
"main": "dist/index.js",
21+
"main": "dist/cli.js",
22+
"types": "dist/cli.d.ts",
23+
"bin": "dist/cli.js",
2224
"repository": {
2325
"type": "git",
2426
"url": "https://github.com/trivikr/aws-sdk-js-codemod.git"

src/__tests__/cli.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { spawn } from "child_process";
2+
3+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4+
// @ts-ignore: package.json will be imported from dist folders
5+
import { version } from "../../package.json"; // eslint-disable-line
6+
import { run } from "../cli";
7+
8+
jest.mock("child_process");
9+
10+
describe("cli", () => {
11+
const jscodeshiftPath = "./node_modules/.bin/jscodeshift";
12+
13+
afterEach(() => {
14+
jest.clearAllMocks();
15+
});
16+
17+
it("should print aws-sdk-js-codemod version", async () => {
18+
jest.spyOn(process.stdout, "write");
19+
20+
const mockArgs = ["--version"];
21+
await run(mockArgs);
22+
23+
expect(process.stdout.write).toHaveBeenCalledWith(
24+
expect.stringMatching(`aws-sdk-js-codemod: ${version}`)
25+
);
26+
expect(spawn).toHaveBeenCalledWith(jscodeshiftPath, mockArgs, { stdio: "inherit" });
27+
});
28+
29+
it("should pass args to jscodeshift", async () => {
30+
const mockArgs = ["random", "args", "one", "two", "three"];
31+
await run(mockArgs);
32+
expect(spawn).toHaveBeenCalledWith(jscodeshiftPath, mockArgs, { stdio: "inherit" });
33+
});
34+
});

src/cli.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env node
2+
3+
import { spawn } from "child_process";
4+
5+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
6+
// @ts-ignore: package.json will be imported from dist folders
7+
import { version } from "../package.json"; // eslint-disable-line
8+
9+
const [, , ...args] = process.argv;
10+
const jscodeshiftPath = "./node_modules/.bin/jscodeshift";
11+
12+
export const run = async (args): Promise<void> => {
13+
if (args[0] === "--version") {
14+
process.stdout.write(`aws-sdk-js-codemod: ${version}\n\n`);
15+
spawn(jscodeshiftPath, ["--version"], { stdio: "inherit" });
16+
} else {
17+
spawn(jscodeshiftPath, args, { stdio: "inherit" });
18+
}
19+
};
20+
21+
run(args);

src/index.spec.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"declaration": true,
55
"esModuleInterop": true,
66
"outDir": "dist",
7+
"resolveJsonModule": true,
78
"rootDir": "src"
89
},
910
"exclude": ["**/dist/**", "**/__tests__/**", "**/__testfixtures__/**"]

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,6 +1835,8 @@ __metadata:
18351835
simple-git-hooks: 2.7.0
18361836
ts-jest: 27.1.3
18371837
typescript: 4.5.5
1838+
bin:
1839+
aws-sdk-js-codemod: dist/cli.js
18381840
languageName: unknown
18391841
linkType: soft
18401842

0 commit comments

Comments
 (0)