Skip to content

Commit

Permalink
feat(codemod): turbo migrate (vercel#3367)
Browse files Browse the repository at this point in the history
## Details:
* New migrate command that will run all codemods needed to move from
turbo `x` to turbo `y` and will (optionally) upgrade turbo for you as
well
* Rewritten transforms using new transform class that automatically
handles logging, determining when to write the file, file diffs,
consistent outputs, and consistent logging
* When running with --print, we now show changed file diffs complete
with colors
* 100% test coverage for all transforms
* A codemod for adding new codemods (:yodog:)
* `pnpm add-transform`. Adds tests, fixtures, and new transform
boilerplate.
* Moved from meow to commander.js for arg parsing

## Reviewers Guide
This PR is huge. But it’s almost all tests and fixtures. I have split
the commits for easier review:
1. [new arg
parsing](vercel@21ff5ae)
2. [rewrite
transforms](vercel@441de33)
3. [add codemod
generation](vercel@45070d2)
4. [add migrate
command](vercel@3cd8ed6)
5. [add
tests](vercel@cdbb17c)

### FAQ
**Q**. Why not just use `jscodeshift`? 
**A**. It would be major overkill. We don’t need AST’s. Everything we
touch (and likely will touch for the foreseeable future) is JSON.

**Q**. Why add an extra class for file transforms?
**A**. This abstracts away the need to care about the CLI options or
handle output. Writing a new transform is as simple as - 1. read the
file, 2. make changes, 3. pass it to runner.modifyFile. It does the rest
(logging, determining when to write, file diffing, consistent outputs,
consistent logging)

## TODO:

- [ ] Update codemod docs and in-product reccomendations
  • Loading branch information
tknickman authored Jan 20, 2023
1 parent dc54647 commit b816608
Show file tree
Hide file tree
Showing 95 changed files with 5,755 additions and 720 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ graph*.html

# generated by cargo xtask publish
packages/node-module-trace/npm
packages/turbo-codemod/__tests__/test-runtime
artifacts
.turbo
.vercel
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ pnpm-lock.yaml
__generated__/
/docs/public/schema.json
/packages/eslint-plugin-turbo/__tests__/fixtures/
/packages/turbo-codemod/templates/
/docs/components/pages/pack-home/benchmark-data/data.json
/examples/with-svelte


# crates
crates/next-core/js/src/compiled
crates/turbopack-node/js/src/compiled
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const os = require("os");

// also trace the react and react/jsx-runtime
require('react')
require('react/jsx-runtime')
require("react");
require("react/jsx-runtime");

import('@mdx-js/node-loader')
import("@mdx-js/node-loader");

import('./mdx.js');
import("./mdx.js");

const { existsSync } = eval("require")("fs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"version": "0.0.0",
"name": "@turbo/tracing-mdx-test",
"type": "module"
}
}
51 changes: 50 additions & 1 deletion packages/turbo-codemod/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,54 @@
# Turborepo Codemods

Turborepo provides Codemod transformations to help upgrade your Turborepo codebase when a feature is deprecated.
Turborepo provides Codemod transformations to help upgrade your Turborepo codebase.

Codemods are transformations that run on your codebase programmatically. This allows for a large amount of changes to be applied without having to manually go through every file.

## Commands

### `migrate`

Updates your Turborepo codebase to the specified version of Turborepo (defaults to the latest), running any required codemods, and installing the new version of Turborepo.

```
Usage: @turbo/codemod migrate|update [options] [path]
Migrate a project to the latest version of Turborepo
Arguments:
path Directory where the transforms should be applied
Options:
--from <version> Specify the version to migrate from (default: current version)
--to <version> Specify the version to migrate to (default: latest)
--install Install new version of turbo after migration (default: true)
--force Bypass Git safety checks and forcibly run codemods (default: false)
--dry Dry run (no changes are made to files) (default: false)
--print Print transformed files to your terminal (default: false)
-h, --help display help for command
```

### `transform`

Runs a single codemod on your codebase.

```
Usage: @turbo/codemod transform [options] [transform] [path]
Apply a single code transformation to a project
Arguments:
transform The transformer to run
path Directory where the transforms should be applied
Options:
--force Bypass Git safety checks and forcibly run codemods (default: false)
--list List all available transforms (default: false)
--dry Dry run (no changes are made to files) (default: false)
--print Print transformed files to your terminal (default: false)
-h, --help display help for command
```

## Developing

To add a new transformer, run `pnpm add-transformer`, or [view the complete guide](./src/transforms/README.md).
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "has-package-manager",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "no-package-manager",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "has-package-manager",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "both-configs",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]",
"turbo": {
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"package-only": {
"cache": false,
"persistent": true
},
"build": {
"outputs": [
".next/**"
]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"turbo-only": {
"cache": false,
"persistent": true
},
"build": {
"outputs": [".next/**"]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "no-turbo-json-config",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing exists here
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "no-turbo-json-config",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]",
"turbo": {
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"outputs": [
".next/**"
]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "both-configs",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"turbo-only": {
"cache": false,
"persistent": true
},
"build": {
"outputs": [".next/**"]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "no-turbo",
"version": "0.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing here
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "no-turbo",
"version": "0.0.0",
"dependencies": {},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "normal-workspaces",
"version": "0.0.0",
"workspaces": [
"apps/*",
"packages/*"
],
"dependencies": {},
"devDependencies": {
"turbo": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "normal-workspaces",
"version": "0.0.0",
"workspaces": [
"apps/*",
"packages/*"
],
"dependencies": {
"turbo": "1.0.0"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pnpm-workspaces",
"version": "0.0.0",
"dependencies": {},
"devDependencies": {
"turbo": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- "apps/*"
- "packages/*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "pnpm-workspaces",
"version": "0.0.0",
"dependencies": {
"turbo": "1.0.0"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
- "apps/*"
- "packages/*"
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "single-package-dev-install",
"version": "0.0.0",
"dependencies": {},
"devDependencies": {
"turbo": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "single-package",
"version": "0.0.0",
"dependencies": {
"turbo": "1.0.0"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": ["$NEXT_PUBLIC_API_KEY", "$STRIPE_API_KEY", ".env"],
"pipeline": {
"build": {
"outputs": [".next/**"],
"dependsOn": ["^build", "$PROD_API_KEY"]
},
"lint": {
"outputs": [],
"dependsOn": ["$IS_CI"]
},
"test": {
"outputs": [],
"dependsOn": ["$IS_CI", "test"]
},
"dev": {
"cache": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "https://turbo.build/schema.json",
"globalDependencies": [],
"globalEnv": ["NEXT_PUBLIC_API_KEY", "STRIPE_API_KEY"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"env": ["PROD_API_KEY"],
"outputs": [".next/**"]
},
"dev": {
"cache": false
},
"lint": {
"dependsOn": [],
"env": ["IS_CI"],
"outputs": []
},
"test": {
"dependsOn": ["test"],
"env": ["IS_CI"],
"outputs": []
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "no-turbo-json",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "migrate-env-var-dependencies-old-config",
"version": "1.0.0",
"dependencies": {},
"devDependencies": {},
"packageManager": "[email protected]",
"turbo": {
"pipeline": {
"build-one": {
"outputs": [
"foo"
]
},
"build-two": {
"outputs": []
},
"build-three": {}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build-one": {
"outputs": ["foo"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing here
Loading

0 comments on commit b816608

Please sign in to comment.