Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jscodeshift-transport

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jscodeshift-transport

jscodeshift transform and API for finding and replacing module names in import/require statements

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by33.33%
Maintainers
1
Weekly downloads
 
Created
Source

jscodeshift-transport

Build Status Coverage Status semantic-release Commitizen friendly

A great jscodeshift transform and API for finding and replacing module names in import/require statements. I wrote this because other transforms I found seemed not as convenient or comprehensive. This handles both relative imports/requires and imports/requires from node_modules. For instance, it's easy to replace a component from a library with your own wrapper in a local file:

jscodeshift -t ~/jscodeshift-transport ./src \
  --find=@material-ui/core/Button \
  --replace=./src/components/Button

The correct relative path will be used in each replacement, relative to the file being processed. It will work just as well if you swap the --find and --replace values, restoring the original module names.

Requirements

  • Node 8 or greater

requires it's not magic enough to handle

  • scopes where require is redeclared, e.g. requires inside a function (require) { ... } will be ignored, if you're doing that, please stahp
  • computed require paths (any argument besides a string literal)

CLI

Run index.js in this repo with jscodeshift and pass two options to it:

  • --find=<VALUE>: The module name to find, just like you would use in an import or require statement, relative to the current working directory: Paths to local files must be absolute or begin with ../ or ./; otherwise, it is assumed you meant an import from node_modules.
  • --replace=<VALUE>: The module name to replace it with, just like you would use in an import or require statement, relative to the current working directory: Paths to local files must be absolute or begin with ../ or ./; otherwise, it is assumed you meant an import from node_modules.
npm i -g jscodeshift
cd ~
git clone https://github.com/jcoreio/jscodeshift-transport
cd ~/jscodeshift-transport
npm i
cd ~/path/to/your/project
jscodeshift -t ~/jscodeshift-transport ./src \
  --find=@material-ui/core/Button \
  --replace=./src/components/Button

API

jscodeshift-transport exports a replaceModuleNames function you can use in your own jscodeshift transforms.

const {replaceModuleNames} = require('jscodeshift-transport')

It accepts the following arguments:

file: string

The path to the file being transformed

root: Collection

The root jscodeshift Collection from the source code of file. You must use the babylon parser (e.g. require('jscodeshift').withParser('babylon')(code))

find: string

The module name to find in import/require statements. Paths starting with ./ or ../ are treated as relative to the current working directory, not file.

replace: string | (ReplaceOptions) => ?string

The module name to replace find with in import/require statements, or a function that computes the replacement. Paths starting with ./ or ../ are treated as relative to the current working directory, not file. If you pass a function, it is called with a ReplacementOptions object containing the following properties, and may return a string replacement module name.

  • moduleName: string: The module name in an import/require statement to replace. It may differ from find if it is a relative path from a different directory to the same file.
  • file: string: The file you passed to replaceSources.
  • path: NodePath: The babel NodePath for the import or require statement.

Keywords

FAQs

Package last updated on 15 Nov 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc