diff --git a/packages/remix-dev/cli/migrate/jscodeshift.ts b/packages/remix-dev/cli/migrate/jscodeshift.ts index 2a22ab4d373..44e015913d9 100644 --- a/packages/remix-dev/cli/migrate/jscodeshift.ts +++ b/packages/remix-dev/cli/migrate/jscodeshift.ts @@ -1,11 +1,10 @@ -import { sync as execaSync } from "execa"; +// @ts-expect-error `@types/jscodeshift` doesn't have types for this +import { run as jscodeshift } from "jscodeshift/src/Runner"; import type { Flags } from "./flags"; -const jscodeshiftExecutable = require.resolve(".bin/jscodeshift"); - type Options = Record; -const toFlags = (options: Options = {}) => +const toCLIOptions = (options: Options = {}) => Object.entries(options) .filter(([_key, value]) => Boolean(value)) .map( @@ -13,48 +12,40 @@ const toFlags = (options: Options = {}) => `--${key}${typeof value === "boolean" ? "" : `=${value}`}` ); -type Args = { - transformPath: string; +type Args = { files: string[]; flags: Flags; transformOptions?: TransformOptions; + transformPath: string; }; -export const run = ({ - transformPath, +export const run = async ({ files, flags: { debug, dry, print, runInBand }, transformOptions, -}: Args): boolean => { - let args = [ - dry ? "--dry" : "", - print ? "--print" : "", - runInBand ? "--run-in-band" : "", - "--fail-on-error", - "--verbose=2", - "--ignore-pattern=**/node_modules/**", - "--ignore-pattern=**/.cache/**", - "--ignore-pattern=**/build/**", - "--extensions=tsx,ts,jsx,js", - "--parser=tsx", - ...["--transform", transformPath], - ...files, - ...toFlags(transformOptions || {}), - ]; + transformPath, +}: Args): Promise => { + let options = { + babel: true, // without this, `jscodeshift` will not be able to parse TS transforms + dry, + extensions: "tsx,ts,jsx,js", + failOnError: true, + ignorePattern: ["**/node_modules/**", "**/.cache/**", "**/build/**"], + parser: "tsx", + print, + runInBand, + verbose: 2, + ...transformOptions, + }; if (debug) { console.log( - `Executing command: jscodeshift ${args - .filter((arg) => arg !== "") - .join(" ")}` + `Executing command: jscodeshift ${toCLIOptions(options).join(" ")}` ); } try { - let result = execaSync(jscodeshiftExecutable, args, { - stdio: debug ? "inherit" : "ignore", - stripFinalNewline: false, - }); - return result.exitCode === 0; + let { error } = await jscodeshift(transformPath, files, options); + return error === 0; } catch (error) { return false; } diff --git a/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/index.ts b/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/index.ts index b327c21d594..29d03899791 100644 --- a/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/index.ts +++ b/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/index.ts @@ -151,11 +151,11 @@ export const replaceRemixImports: MigrationFunction = async ({ cwd: config.appDirectory, absolute: true, }); - let codemodOk = jscodeshift.run({ - transformPath: TRANSFORM_PATH, + let codemodOk = await jscodeshift.run({ files, flags, - transformOptions: { runtime, adapter }, + transformOptions: { adapter, runtime }, + transformPath: TRANSFORM_PATH, }); if (!codemodOk) { console.error("❌ I couldn't replace all of your `remix` imports."); diff --git a/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/transform/options.ts b/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/transform/options.ts index fcebb25c134..7a73b6ac327 100644 --- a/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/transform/options.ts +++ b/packages/remix-dev/cli/migrate/migrations/replace-remix-imports/transform/options.ts @@ -7,7 +7,7 @@ import type { Adapter } from "./adapter"; import type { Runtime } from "./runtime"; -export interface Options { - runtime: Runtime; +export type Options = { adapter?: Adapter; -} + runtime: Runtime; +}; diff --git a/packages/remix-dev/package.json b/packages/remix-dev/package.json index f1cdf29bd66..88f655a7694 100644 --- a/packages/remix-dev/package.json +++ b/packages/remix-dev/package.json @@ -28,7 +28,6 @@ "dotenv": "^16.0.0", "esbuild": "0.14.22", "esbuild-plugin-cache": "^0.2.9", - "execa": "^5.1.1", "exit-hook": "2.2.1", "express": "4.17.3", "fast-glob": "3.2.11", diff --git a/yarn.lock b/yarn.lock index 8d10f6131d5..ea31102f0ea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4728,7 +4728,7 @@ events@^3.3.0: resolved "https://registry.npmjs.org/events/-/events-3.3.0.tgz" integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q== -execa@^5.0.0, execa@^5.1.1: +execa@^5.0.0: version "5.1.1" resolved "https://registry.npmjs.org/execa/-/execa-5.1.1.tgz" integrity sha512-8uSpZZocAZRBAPIEINJj3Lo9HyGitllczc27Eh5YYojjMFMn8yHMDMaUHE2Jqfq05D/wucwI4JGURyXt1vchyg==