Easily refactor imports in TypeScript files via the CLI.
npm install refactor-imports -g
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]
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
import { foo, bar } from 'my-old-lib';
// after
import { foo, bar } from 'my-new-lib';
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
import { foo } from 'my-old-lib';
import { bar } from 'my-even-older-lib';
// after
import { foo, bar } from 'my-new-lib';
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
import { foo, bar, baz } from 'my-old-lib';
// after
import { baz } from 'my-old-lib';
import { foo, bar } from 'my-new-lib';
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
import { foo } from '@my-libs/old';
import { bar } from '@my-libs/even-older';
// after
import { foo, bar } from '@my-libs/new';
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!