Skip to content

Commit 41c1fa6

Browse files
committed
feat(codemod): Created a new @react-md/codemod package to help with new releases
1 parent 436fbff commit 41c1fa6

32 files changed

+1158
-22
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
packages/material-icons/src
22
packages/documentation/src/constants/sandboxes
33
packages/documentation/src/components/SassDocPage
4+
packages/codemod/transforms/**/__tests__
5+
packages/codemod/transforms/**/__testfixtures__
46

57
# this is needed to fix a config extension error for "@ljharb" in the qs library
68
packages/*/node_modules

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ packages/*/*.tsbuildinfo
1111
/packages/*/lib
1212
/packages/*/dist
1313
/packages/*/types
14+
/packages/codemod/bin
1415
/packages/dev-utils/bin
1516
/packages/material-icons/bin
1617
/packages/material-icons/temp

.prettierignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ packages/*/bin
1818
packages/*/dist
1919
packages/*/types
2020
packages/*/.next
21+
packages/codemod/transforms/**/__testfixtures__/**/*.output.ts
22+
packages/codemod/transforms/**/__testfixtures__/**/*.output.tsx
23+
packages/codemod/transforms/**/__testfixtures__/**/*.output.js
24+
packages/codemod/transforms/**/__testfixtures__/**/*.output.jsx
2125
packages/documentation/src/constants/sassdoc
2226
packages/documentation/src/constants/sandboxes
2327
packages/documentation/public/tsdocs

jest.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const path = require('path');
33

44
const packages = fs
55
.readdirSync(path.join(process.cwd(), 'packages'))
6-
.filter((name) => !['dev-utils'].includes(name));
6+
.filter((name) => !['dev-utils', 'codemod'].includes(name));
7+
8+
const roots = packages.map((name) => `<rootDir>/packages/${name}/src`);
9+
roots.push('<rootDir>/packages/codemod');
710

811
const docsSrc = '<rootDir>/packages/documentation/src';
912

@@ -15,6 +18,7 @@ if (!isWatchMode) {
1518
'<rootDir>/packages/*/src/**/*.{ts,tsx}',
1619
// internal usage and don't matter for the library coverage reports
1720
'!<rootDir>/packages/{dev-utils,documentation,material-icons,react-md}/src/**/*',
21+
'!<rootDir>/packages/codemod/**/*',
1822
// these are generated files
1923
'!<rootDir>/packages/*/src/scssVariables.ts',
2024
// index.ts files are always `export * from "./fileOrFolder"`
@@ -25,7 +29,7 @@ if (!isWatchMode) {
2529
module.exports = {
2630
preset: 'ts-jest',
2731
testEnvironment: 'jsdom',
28-
roots: packages.map((name) => `<rootDir>/packages/${name}/src`),
32+
roots,
2933
globals: {
3034
'ts-jest': {
3135
tsconfig: './tsconfig.test.json',

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"prepare": "husky install",
99
"cz": "cz",
10+
"rmd-codemod": "node packages/codemod/bin/rmd-codemod.js",
1011
"release": "dev-utils release",
1112
"sandbox": "dev-utils sandbox",
1213
"sassdoc": "dev-utils sassdoc",
@@ -27,7 +28,8 @@
2728
"build-cjs": "tsc -b tsconfig.cjs.json",
2829
"build-var": "tsc -b tsconfig.var.json",
2930
"build-umd": "yarn workspace react-md umd --silent",
30-
"build": "npm-run-all styles combine-styles build-ejs build-cjs build-var",
31+
"build-codemod": "yarn workspace @react-md/codemod build",
32+
"build": "npm-run-all styles combine-styles build-ejs build-cjs build-var build-codemod",
3133
"typecheck": "tsc -p tsconfig.check.json",
3234
"lint-scripts": "eslint \"packages/*/src/**/*.{ts,tsx,js,jsx}\"",
3335
"lint-styles": "stylelint \"packages/*/src/**/*.scss\"",

packages/codemod/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2021 Mikkel Laursen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

packages/codemod/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# @react-md/codemod
2+
3+
This repository contains a collection of codemod scripts for use with
4+
[JSCodeshift](https://github.com/facebook/jscodeshift) that help update ReactMD
5+
APIs.
6+
7+
> Note: If you use [prettier](https://prettier.io/), you will most likely need
8+
> to re-format your files after running a codemod.
9+
10+
<!-- docs -->
11+
12+
## Usage
13+
14+
```sh
15+
Usage: npx @react-md/codemod [options] [command]
16+
17+
Run a codemod script to update to the latest version of ReactMD.
18+
19+
Running this script without any options or commands will start an interactive wizard.
20+
21+
22+
Options:
23+
-h, --help display help for command
24+
25+
Commands:
26+
v3-to-v4/preset [options] [files...]
27+
v3-to-v4/rename-text-to-typography [options] [files...]
28+
v3-to-v4/scale-transition-props [options] [files...]
29+
```
30+
31+
### Transformations
32+
33+
### `v3-to-v4/preset`
34+
35+
```sh
36+
Usage: npx @react-md/codemod v3-to-v4/preset [options] [files...]
37+
38+
Arguments:
39+
files An optional glob or folder path to transform
40+
(default: ".")
41+
42+
Options:
43+
-d, --dry Dry run (no changes are made to files) (default:
44+
false)
45+
-p, --print Print transformed files to your terminal
46+
(default: false)
47+
--parser <parser> The file parser to use. (choices: "babel",
48+
"tsx", "", default: "")
49+
--jscodeshift <jscodeshift> (Advanced) Pass options directly to jscodeshift
50+
(default: "")
51+
-h, --help display help for command
52+
```
53+
54+
### `v3-to-v4/rename-text-to-typography`
55+
56+
#### Changes
57+
58+
```diff
59+
import {
60+
- Text,
61+
- TextProps,
62+
- TextTypes,
63+
- TextRenderFunction,
64+
- TextElement,
65+
+ Typography,
66+
+ TypographyProps,
67+
+ TypographyType,
68+
+ TypographyRenderFunction,
69+
+ TypographyHTMLElement,
70+
TextContainer,
71+
} from "@react-md/typography";
72+
73+
-const renderer: TextRenderFunction = ({ className }) => (
74+
+const renderer: TypographyRenderFunction = ({ className }) => (
75+
<div className={className} />
76+
);
77+
78+
-const types: TextTypes[] = [
79+
+const types: TypographyType[] = [
80+
"headline-1",
81+
"headline-2",
82+
"headline-3",
83+
@@ -27,17 +27,15 @@ const types: TextTypes[] = [
84+
"button",
85+
];
86+
87+
-const props: TextProps = {};
88+
-let element: TextElement;
89+
+const props: TypographyProps = {};
90+
+let element: TypographyHTMLElement;
91+
92+
export default function Example() {
93+
- return (
94+
- <>
95+
- <Text>Hello</Text>
96+
+ return <>
97+
+ <Typography>Hello</Typography>
98+
<TextContainer>
99+
- <Text>World!</Text>
100+
- <Text type="headline-1">Headline</Text>
101+
+ <Typography>World!</Typography>
102+
+ <Typography type="headline-1">Headline</Typography>
103+
</TextContainer>
104+
- </>
105+
- );
106+
+ </>;
107+
}
108+
,
109+
```
110+
111+
```sh
112+
Usage: npx @react-md/codemod v3-to-v4/rename-text-to-typography [options] [files...]
113+
114+
Arguments:
115+
files An optional glob or folder path to transform
116+
(default: ".")
117+
118+
Options:
119+
-d, --dry Dry run (no changes are made to files) (default:
120+
false)
121+
-p, --print Print transformed files to your terminal
122+
(default: false)
123+
--parser <parser> The file parser to use. (choices: "babel",
124+
"tsx", "", default: "")
125+
--jscodeshift <jscodeshift> (Advanced) Pass options directly to jscodeshift
126+
(default: "")
127+
-h, --help display help for command
128+
```
129+
130+
### `v3-to-v4/scale-transition-props`
131+
132+
#### Changes
133+
134+
```diff
135+
136+
export default function Example() {
137+
const [visible, setVisible] = useState(false);
138+
- return (
139+
- <>
140+
+ return <>
141+
<button type="button" onClick={() => setVisible((p) => !p)}>
142+
Toggle
143+
</button>
144+
- <ScaleTransition visible={visible}>
145+
+ <ScaleTransition transitionIn={visible}>
146+
<div>Something</div>
147+
</ScaleTransition>
148+
- </>
149+
- );
150+
+ </>;
151+
}
152+
,
153+
```
154+
155+
```sh
156+
Usage: npx @react-md/codemod v3-to-v4/scale-transition-props [options] [files...]
157+
158+
Arguments:
159+
files An optional glob or folder path to transform
160+
(default: ".")
161+
162+
Options:
163+
-d, --dry Dry run (no changes are made to files) (default:
164+
false)
165+
-p, --print Print transformed files to your terminal
166+
(default: false)
167+
--parser <parser> The file parser to use. (choices: "babel",
168+
"tsx", "", default: "")
169+
--jscodeshift <jscodeshift> (Advanced) Pass options directly to jscodeshift
170+
(default: "")
171+
-h, --help display help for command
172+
```

packages/codemod/package.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@react-md/codemod",
3+
"version": "3.1.1",
4+
"description": "ReactMD codemod scripts",
5+
"author": "Mikkel Laursen <mlaursen03@gmail.com>",
6+
"bin": {
7+
"rmd-codemod": "./bin/rmd-codemod.js"
8+
},
9+
"files": [
10+
"bin"
11+
],
12+
"scripts": {
13+
"update-docs": "node updateDocs.js",
14+
"build": "tsc --project tsconfig.build.json"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "https://github.com/mlaursen/react-md.git",
19+
"directory": "packages/codemod"
20+
},
21+
"bugs": {
22+
"url": "https://github.com/mlaursen/react-md/issues"
23+
},
24+
"homepage": "https://react-md.dev",
25+
"keywords": [
26+
"react-md",
27+
"material design",
28+
"react",
29+
"codemod",
30+
"jscodeshift",
31+
"transforms"
32+
],
33+
"license": "MIT",
34+
"dependencies": {
35+
"commander": "^8.3.0",
36+
"glob": "^7.2.0",
37+
"inquirer": "^8.2.0",
38+
"jscodeshift": "^0.13.0"
39+
},
40+
"devDependencies": {
41+
"@types/glob": "^7.2.0",
42+
"@types/inquirer": "^8.1.3",
43+
"@types/jscodeshift": "^0.11.3",
44+
"typescript": "^4.5.2"
45+
},
46+
"publishConfig": {
47+
"access": "public"
48+
}
49+
}

0 commit comments

Comments
 (0)