Coder Social home page Coder Social logo

mvjs's Introduction

mvjs logo

Easily move files and directories around your JavaScript project.

mvjs ยท GitHub license npm version

Easily move files and directories around your JavaScript project.

The problem

Moving JavaScript modules inside a project is generally a tedious task. After you move it using mv, you have to update their references on the other modules inside your project.

mvjs moves the module and also updates all its references on the other modules making the task of moving modules much simpler, similarly to using mv.

It does this by running a codemod on all JavaScript modules inside the project (with the help of jscodeshift) and smartly updating import declarations and require calls.

For non-js module files, a regular expression runs on their content, replacing any string that looks like a path and matches any of the moved files will be properly replaced. This gives extra piece of mind when moving .scss and other file extensions that can potentially import one of the moved files.

Features

  • Supports and updates import _ from '...', import('...') and require('...')
  • Updates files using JavaScript, TypeScript or Flow
  • Does its best to also update non-js files
  • Moves files or directories
  • Same api and simplicity of the mv command
  • Shows easy to understand errors when unexpected things happen
  • Uses DEBUG environment variable to show extra dbug information. Ex: DEBUB=* mvjs ./a.js ./b.js

Install

npm install -g @fabiomcosta/mvjs

This makes mvjs available globally.

CLI Usage

$ mvjs --help
mvjs - moves a JavaScript module and updates all import references in the project.

Options:
  --version         Show version number                                                                        [boolean]
  --parser          jscodeshift's parser option.
                    See https://github.com/facebook/jscodeshift#parser                        [string] [default: "flow"]
  --ignore-pattern  Pattern of files that won't have their file references updated                              [string]
  --help            Show help                                                                                  [boolean]

Examples:
  cli.js ./a.js ./b.js                                          Moves "a.js" to "b.js" and updates the other modules
                                                                importing "a.js" to now import "b.js".
  cli.js --recast.quote='double' ./a.js ./b.js                  Recast options can be changed by using
                                                                --recast.optionName notation.
                                                                In this example the codemoded files are going to have
                                                                double quotes for all strings.
                                                                See https://github.com/benjamn/recast/blob/master/lib/op
                                                                tions.ts
  cli.js --ignore-pattern='*.d.ts'                              Ignore patterns with the familiar gitignore syntax
  --ignore-pattern='*.js.flow' ./a.js ./b.js                    defined at https://git-scm.com/docs/gitignore
                                                                Multiple patterns can be ignored by providing multiple
                                                                options.

Example

Consider the following folder structure for a project:

root/
โ”‚   package.json
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ common/
    โ”‚   โ””โ”€โ”€ config.js
    โ””โ”€โ”€ client/
        โ”‚   paths-client.js
        โ””โ”€โ”€ files-client.js

And the following file content for ./src/client/paths-client.js:

import files from './files-client';
// or const files = require('./files-client');

Let's move the ./src/client/files-client.js module inside the ./src/common/ folder:

mvjs ./src/client/files-client.js ./src/common/files.js

This will make sure that all files that had a reference to this module are also going to be properly updated, which means that paths-client.js will be updated to:

import files from '../common/files';
// or const files = require('../common/files');

API Usage

move (and ONLY move) modules:

import {move} from '@fabiomcosta/mvjs';

(async () => {
  await move({
    sourcePaths: ['./foo.js', './bar.mjs'],
    targePath: './baz'
  });
})();

codemod the import statements from the current project modules:

import {transform} from '@fabiomcosta/mvjs';

(async () => {
  await transform({
    sourcePaths: ['./foo.js', './bar.mjs'],
    targePath: './baz',
    parser: 'flow', // optional
    recastOptions: {quotes: 'single'} // optional
  });
})();

TODO

  • move .jsx, .mjs, .es, .es6
  • move multiple sources to a directory
  • move a directory to another directory
  • move any file extension (keep updating the references only from .js, .jsx, .mjs, .es, .es6 files)
  • Update references on other types of files, like CSS, SASS, LESS etc.
  • Optionaly rename imported default
  • Add option to allow other function calls to be replaced

mvjs's People

Contributors

dependabot[bot] avatar fabiomcosta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mvjs's Issues

Use `git` or `hg` to search for the project files

We are currently searching for the project files ourselves, with pure Node fs functions.
This is fine, but we end up changing more files than we should.
If we used git to search for the existing files, it will already consider the .gitignore definitions, which generally excludes node_modules (which we currently already exclude in a hard coded way) and excludes folders that contains auto-generated code (generally babel transpiled code), that we also don't want to update.

Add option to format with prettier

First, awesome project, works like a charm, although I think the latest version is not published to npm yet.

Second, it would be great if it could format with prettier after applying transforms, it could be behind an option, perhaps `--format=prettier'

Moving a file should be done with either `git` or `hg`

The default behavior for when a project uses git or hg is to want to have the file moved with those tools in order to have the commit changes to look like moving a file.
Let's detect if the project is using one of these tools and use git mv or hg mvto move the files instead of using node'sfs.rename` function.

Support merging directories

When moving a directory if it already exist it throws an error. I would be useful if it merges the directories instead.

Don't throw when imports are not

Saw these errors and they should just be warnings... this tool shouldn't be affected by those, just let the users know via a warning.

INFO: Detected project path: gDev/gd/vnext-editor
Error: File "gDev/gd/vnext-editor/bin/clean" is importing "gDev/gd/vnext-editor/build'));
rimraf.sync(path.resolve(__dirname, '../stats" but it does not exists.
    at requireResolve (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/requireResolve.js:17:13)
    at getAbsoluteImportSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:101:38)
    at updateSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:155:36)
    at content.replace (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/runner.js:51:53)
    at String.replace (<anonymous>)
    at genericTransform (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/runner.js:40:40) ERR gDev/gd/vnext-editor/src/server/config/local/index.js Transformation error (File "gDev/gd/vnext-editor/src/server/config/local/index.js" is importing "gDev/gd/vnext-editor/src/server/config/local/overrides.json" but it does not exists.)
Error: File "gDev/gd/vnext-editor/src/server/config/local/index.js" is importing "gDev/gd/vnext-editor/src/server/config/local/overrides.json" but it does not exists.
    at requireResolve (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/requireResolve.js:17:13)
    at getAbsoluteImportSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:101:38)
    at updateSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:155:36)
    at updateLiteralPath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/ast.js:43:47)
    at updateNodePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/ast.js:53:14)
    at NodePath.transform.find.filter.forEach.path (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/transform.js:31:54)
    at __paths.forEach (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/node_modules/jscodeshift/src/Collection.js:77:36)
    at Array.forEach (<anonymous>)
    at Collection.forEach (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/node_modules/jscodeshift/src/Collection.js:76:18)
    at transformer (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/transform.js:30:93)
 ERR gDev/gd/vnext-editor/src/server/config/local-build/index.js Transformation error (File "gDev/gd/vnext-editor/src/server/config/local-build/index.js" is importing "gDev/gd/vnext-editor/src/server/config/local-build/overrides.json" but it does not exists.)
Error: File "gDev/gd/vnext-editor/src/server/config/local-build/index.js" is importing "gDev/gd/vnext-editor/src/server/config/local-build/overrides.json" but it does not exists.
    at requireResolve (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/requireResolve.js:17:13)
    at getAbsoluteImportSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:101:38)
    at updateSourcePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/path.js:155:36)
    at updateLiteralPath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/ast.js:43:47)
    at updateNodePath (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/ast.js:53:14)
    at NodePath.transform.find.filter.forEach.path (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/transform.js:31:54)
    at __paths.forEach (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/node_modules/jscodeshift/src/Collection.js:77:36)
    at Array.forEach (<anonymous>)
    at Collection.forEach (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/node_modules/jscodeshift/src/Collection.js:76:18)
    at transformer (g.npm/_npx/75097/lib/node_modules/@fabiomcosta/mvjs/lib/transform.js:30:93)

Provide way to ignore some paths

There is currently no way to ignore code paths ATM.
Let's add one. Look at how well known commands do that, like grep, ack and expose it with a similar API.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.