Skip to content

Commit

Permalink
Write a basic wrapper over jscodeshift (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 2, 2022
1 parent 91ec885 commit baafe94
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-games-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add a basic wrapper over jscodeshift
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"files": [
"dist"
],
"main": "dist/index.js",
"main": "dist/cli.js",
"types": "dist/cli.d.ts",
"bin": "dist/cli.js",
"repository": {
"type": "git",
"url": "https://github.com/trivikr/aws-sdk-js-codemod.git"
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 { run } from "../cli";

jest.mock("child_process");

describe("cli", () => {
const jscodeshiftPath = "./node_modules/.bin/jscodeshift";

afterEach(() => {
jest.clearAllMocks();
});

it("should print aws-sdk-js-codemod version", async () => {
jest.spyOn(process.stdout, "write");

const mockArgs = ["--version"];
await run(mockArgs);

expect(process.stdout.write).toHaveBeenCalledWith(
expect.stringMatching(`aws-sdk-js-codemod: ${version}`)
);
expect(spawn).toHaveBeenCalledWith(jscodeshiftPath, mockArgs, { stdio: "inherit" });
});

it("should pass args to jscodeshift", async () => {
const mockArgs = ["random", "args", "one", "two", "three"];
await run(mockArgs);
expect(spawn).toHaveBeenCalledWith(jscodeshiftPath, mockArgs, { stdio: "inherit" });
});
});
21 changes: 21 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

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

const [, , ...args] = process.argv;
const jscodeshiftPath = "./node_modules/.bin/jscodeshift";

export const run = async (args): Promise<void> => {
if (args[0] === "--version") {
process.stdout.write(`aws-sdk-js-codemod: ${version}\n\n`);
spawn(jscodeshiftPath, ["--version"], { stdio: "inherit" });
} else {
spawn(jscodeshiftPath, args, { stdio: "inherit" });
}
};

run(args);
5 changes: 0 additions & 5 deletions src/index.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"declaration": true,
"esModuleInterop": true,
"outDir": "dist",
"resolveJsonModule": true,
"rootDir": "src"
},
"exclude": ["**/dist/**", "**/__tests__/**", "**/__testfixtures__/**"]
Expand Down
2 changes: 2 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,8 @@ __metadata:
simple-git-hooks: 2.7.0
ts-jest: 27.1.3
typescript: 4.5.5
bin:
aws-sdk-js-codemod: dist/cli.js
languageName: unknown
linkType: soft

Expand Down

0 comments on commit baafe94

Please sign in to comment.