-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(remix-dev): rewrite
jscodeshift
-based migrations as `babel…
…`-based codemods (#4572) * refactor(dev): extract `useColor` usages into `safe` utility * refactor(dev): remove outdated reference to compiler shims * fix(dev): remove access to `convert-to-javascript` migration via CLI conversion to javascript is not a "migration" in that it does not help the user to upgrade to a newer version of Remix * refactor(dev): rewrite `replace-remix-magic-exports` as a babel codemod Compared to the previous jscodeshift-based migration: - codemod no longer depends on a network connection - babel's visitor API for traversing the AST is simpler - not spinning up workers for applying code transforms This ends up speeding up the codemod by ~10x and (hopefully 🤞) fixes some of the issues we were seeing in CI on Windows (since we think problems are mostly timeouts caused by slow tests or overhead for workers). * test(dev): restore fixture this fixture was incorrectly included as part of dependency upgrades: - #3917 - #3929 it should not have been updated since it is supposed to represent a Remix 1.3.x codebase * test(dev): add tests for generic codemods and specifically for `replace-remix-magic-imports` * ci: run build before primary tests some tests can make use of the built javascript artifacts (e.g. `cli.js`) to run 8-10x faster than running with source Typescript (e.g. `cli.ts`) * test(dev): retry temp dir removal for windows ci Windows sometimes throws `EBUSY: resource busy or locked, rmdir` errors when attempting to removing the temporary directory. Retrying a couple times seems to get it to succeed. See https://github.com/jprichardson/node-fs-extra/issues?q=EBUSY%3A+resource+busy+or+locked%2C+rmdir * Create long-colts-remain.md
- Loading branch information
1 parent
869238a
commit 53a4fc1
Showing
85 changed files
with
1,970 additions
and
1,978 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
"remix": patch | ||
"@remix-run/dev": patch | ||
--- | ||
|
||
Replace migrations with codemods. Specifically, `npx @remix-run/dev migrate` is now `@remix-run/dev codemod`. | ||
|
||
Under the hood, codemods are now written via Babel's Visitor API instead of jscodeshift. | ||
Also `replace-remix-magic-imports` is now faster as it no longer depends on a network connection | ||
and does not incur the overhead of spinning up workers for jscodeshift. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/remix-dev/__tests__/codemod-replaceRemixMagicImports-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import NpmCliPackageJson from "@npmcli/package-json"; | ||
import fs from "fs"; | ||
import glob from "fast-glob"; | ||
import path from "path"; | ||
import shell from "shelljs"; | ||
import stripAnsi from "strip-ansi"; | ||
|
||
import { readConfig } from "../config"; | ||
import * as cli from "./utils/cli"; | ||
import * as eol from "./utils/eol"; | ||
import * as git from "./utils/git"; | ||
import withApp from "./utils/withApp"; | ||
|
||
let CODEMOD = "replace-remix-magic-imports"; | ||
let FIXTURE = path.join(__dirname, "fixtures/replace-remix-magic-imports"); | ||
|
||
it("replaces `remix` magic imports", async () => { | ||
await withApp(FIXTURE, async (projectDir) => { | ||
await git.initialCommit(projectDir); | ||
let result = await cli.run(["codemod", CODEMOD, projectDir]); | ||
let stderr = stripAnsi(result.stderr); | ||
expect(result.exitCode).toBe(0); | ||
|
||
let successes = [ | ||
`✔ Found codemod: ${CODEMOD}`, | ||
"✔ No Remix server adapter detected", | ||
"✔ Detected Remix server runtime: node", | ||
"✔ Removed magic `remix` package from dependencies", | ||
"✔ Removed `remix setup` from postinstall script", | ||
"✔ Replaced magic `remix` imports | 24/24 files", | ||
]; | ||
for (let success of successes) { | ||
expect(stderr).toContain(success); | ||
} | ||
|
||
expect(result.stdout).toContain( | ||
"👉 To update your lockfile, run `yarn install`" | ||
); | ||
|
||
let pkg = await NpmCliPackageJson.load(projectDir); | ||
let packageJson = pkg.content; | ||
|
||
// check that `remix` dependency was removed | ||
expect(packageJson.dependencies).not.toContain("remix"); | ||
expect(packageJson.devDependencies).not.toContain("remix"); | ||
|
||
// check that Remix packages were standardized | ||
expect(packageJson.dependencies).toEqual( | ||
expect.objectContaining({ | ||
"@remix-run/node": "1.3.4", | ||
"@remix-run/react": "1.3.4", | ||
"@remix-run/serve": "1.3.4", | ||
}) | ||
); | ||
expect(packageJson.devDependencies).toEqual( | ||
expect.objectContaining({ | ||
"@remix-run/dev": "1.3.4", | ||
}) | ||
); | ||
|
||
// check that postinstall was removed | ||
expect(packageJson.scripts).not.toContain("postinstall"); | ||
|
||
// check that `from "remix"` magic imports were removed | ||
let config = await readConfig(projectDir); | ||
let files = await glob("**/*.{js,jsx,ts,tsx}", { | ||
cwd: config.appDirectory, | ||
absolute: true, | ||
}); | ||
let remixMagicImports = shell.grep("-l", /from ('remix'|"remix")/, files); | ||
expect(remixMagicImports.code).toBe(0); | ||
expect(remixMagicImports.stdout.trim()).toBe(""); | ||
expect(remixMagicImports.stderr).toBeNull(); | ||
|
||
// check that imports look good for a specific file | ||
let loginRoute = eol.normalize( | ||
fs.readFileSync(path.join(projectDir, "app/routes/login.tsx"), "utf8") | ||
); | ||
expect(loginRoute).toContain( | ||
[ | ||
"import {", | ||
" type ActionFunction,", | ||
" type LoaderFunction,", | ||
" type MetaFunction,", | ||
" json,", | ||
" redirect,", | ||
'} from "@remix-run/node";', | ||
'import { Form, Link, useActionData, useSearchParams } from "@remix-run/react";', | ||
].join("\n") | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
|
||
import * as cli from "./utils/cli"; | ||
import * as git from "./utils/git"; | ||
import withApp from "./utils/withApp"; | ||
|
||
let FIXTURE = path.join(__dirname, "fixtures/replace-remix-magic-imports"); | ||
|
||
it("checks that project is a clean git repository", async () => { | ||
await withApp(FIXTURE, async (projectDir) => { | ||
// ensure project is a git repository | ||
let error1 = await cli.shouldError([ | ||
"codemod", | ||
"some-codemod-name", | ||
projectDir, | ||
]); | ||
expect(error1.exitCode).not.toBe(0); | ||
expect(error1.stderr).toContain(`${projectDir} is not a git repository`); | ||
expect(error1.stdout).toContain( | ||
"To override this safety check, use the --force flag" | ||
); | ||
|
||
// initialize git repo in project | ||
await git.initialCommit(projectDir); | ||
|
||
// make some uncommitted changes | ||
fs.appendFileSync(path.join(projectDir, "package.json"), "change"); | ||
|
||
// ensure project has no uncommitted changes | ||
let error2 = await cli.shouldError([ | ||
"codemod", | ||
"some-codemod-name", | ||
projectDir, | ||
]); | ||
expect(error2.exitCode).not.toBe(0); | ||
expect(error2.stderr).toContain(`${projectDir} has uncommitted changes`); | ||
expect(error2.stdout).toContain( | ||
"Stash or commit your changes before running codemods" | ||
); | ||
expect(error2.stdout).toContain( | ||
"To override this safety check, use the --force flag" | ||
); | ||
}); | ||
}); | ||
|
||
it("checks that the specified codemod exists", async () => { | ||
await withApp(FIXTURE, async (projectDir) => { | ||
await git.initialCommit(projectDir); | ||
|
||
let codemodName = "invalid-codemod-name"; | ||
let error = await cli.shouldError(["codemod", codemodName, projectDir]); | ||
expect(error.exitCode).toBe(1); | ||
expect(error.stderr).toContain(`Unrecognized codemod: ${codemodName}`); | ||
}); | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.