|
| 1 | +--- |
| 2 | +id: monorepos |
| 3 | +title: Integrating with monorepos |
| 4 | +slug: /monorepos |
| 5 | +--- |
| 6 | + |
| 7 | +Codeshift is specifically designed to play nicely with monorepos (AKA multi-package repositories, multi-project repositories, or monolithic repositories) such as [Turborepo](https://turborepo.org/), [Yarn](https://classic.yarnpkg.com/lang/en/docs/workspaces/), [npm](https://docs.npmjs.com/cli/v7/using-npm/workspaces/) and [pnpm](https://pnpm.io/workspaces) workspaces and [Lerna](https://lerna.js.org/). |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +Configuring Codeshift in a monorepo can be done in the exact same way as [any existing npm package](/docs/authoring#2-add-codeshift-to-an-existing-package), simply by adding `codeshift.config.js` files and codemods to your packages. |
| 12 | + |
| 13 | +Given you are using a workspace-based monorepo, it's recommended to co-locate codemods with your package source files so they can be published as part of the package, for use by both external and internal users. |
| 14 | + |
| 15 | +```diff |
| 16 | +packages/ |
| 17 | + button/ |
| 18 | ++ codemods/ |
| 19 | ++ 1.0.0/ |
| 20 | ++ transform.ts |
| 21 | ++ transform.spec.ts |
| 22 | ++ 2.0.0/ |
| 23 | ++ codeshift.config.js |
| 24 | + src/ |
| 25 | + ... |
| 26 | + package.json |
| 27 | + rollup.config.json |
| 28 | + jest.config.js |
| 29 | + dialog/ |
| 30 | + modal/ |
| 31 | + |
| 32 | +package.json |
| 33 | +``` |
| 34 | + |
| 35 | +Codeshift config files can be located in either the root, `/src` or `/codemods` directory of a package. |
| 36 | + |
| 37 | +```diff |
| 38 | +packages/ |
| 39 | + button/ |
| 40 | ++ codemods/ |
| 41 | ++ 1.0.0/ |
| 42 | ++ 2.0.0/ |
| 43 | + src/ |
| 44 | + ... |
| 45 | + package.json |
| 46 | + rollup.config.json |
| 47 | + jest.config.js |
| 48 | ++ codeshift.config.js |
| 49 | + dialog/ |
| 50 | + modal/ |
| 51 | + |
| 52 | +package.json |
| 53 | +``` |
| 54 | + |
| 55 | +The structure or use of the `/codemods` directory is entirely up to you. Codemods can be located anywhere in the package as long as the config file correctly |
| 56 | +points to them. |
| 57 | + |
| 58 | +See the [configuration guide](/docs/configuration) for help writing config files. |
| 59 | + |
| 60 | +## Initialization |
| 61 | + |
| 62 | +Codeshift provides a CLI to quickly codegen a working codemod package around your existing source files. To do so, run `init` with the `--config-only` flag, |
| 63 | +which will output a bare-bones configuration. If you provide a `--transform` or `--preset` it will also generate empty transform files in addition. |
| 64 | + |
| 65 | +`$ codeshift init --config-only --transform 10.0.0 --preset foobar packages/my-package` |
| 66 | + |
| 67 | +(Note: this script assumes you have installed `@codeshift/cli` globally) |
| 68 | + |
| 69 | +The output of this command will give you a filestructure matching the above example. |
| 70 | + |
| 71 | +## Development |
| 72 | + |
| 73 | +When writing codemod(s) it's recommended to use a [test-driven development approach](/docs/testing) before attempting to run on any real source code. |
| 74 | +Once you're confident that your codemod works as expected, you will likely want to manually verify against real code. |
| 75 | +That's where you can use the following command: |
| 76 | + |
| 77 | +`$ codeshift path/to/test/code`. |
| 78 | + |
| 79 | +When used at the root directory of your monorepo, the CLI will leverage your workspaces config located in your `package.json` to determine which codemods in the monorepo it can run. |
| 80 | + |
| 81 | +ie: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "name": "monorepo", |
| 86 | + "version": "1.0.0", |
| 87 | + "workspaces": ["docs", "apps/*", "packages/*"] |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +And show an interactive list for you to search and can choose from. |
| 92 | + |
| 93 | +## Publising |
| 94 | + |
| 95 | +The workflow to publishing your codemod enhanced packages should remain completely unchanged. |
| 96 | +However, it's important to verify that your `codeshift.config.js` and codemods are not ignored by npm (via `.npmignore` or similar) so that they are successfully published to the registry. |
| 97 | + |
| 98 | +In some cases you may need to add these to the `files` property of your package's `package.json` like so: |
| 99 | + |
| 100 | +```json |
| 101 | +{ |
| 102 | + "name": "@monorepo/my-package", |
| 103 | + "version": "0.6.0", |
| 104 | + "main": "./dist/index.js", |
| 105 | + "files": ["dist", "src", "codemods", "codeshift.config.js"] |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Running |
| 110 | + |
| 111 | +Running a codemod as a consumer can now be done via the `@codemod/cli`, assuming your package is named `@monorepo/my-package` and your codemod is `1.0.0`: |
| 112 | + |
| 113 | +`$ codeshift --packages @monorepo/my-package@1.0.0 /path/to/source` |
| 114 | + |
| 115 | +You can even omit the codemod name to be prompted with all codemods an interactive list: |
| 116 | + |
| 117 | +`$ codeshift --packages @monorepo/my-package /path/to/source` |
| 118 | + |
| 119 | +## Examples |
| 120 | + |
| 121 | +Here are some helpful example of this setup working in the wild: |
| 122 | + |
| 123 | +- [Compiled codemods](https://github.com/atlassian-labs/compiled/tree/master/packages/codemods) |
| 124 | +- [Webdriver.io codemods](https://github.com/webdriverio/codemod) |
0 commit comments