• Repo
  • 文档
  • API Reference
  • Codemods

Turborepo Codemods

Turborepo provides Codemod transformations and automatic migration scripts to help upgrade your Turborepo codebase when a feature is deprecated.

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.

Usage

npx @turbo/codemod <transform> <path>
  • transform - name of transform, see available transforms below.
  • path - files or directory to transform
  • --dry - Do a dry-run, no code will be edited
  • --print - Prints the changed output for comparison

Turborepo 1.x

add-package-manager

Transforms the root package.json so that packageManager key as the detected package manager (yarn, npm, pnpm) and version (e.g. yarn@1.22.17). This key is now supported by Node.js and is used by Turborepo for faster package manager detection (vs. inferring from just the filesystem alone).

For example, for Yarn v1:

// Before
{
  "name": "turborepo-basic",
  "version": "0.0.0",
  "private": true,
  "workspaces": ["apps/*", "packages/*"]
  // ...
}
{
  "name": "turborepo-basic",
  "version": "0.0.0",
  "private": true,
+  "packageManager": "yarn@1.22.17",
  "workspaces": [
    "apps/*",
    "packages/*"
  ]
}

Usage

Go to your project:

cd path-to-your-turborepo/

Run the codemod:

npx @turbo/codemod add-package-manager

create-turbo-config

Creates the turbo.json file at the root of your project based on the "turbo" key in package.json. The "turbo" key is subsequently deleted from package.json.

For example:

// Before, package.json
{
  "name": "Monorepo root",
  "private": true,
  "turbo": {
    "pipeline": {
      ...
    }
  },
  ...
}
// After, package.json
{
  "name": "Monorepo root",
  "private": true,
-  "turbo": {
-    "pipeline": {
-      ...
-    }
-  },
  ...
}
 
// After, turbo.json
+{
+  "$schema": "https://turbo.build/schema.json",
+  "pipeline": {
+    ...
+  }
+}

Usage

Go to your project:

cd path-to-your-turborepo/

Run the codemod:

npx @turbo/codemod create-turbo-config

migrate-env-var-dependencies

Migrates all environment variable dependencies in turbo.json from dependsOn and globalDependencies to env and globalEnv respectively.

For example:

// Before, turbo.json
{
  "$schema": "https://turbo.build/schema.json",
  "globalDependencies": [".env", "$CI_ENV"],
  "pipeline": {
    "build": {
      "dependsOn": ["^build", "$API_BASE"],
      "outputs": [".next/**"]
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  }
}
// After, turbo.json
{
  "$schema": "https://turbo.build/schema.json",
- "globalDependencies": [".env", "$CI_ENV"],
+ "globalDependencies": [".env"],
+ "globalEnv": ["CI_ENV"],
  "pipeline": {
    "build": {
-     "dependsOn": ["^build", "$API_BASE"],
+     "dependsOn": ["^build"],
+     "env": ["API_BASE"],
      "outputs": [".next/**"],
    },
    "lint": {
      "outputs": []
    },
    "dev": {
      "cache": false
    }
  }
}
 
#### Usage
 
Go to your project:
 
```sh
cd path-to-your-turborepo/

Run the codemod:

npx @turbo/codemod migrate-env-var-dependencies