Installation
You can run codemods without installation using npx:Usage
Interactive Mode
Run the codemod in interactive mode to be prompted for options:- Which files or directory to transform
- Which dialect of JavaScript you use (JavaScript or TypeScript)
- Which transformation to apply
Command Line Mode
Run a specific transformation directly:transform- Name of the transformation to applypath- Files or directory to transform (can be a glob pattern)
Available Transformations
barreled-to-unbarreled
Converts barreled imports from@twilio-paste/core to direct package imports.
Why use this?
Importing directly from package sources instead of the barrel export (@twilio-paste/core) provides:
- Better tree-shaking and smaller bundle sizes
- Faster build times
- More explicit dependencies
Options
—force
Bypass Git safety checks and forcibly run codemods.--force to skip this check.
—dry
Run in dry-run mode (no changes are made to files).—explicit-require
Transform only files where React is explicitly imported.—jscodeshift
Pass advanced options directly to jscodeshift.Using with Different File Types
JavaScript Projects
For JavaScript projects (.js, .jsx files):TypeScript Projects
For TypeScript projects (.ts, .tsx files):Glob Patterns
You can use glob patterns to target specific files:Workflow Recommendations
1. Ensure Clean Git State
Before running codemods, commit or stash your changes:2. Run in Dry Mode First
Preview changes without modifying files:3. Apply the Transformation
Run the codemod on your codebase:4. Review Changes
Review the changes made by the codemod:5. Test Your Application
Run your tests to ensure nothing broke:6. Commit the Changes
If everything looks good, commit the changes:Common Use Cases
Migrating a Large Codebase
For large codebases, process files in batches:CI/CD Integration
You can integrate codemods into your CI/CD pipeline to check for outdated patterns:Monorepo Usage
For monorepos, run codemods on each package:Troubleshooting
Git Directory Not Clean
Error:--force flag:
No Files Found
Error:Transformation Failed
If a transformation fails on specific files:- Check the console output for error messages
- Try running on a smaller subset of files
- Check if the files have syntax errors
- Ensure you’re using the correct parser (JavaScript vs TypeScript)
Partial Transformations
Some imports might not be transformed if:- The component isn’t exported from
@twilio-paste/core - The import uses a non-standard format
- The import is from a subpath (e.g.,
@twilio-paste/core/dist)
Best Practices
Do
- Always run codemods on a clean Git working directory
- Use
--drymode first to preview changes - Run your test suite after applying transformations
- Review the diff before committing
- Apply codemods to your entire codebase at once for consistency
Don’t
- Don’t run codemods on production code without testing
- Don’t skip the dry-run step for large transformations
- Don’t ignore warnings or errors in the output
- Don’t apply codemods selectively across your codebase (this can cause inconsistencies)
Under the Hood
Paste codemods use:- jscodeshift: A toolkit for running codemods over JavaScript/TypeScript codebases
- AST transformations: Parses code into an Abstract Syntax Tree, transforms it, and generates new code
- Babel parser: For JavaScript files
- TypeScript parser: For TypeScript files
Version
Current version: 0.17.1Contributing
To contribute new codemods or report issues:- Visit the Paste GitHub repository
- Create an issue describing the transformation needed
- Submit a pull request with your codemod implementation