Skip to content

Commit d861dfb

Browse files
committed
Adds monorepo guide
1 parent ccf88eb commit d861dfb

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

website/docs/api/codeshift-cli.mdx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ Create a 'config only' codeshift package in the current directory
218218

219219
- `$ codeshift init --config-only .`
220220

221+
### --config-only (optional)
222+
223+
Only output a configuration file.
224+
225+
**example:**
226+
Initializes a configuration file only
227+
228+
- `$ codeshift init --config-only --preset update-imports path/to/src``
229+
221230
### --version (optional)
222231

223232
A semver-compatible string. Will be used to generate mock transform files and configuration.

website/docs/guides/monorepos.mdx

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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)

website/docusaurus.config.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ module.exports = {
9191
items: [
9292
{
9393
label: 'Stack Overflow',
94-
href:
95-
'https://stackoverflow.com/questions/tagged/CodeshiftCommunity',
94+
href: 'https://stackoverflow.com/questions/tagged/CodeshiftCommunity',
9695
},
9796
{
9897
label: 'Twitter',
@@ -109,8 +108,7 @@ module.exports = {
109108
},
110109
{
111110
label: 'Roadmap',
112-
href:
113-
'https://github.com/CodeshiftCommunity/CodeshiftCommunity/projects/1',
111+
href: 'https://github.com/CodeshiftCommunity/CodeshiftCommunity/projects/1',
114112
},
115113
],
116114
},

website/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = {
3636
'guides/when-not-to-codemod',
3737
'guides/prompting-for-human-input',
3838
'guides/css-codemods',
39+
'guides/monorepos',
3940
],
4041
},
4142
{

0 commit comments

Comments
 (0)