forked from vercel/turborepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): turbo migrate (vercel#3367)
## 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
Showing
95 changed files
with
5,755 additions
and
720 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
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
8 changes: 4 additions & 4 deletions
8
crates/turbopack/tests/node-file-trace/integration/mdx/index.cjs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"version": "0.0.0", | ||
"name": "@turbo/tracing-mdx-test", | ||
"type": "module" | ||
} | ||
} |
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 |
---|---|---|
@@ -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). |
7 changes: 7 additions & 0 deletions
7
...turbo-codemod/__tests__/__fixtures__/add-package-manager/has-package-manager/package.json
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,7 @@ | ||
{ | ||
"name": "has-package-manager", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
6 changes: 6 additions & 0 deletions
6
.../turbo-codemod/__tests__/__fixtures__/add-package-manager/no-package-manager/package.json
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,6 @@ | ||
{ | ||
"name": "no-package-manager", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
7 changes: 7 additions & 0 deletions
7
...rbo-codemod/__tests__/__fixtures__/add-package-manager/wrong-package-manager/package.json
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,7 @@ | ||
{ | ||
"name": "has-package-manager", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/turbo-codemod/__tests__/__fixtures__/create-turbo-config/both-configs/package.json
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,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 | ||
} | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/turbo-codemod/__tests__/__fixtures__/create-turbo-config/both-configs/turbo.json
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,18 @@ | ||
{ | ||
"$schema": "https://turbo.build/schema.json", | ||
"pipeline": { | ||
"turbo-only": { | ||
"cache": false, | ||
"persistent": true | ||
}, | ||
"build": { | ||
"outputs": [".next/**"] | ||
}, | ||
"lint": { | ||
"outputs": [] | ||
}, | ||
"dev": { | ||
"cache": false | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...bo-codemod/__tests__/__fixtures__/create-turbo-config/no-package-json-config/package.json
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,7 @@ | ||
{ | ||
"name": "no-turbo-json-config", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
1 change: 1 addition & 0 deletions
1
...codemod/__tests__/__fixtures__/create-turbo-config/no-package-json-file/a-random-file.txt
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 @@ | ||
Nothing exists here |
23 changes: 23 additions & 0 deletions
23
...urbo-codemod/__tests__/__fixtures__/create-turbo-config/no-turbo-json-config/package.json
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,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 | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...s/turbo-codemod/__tests__/__fixtures__/create-turbo-config/turbo-json-config/package.json
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,7 @@ | ||
{ | ||
"name": "both-configs", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
18 changes: 18 additions & 0 deletions
18
...ges/turbo-codemod/__tests__/__fixtures__/create-turbo-config/turbo-json-config/turbo.json
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,18 @@ | ||
{ | ||
"$schema": "https://turbo.build/schema.json", | ||
"pipeline": { | ||
"turbo-only": { | ||
"cache": false, | ||
"persistent": true | ||
}, | ||
"build": { | ||
"outputs": [".next/**"] | ||
}, | ||
"lint": { | ||
"outputs": [] | ||
}, | ||
"dev": { | ||
"cache": false | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/turbo-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/no-deps/package.json
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,4 @@ | ||
{ | ||
"name": "no-turbo", | ||
"version": "0.0.0" | ||
} |
1 change: 1 addition & 0 deletions
1
...o-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/no-package/README.md
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 @@ | ||
Nothing here |
6 changes: 6 additions & 0 deletions
6
...ages/turbo-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/no-turbo/package.json
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,6 @@ | ||
{ | ||
"name": "no-turbo", | ||
"version": "0.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
12 changes: 12 additions & 0 deletions
12
...tests__/__fixtures__/get-turbo-upgrade-command/normal-workspaces-dev-install/package.json
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,12 @@ | ||
{ | ||
"name": "normal-workspaces", | ||
"version": "0.0.0", | ||
"workspaces": [ | ||
"apps/*", | ||
"packages/*" | ||
], | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"turbo": "1.0.0" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...o-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/normal-workspaces/package.json
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,12 @@ | ||
{ | ||
"name": "normal-workspaces", | ||
"version": "0.0.0", | ||
"workspaces": [ | ||
"apps/*", | ||
"packages/*" | ||
], | ||
"dependencies": { | ||
"turbo": "1.0.0" | ||
}, | ||
"devDependencies": {} | ||
} |
8 changes: 8 additions & 0 deletions
8
...__tests__/__fixtures__/get-turbo-upgrade-command/pnpm-workspaces-dev-install/package.json
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,8 @@ | ||
{ | ||
"name": "pnpm-workspaces", | ||
"version": "0.0.0", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"turbo": "1.0.0" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...__/__fixtures__/get-turbo-upgrade-command/pnpm-workspaces-dev-install/pnpm-workspace.yaml
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,3 @@ | ||
packages: | ||
- "apps/*" | ||
- "packages/*" |
8 changes: 8 additions & 0 deletions
8
...rbo-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/pnpm-workspaces/package.json
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,8 @@ | ||
{ | ||
"name": "pnpm-workspaces", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"turbo": "1.0.0" | ||
}, | ||
"devDependencies": {} | ||
} |
3 changes: 3 additions & 0 deletions
3
...emod/__tests__/__fixtures__/get-turbo-upgrade-command/pnpm-workspaces/pnpm-workspace.yaml
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,3 @@ | ||
packages: | ||
- "apps/*" | ||
- "packages/*" |
8 changes: 8 additions & 0 deletions
8
.../__tests__/__fixtures__/get-turbo-upgrade-command/single-package-dev-install/package.json
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,8 @@ | ||
{ | ||
"name": "single-package-dev-install", | ||
"version": "0.0.0", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"turbo": "1.0.0" | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...urbo-codemod/__tests__/__fixtures__/get-turbo-upgrade-command/single-package/package.json
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,8 @@ | ||
{ | ||
"name": "single-package", | ||
"version": "0.0.0", | ||
"dependencies": { | ||
"turbo": "1.0.0" | ||
}, | ||
"devDependencies": {} | ||
} |
21 changes: 21 additions & 0 deletions
21
...o-codemod/__tests__/__fixtures__/migrate-env-var-dependencies/env-dependencies/turbo.json
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,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 | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
.../__tests__/__fixtures__/migrate-env-var-dependencies/migrated-env-dependencies/turbo.json
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,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": [] | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...bo-codemod/__tests__/__fixtures__/migrate-env-var-dependencies/no-turbo-json/package.json
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,7 @@ | ||
{ | ||
"name": "no-turbo-json", | ||
"version": "1.0.0", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"packageManager": "[email protected]" | ||
} |
20 changes: 20 additions & 0 deletions
20
...turbo-codemod/__tests__/__fixtures__/migrate-env-var-dependencies/old-config/package.json
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,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": {} | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
..._/__fixtures__/with-old-config/turbo.json → ...nv-var-dependencies/old-config/turbo.json
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"$schema": "https://turbo.build/schema.json", | ||
"pipeline": { | ||
"build-one": { | ||
"outputs": ["foo"] | ||
|
1 change: 1 addition & 0 deletions
1
packages/turbo-codemod/__tests__/__fixtures__/migrate/no-repo/README.md
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 @@ | ||
Nothing here |
Oops, something went wrong.