refactor-imports
Easily refactor imports in TypeScript files via the CLI.
Installation
npm install refactor-imports -g
Usage
Options:
--help Show help [boolean]
--version Show version number [boolean]
--path, -p [required]
--current-import-sources, -s [array] [required]
--target-import-source, -t [required]
--only-imported-exports, -e [array] [default: []]
--dry-run, -d [boolean] [default: false]
--fuzzy-match, -f [boolean] [default: false]
Required Arguments
You need to pass the path
(-p
) to the files that should be refactored, the current-import-sources
(-s
) and the target-import-source
(-t
).
refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib"
// before; // after;
Multiple Current Import Sources
You can specify more than one current import source if you want to merge imported exports from various import sources into one.
refactor-imports -p ./src -s "my-old-lib" "my-even-older-lib" -t "my-new-lib"
// before;; // after;
Refactor only selected Imported Exports
Instead of just replacing whole import sources, you can also move selected imported exports of an import source to a new import source.
refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib" -e "foo" "bar"
// before; // after;;
Fuzzy Matching Import Sources
You can use a regular expressions as current-import-source
when you add the fuzzy-match
(-f
) argument.
refactor-imports -p ./src -s "@my-libs/.+" -t "my-new-lib" -f
// before;; // after;
Acknowledgements
The heavy-lifting of this tool is done by Facebook's jscodeshift and the transform-imports codemod written by suchipi. Thanks for doing the actual work!