Skip to content
master
Go to file
Code

Files

Permalink
Failed to load latest commit information.

README.md

Travis build status

A tiny cli tool (codemod) to replace relative paths to defined alias in your project.

You can use anything to define alias for your files/directory (Aliasing modules) and then use this module to refactor your code to start using those alias instead of relative paths.

Install

npm install -g relative-to-alias

Usage

On your project root directory

relative-to-alias --src ./src --alias utils --alias-path ./src/util

Note: alias-path is relative to root-path argument. while src path is relative to the current directory.

Options:
  --version                           Show version number  [boolean]
  --root-path, -r                     Root path of your project
                                      folder. Your imports /
                                      requires will be resolved
                                      based on this
                                            [string] [default: "./"]
  --src, -s                           Source folder or file in which
                                      you want to run the script
                                                 [string] [required]
  --alias, -a                         Alias for a given path
                                                 [string] [required]
  --alias-path, --ap                  Path which you want to be
                                      replaced with alias
                                                 [string] [required]
  --extensions, -e                    File extensions which has to
                                      be parsed.
                                        [string] [default: "js,jsx"]
  --language, -l                      Typed language the project is
                                      in (flow/typescript).
                                          [string] [default: "flow"]
  --include-alias-path-directory, -i  If true it will replace path
                                      to alias for the alias path
                                      directory.
                                          [boolean] [default: false]
  --ignore                            Exclude given glob paths for
                                      the parsing.
                         [array] [default: ["./**/node_modules/**"]]
  --help                              Show help            [boolean]

Example

Consider this folder directory

|-- src
|   |-- util
|   |   |-- common.js
|   |-- index.js
|   |-- component
|   |   |-- header.js
|   |   |-- body.js
|   |   |-- util
|   |   |   |-- common.js

-- index.js

import {debounce} from './util/common';
/***
 Other code
***/

-- header.js

import {debounce} from '../util/common';
import {hideScrollbar} from './util/common'; //This will not change as its not on alias path

/***
 Other code
***/

-- body.js

const {debounce} = require('../util/common');
/***
 Other code
***/

After compile

-- index.js

import {debounce} from 'utils/common';
/***
 Other code
***/

-- header.js

import {debounce} from 'utils/common';
import {hideScrollbar} from './util/common'; //This will not change as its not on alias path

/***
 Other code
***/

-- body.js

const {debounce} = require('utils/common');
/***
 Other code
***/

Ignoring folders

By default node_modules are excluded from parsing, you may want to override ignore option. You can pass multiple glob patterns space separated.

relative-to-alias --src ./src --alias utils --alias-path ./src/util --ignore node_modules/**/* test/**/*

Note: If you are passing ignore option, you might have to define node_modules pattern again (only if the node_module folder is inside the provided src) as the option overrides the default value.

Aliasing modules

You can use one of the following to define alias for your files/directory in your application.

Like this

this repo

Notes

  • This is a codemod which will replace your source files, so make sure to either backup or commit uncommitted changes before running this tool.
You can’t perform that action at this time.