npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

relative-to-alias

v2.0.1

Published

A tiny cli tool to convert relative path to specified alias.

Downloads

44

Readme

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:
  --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"]
  --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

:star: 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.