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
This commit is contained in:
Ruriko Araki 2023-01-03 17:39:23 -08:00 коммит произвёл GitHub
Родитель b165a81f18
Коммит 82b813327d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
17 изменённых файлов: 72 добавлений и 14 удалений

Просмотреть файл

@ -1,2 +1,2 @@
#Ignore transforms test output files
**/src/__testfixtures__/*.output.tsx
**/__testfixtures__/*.output.tsx

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Build cli for codemod package",
"packageName": "@fluentui-react-native/codemods",
"email": "ruaraki@microsoft.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -1,7 +0,0 @@
{
"type": "patch",
"comment": "additional of isMobile utils",
"packageName": "@fluentui-react-native/platform-utils",
"email": "rohanpd.work@gmail.com",
"dependentChangeType": "patch"
}

Просмотреть файл

@ -8,13 +8,13 @@ This package supplies transforms that help with refactoring FURN code. The trans
2. Run the transforms using the following command:
```cli
jscodeshift -t <path to transform file> --parser=tsx --extensions=tsx <path to file to be transformed>
npx -p @fluentui-react-native/codemods transform -t <transform_name> --path <path_to_files_to_transform>
```
For example
```cli
jscodeshift -t transforms/src/button-v0-to-v1.ts --parser=tsx --extensions=tsx apps/fluent-tester/src/TestComponents/Button/deprecated
npx -p @fluentui-react-native/codemods transform -t button-v0-to-v1 --path .\apps\fluent-tester\src\TestComponents\Button\deprecated\ButtonFocusTest.tsx
```
## Tests

Просмотреть файл

@ -8,7 +8,11 @@
"main": "src/index.ts",
"module": "src/index.ts",
"typings": "lib/index.d.ts",
"bin": {
"transform": "./transform.js"
},
"scripts": {
"build": "fluentui-scripts build",
"just": "fluentui-scripts",
"clean": "fluentui-scripts clean",
"depcheck": "fluentui-scripts depcheck",
@ -22,16 +26,22 @@
"url": "https://github.com/microsoft/fluentui-react-native.git",
"directory": "packages/codemods"
},
"dependencies": {
"jscodeshift": "^0.13.1",
"yargs": "^17.0.0"
},
"devDependencies": {
"@fluentui-react-native/eslint-config-rules": "^0.1.1",
"@fluentui-react-native/scripts": "^0.1.1",
"@fluentui-react-native/test-tools": ">=0.1.1 <1.0.0",
"@types/jscodeshift": "^0.11.5",
"jscodeshift": "^0.13.1"
"@types/jscodeshift": "^0.11.5"
},
"depcheck": {
"ignoreMatches": [
".bin"
],
"ignorePatterns": [
"src/__testfixtures__/*"
"src/transforms/__testfixtures__/*"
]
}
}

Просмотреть файл

@ -0,0 +1,3 @@
import { transform, yargsParse } from './transform';
transform(yargsParse(process.argv));

Просмотреть файл

@ -0,0 +1,42 @@
import path from 'path';
import yargs from 'yargs';
import { execSync } from 'child_process';
const transformerDirectory = path.join(__dirname, 'transforms');
const jscodeshiftExecutable = require.resolve('.bin/jscodeshift');
interface argsType {
path: string;
transform: string;
}
export const yargsParse = (args: string[]): argsType => {
return yargs([])
.help()
.exitProcess(false)
.option('path', {
alias: 'p',
type: 'string',
description: 'Path that transform should be run over',
normalize: true,
})
.option('transform', {
alias: 't',
type: 'string',
description: 'Name of transform to run',
choices: ['button-v0-to-v1', 'deprecate-exports'],
})
.demandOption(['path', 'transform'])
.parse(args);
};
export const transform = (args: argsType) => {
const codeshiftArgs = [];
codeshiftArgs.push('-t', path.join(transformerDirectory, args.transform + '.js'));
codeshiftArgs.push('--parser=tsx');
codeshiftArgs.push('--extensions=tsx');
codeshiftArgs.push(args.path);
execSync(jscodeshiftExecutable + ' ' + codeshiftArgs.join(' '));
};

3
packages/codemods/transform.js Executable file
Просмотреть файл

@ -0,0 +1,3 @@
#!/usr/bin/env node
require('./lib-commonjs/index');

Просмотреть файл

@ -5,5 +5,5 @@
"types": ["node", "jest"]
},
"include": ["src"],
"exclude": ["src/__testfixtures__"]
"exclude": ["src/transforms/__testfixtures__"]
}