docs(devkit): example of jscodeshift codemod (#7976)
Co-authored-by: Isaac Mann <isaacplmann+git@gmail.com>
This commit is contained in:
parent
fcab9f2001
commit
99164baaa0
@ -16,3 +16,27 @@ export default async function (tree: Tree, schema: any) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Using jscodeshift Codemods
|
||||||
|
|
||||||
|
Codemods created for use with [`jscodeshift`](https://github.com/facebook/jscodeshift) can be used within Nx Devkit generators using the `visitNotIgnoredFiles` helper function. This way you can compose codemods with other generators while retaining `--dry-run` and Nx Console compatibilities.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Tree, visitNotIgnoredFiles } from '@nrwl/devkit';
|
||||||
|
import { applyTransform } from 'jscodeshift/src/testUtils';
|
||||||
|
import arrowFunctionsTransform from './arrow-functions';
|
||||||
|
|
||||||
|
// The schema path can be an individual file or a directory
|
||||||
|
export default async function (tree: Tree, schema: { path: string }): any {
|
||||||
|
visitNotIgnoredFiles(tree, schema.path, (filePath) => {
|
||||||
|
const input = tree.read(filePath).toString();
|
||||||
|
const transformOptions = {};
|
||||||
|
const output = applyTransform(
|
||||||
|
{ default: arrowFunctionsTransform, parser: 'ts' },
|
||||||
|
transformOptions,
|
||||||
|
{ source: input, path: filePath }
|
||||||
|
);
|
||||||
|
tree.write(filePath, output);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user