Skip to content

Commit 82b8133

Browse files
authored
Add cli to run transforms using the codemods package (#2189)
* Build cli for codemod package * Make the thing actually work * Change files * Update readme * Update ignore path * Fix cli * Include new codemod * consistent slashes * Fix depcheck * Remove unnecessary change file * Add exe bit
1 parent b165a81 commit 82b8133

17 files changed

+72
-14
lines changed

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#Ignore transforms test output files
2-
**/src/__testfixtures__/*.output.tsx
2+
**/__testfixtures__/*.output.tsx
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "Build cli for codemod package",
4+
"packageName": "@fluentui-react-native/codemods",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

change/@fluentui-react-native-platform-utils-37f27f7c-d8ed-4901-b8ba-a6e8cd9bb892.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/codemods/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ This package supplies transforms that help with refactoring FURN code. The trans
88
2. Run the transforms using the following command:
99

1010
```cli
11-
jscodeshift -t <path to transform file> --parser=tsx --extensions=tsx <path to file to be transformed>
11+
npx -p @fluentui-react-native/codemods transform -t <transform_name> --path <path_to_files_to_transform>
1212
```
1313

1414
For example
1515

1616
```cli
17-
jscodeshift -t transforms/src/button-v0-to-v1.ts --parser=tsx --extensions=tsx apps/fluent-tester/src/TestComponents/Button/deprecated
17+
npx -p @fluentui-react-native/codemods transform -t button-v0-to-v1 --path .\apps\fluent-tester\src\TestComponents\Button\deprecated\ButtonFocusTest.tsx
1818
```
1919

2020
## Tests

packages/codemods/package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
"main": "src/index.ts",
99
"module": "src/index.ts",
1010
"typings": "lib/index.d.ts",
11+
"bin": {
12+
"transform": "./transform.js"
13+
},
1114
"scripts": {
15+
"build": "fluentui-scripts build",
1216
"just": "fluentui-scripts",
1317
"clean": "fluentui-scripts clean",
1418
"depcheck": "fluentui-scripts depcheck",
@@ -22,16 +26,22 @@
2226
"url": "https://github.com/microsoft/fluentui-react-native.git",
2327
"directory": "packages/codemods"
2428
},
29+
"dependencies": {
30+
"jscodeshift": "^0.13.1",
31+
"yargs": "^17.0.0"
32+
},
2533
"devDependencies": {
2634
"@fluentui-react-native/eslint-config-rules": "^0.1.1",
2735
"@fluentui-react-native/scripts": "^0.1.1",
2836
"@fluentui-react-native/test-tools": ">=0.1.1 <1.0.0",
29-
"@types/jscodeshift": "^0.11.5",
30-
"jscodeshift": "^0.13.1"
37+
"@types/jscodeshift": "^0.11.5"
3138
},
3239
"depcheck": {
40+
"ignoreMatches": [
41+
".bin"
42+
],
3343
"ignorePatterns": [
34-
"src/__testfixtures__/*"
44+
"src/transforms/__testfixtures__/*"
3545
]
3646
}
3747
}

packages/codemods/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { transform, yargsParse } from './transform';
2+
3+
transform(yargsParse(process.argv));

packages/codemods/src/transform.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import path from 'path';
2+
import yargs from 'yargs';
3+
import { execSync } from 'child_process';
4+
5+
const transformerDirectory = path.join(__dirname, 'transforms');
6+
const jscodeshiftExecutable = require.resolve('.bin/jscodeshift');
7+
8+
interface argsType {
9+
path: string;
10+
transform: string;
11+
}
12+
13+
export const yargsParse = (args: string[]): argsType => {
14+
return yargs([])
15+
.help()
16+
.exitProcess(false)
17+
.option('path', {
18+
alias: 'p',
19+
type: 'string',
20+
description: 'Path that transform should be run over',
21+
normalize: true,
22+
})
23+
.option('transform', {
24+
alias: 't',
25+
type: 'string',
26+
description: 'Name of transform to run',
27+
choices: ['button-v0-to-v1', 'deprecate-exports'],
28+
})
29+
.demandOption(['path', 'transform'])
30+
.parse(args);
31+
};
32+
33+
export const transform = (args: argsType) => {
34+
const codeshiftArgs = [];
35+
36+
codeshiftArgs.push('-t', path.join(transformerDirectory, args.transform + '.js'));
37+
codeshiftArgs.push('--parser=tsx');
38+
codeshiftArgs.push('--extensions=tsx');
39+
codeshiftArgs.push(args.path);
40+
41+
execSync(jscodeshiftExecutable + ' ' + codeshiftArgs.join(' '));
42+
};

0 commit comments

Comments
 (0)