Programmatic
Explains how to run codemod-kit using the programmatic method.
Installation
install the codemod-kit
- npm
- yarn
- pnpm
npm install --save-dev codemod-kit
yarn add -D codemod-kit
pnpm add -D codemod-kit
Usage
type TransformMap = Record<
string,
(
toConvertPaths: string | string[],
customOptions: Option,
jscodeshiftOptions?: { parser: string }
) => void
>;
Select the type to use in transformMap imported from codemod-kit/programmatic, and then run the function with appropriate options. The options are as follows:
toConvertPaths
Enter the file to be converted as a string or array.
customOptions
These are the options required by the selected type of transformer. Please refer to each transformer document for the options required by type.
jscodeshiftOptions
Options provided by jscodeshift. The currently allowed option is parser and the default is tsx.
Example
Let's say we want to perform a transformation like the example below.
- before
- after
import { sum } from "utils"
console.log(sum(2,1))
import { calculateSum } from "utils"
console.log(calculateSum(2,1))
You can convert the code by excute below code
import { transformMap } from "codemod-kit/programmatic"
transformMap['add-props']({
toConvertPaths:"src/test.ts"
customOptions:{
source: "utils",
sourceType: "absolute",
fromSpecifier: "sum",
toSpecifier: "calculateSum",
},
jscodeshiftOptions:{
parser:"tsx"
}
})