Skip to content

Commit

Permalink
fix(remix-dev/cli/migrate): directly use jscodeshift instead of via…
Browse files Browse the repository at this point in the history
… CLI (remix-run#3114)
  • Loading branch information
MichaelDeBoey authored and Christopher Trudel committed May 16, 2022
1 parent 55ae050 commit e81f0ad
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
55 changes: 23 additions & 32 deletions packages/remix-dev/cli/migrate/jscodeshift.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
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<string, unknown>;
const toFlags = (options: Options = {}) =>
const toCLIOptions = (options: Options = {}) =>
Object.entries(options)
.filter(([_key, value]) => Boolean(value))
.map(
([key, value]) =>
`--${key}${typeof value === "boolean" ? "" : `=${value}`}`
);

type Args<TransformOptions> = {
transformPath: string;
type Args<TransformOptions extends Options = Options> = {
files: string[];
flags: Flags;
transformOptions?: TransformOptions;
transformPath: string;
};
export const run = <TransformOptions>({
transformPath,
export const run = async <TransformOptions extends Options = Options>({
files,
flags: { debug, dry, print, runInBand },
transformOptions,
}: Args<TransformOptions>): 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<TransformOptions>): Promise<boolean> => {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ export const replaceRemixImports: MigrationFunction = async ({
cwd: config.appDirectory,
absolute: true,
});
let codemodOk = jscodeshift.run<Options>({
transformPath: TRANSFORM_PATH,
let codemodOk = await jscodeshift.run<Options>({
files,
flags,
transformOptions: { runtime, adapter },
transformOptions: { adapter, runtime },
transformPath: TRANSFORM_PATH,
});
if (!codemodOk) {
console.error("❌ I couldn't replace all of your `remix` imports.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
1 change: 0 additions & 1 deletion packages/remix-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit e81f0ad

Please sign in to comment.