Skip to content

luchsamapparat/refactor-imports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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]
  -p, --path                                                          [required]
  -s, --current-import-sources                                [array] [required]
  -t, --target-import-source
  -r, --resolve-import-mapping                        [boolean] [default: false]
      --tsconfig-path, --tsc
  -e, --only-imported-exports                              [array] [default: []]
  -d, --dry-run                                       [boolean] [default: false]
  -f, --fuzzy-match                                   [boolean] [default: false]
      --parser                           [choices: "ts", "tsx"] [default: "tsx"]

Required Arguments

You need to pass the path (-p) and the current-import-sources (-s). Additionally, you must provide either target-import-source (-t) OR both resolve-import-mapping (-r) and tsconfig-path (-tsc).

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';

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
import { foo } from 'my-old-lib';
import { bar } from 'my-even-older-lib';

// after
import { foo, bar } from 'my-new-lib';

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
import { foo, bar, baz } from 'my-old-lib';

// after
import { baz } from 'my-old-lib';
import { foo, bar } from 'my-new-lib';

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
import { foo } from '@my-libs/old';
import { bar } from '@my-libs/even-older';

// after
import { foo, bar } from '@my-libs/new';

Resolve Import Mappings from TypeScript Config

You can automatically resolve path aliases defined in your tsconfig.json to relative paths by using the resolve-import-mapping (-r) option along with tsconfig-path (-tsc).

refactor-imports -p ./src -s "@my-app" -r -tsc ./tsconfig.json

Given a tsconfig.json with path mappings like:

{
  "compilerOptions": {
    "paths": {
      "@my-app/*": ["./src/*"]
    }
  }
}
// before
import { foo } from '@my-app/utils/helpers';

// after
import { foo } from './utils/helpers'; // or '../utils/helpers' depending on file location

Parser Option

By default, the tool uses the tsx parser to support JSX syntax. If you're working with plain TypeScript files without JSX, you can use the ts parser.

refactor-imports -p ./src -s "my-old-lib" -t "my-new-lib" --parser ts

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!

About

Easily refactor imports in TypeScript files via the CLI.

Resources

Stars

Watchers

Forks

Packages

No packages published