From baafe94821deda8af42594e148b5a389332863ed Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Wed, 2 Mar 2022 10:35:40 -0800 Subject: [PATCH] Write a basic wrapper over jscodeshift (#4) --- .changeset/tall-games-knock.md | 5 +++++ LICENSE | 21 --------------------- package.json | 4 +++- src/__tests__/cli.spec.ts | 34 ++++++++++++++++++++++++++++++++++ src/cli.ts | 21 +++++++++++++++++++++ src/index.spec.ts | 5 ----- src/index.ts | 1 - tsconfig.json | 1 + yarn.lock | 2 ++ 9 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 .changeset/tall-games-knock.md delete mode 100644 LICENSE create mode 100644 src/__tests__/cli.spec.ts create mode 100755 src/cli.ts delete mode 100644 src/index.spec.ts delete mode 100644 src/index.ts diff --git a/.changeset/tall-games-knock.md b/.changeset/tall-games-knock.md new file mode 100644 index 000000000..03e7e63e9 --- /dev/null +++ b/.changeset/tall-games-knock.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Add a basic wrapper over jscodeshift diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 81d37a79d..000000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2022 Trivikram Kamat - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/package.json b/package.json index 87426c52d..06f279950 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/__tests__/cli.spec.ts b/src/__tests__/cli.spec.ts new file mode 100644 index 000000000..b0436c57e --- /dev/null +++ b/src/__tests__/cli.spec.ts @@ -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" }); + }); +}); diff --git a/src/cli.ts b/src/cli.ts new file mode 100755 index 000000000..50e9abefc --- /dev/null +++ b/src/cli.ts @@ -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 => { + 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); diff --git a/src/index.spec.ts b/src/index.spec.ts deleted file mode 100644 index 1c57ba8b6..000000000 --- a/src/index.spec.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { answer } from "./index"; - -it("answer is 42", () => { - expect(answer).toBe(42); -}); diff --git a/src/index.ts b/src/index.ts deleted file mode 100644 index 64a32fd29..000000000 --- a/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export const answer = 42; diff --git a/tsconfig.json b/tsconfig.json index 21a4873c0..a787b099e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "declaration": true, "esModuleInterop": true, "outDir": "dist", + "resolveJsonModule": true, "rootDir": "src" }, "exclude": ["**/dist/**", "**/__tests__/**", "**/__testfixtures__/**"] diff --git a/yarn.lock b/yarn.lock index d54e4edd7..6c0b4316d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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