Giter Site home page Giter Site logo

codemod-cli's Introduction

codemod-cli

codemod-cli is a command line tool for generating, testing, and publishing codemods.

Installation

npm install --global codemod-cli

# OR

yarn global add codemod-cli

Usage

The codemod-cli workflow is focused on managing a group of codemods.

To get started you first need a project. You can generate a new codemod-cli project via:

codemod-cli new <project-name>

This will create a small project structure (README.md, package.json, etc) which is ready to help you manage your codemods.

Once you have a project, you can generate a new codemod:

codemod-cli generate codemod <name of codemod>

This will setup a new jscodeshift codemod within your project at transforms/<name of codemod>/index.js along with a test harness, README, fixture directory, and an initial set of input/output fixtures.

Once you have tweaked your codemod and its fixtures to your liking, it is time to run your tests:

codemod-cli test

As you develop your codemod you may need additional fixtures (e.g. to test various combinations of inputs). To generate a new fixture, run the following:

codemod-cli generate fixture <name of codemod> <name of fixture>

This sets up two new files in transforms/<name of codemod>/__testfixtures__/ using the fixture name you provided. These fixtures are used by the testing harness to verify that your codemod is working properly.

Once you have things just how you like them with your new codemod (and your tests are passing ๐Ÿ˜‰) you can update your project's README and your transforms README via:

codemod-cli update-docs

File Types

By default the bin script that is generated for your codemod-cli project will run against .js and .ts files. If you'd like to change that (e.g. to run against .hbs or .jsx files) you can tweak your projects bin/cli.js script to add --extensions=hbs,jsx:

#!/usr/bin/env node
'use strict';

require('codemod-cli').runTransform(
  __dirname,
  process.argv[2],       /* transform name */,
  process.argv.slice(3), /* paths or globs */
  'hbs,jsx'
)

Debugging Workflow

Oftentimes, you want to debug the codemod or the transform to identify issues with the code or to understand how the transforms are working, or to troubleshoot why some tests are failing.

Hence we recommend a debugging work-flow like below to quickly find out what is causing the issue.

1. Place debugger statements

Add debugger statements, in appropriate places in the code. For example:

...
const params = a.value.params.map(p => {
  debugger;
  if(p.type === "SubExpression") {
    return transformNestedSubExpression(p)
...

2. Inspect the process with node debug

Here we are going to start the tests selectively in node debug mode. Since the codemod is bootstrapped using codemod-cli which is using jest in turn to run the tests, jest is having an option -t <name-of-spec> to run a particular set of tests instead of running the whole test suite.

We are making use of both these features to start our tests in this particular fashion. For more details on node debug, visit the official Node.js debugging guide, and for jest documentation on tests, here

node --inspect-brk ./node_modules/.bin/codemod-cli test -t '<fixture-name>'

For example, if you want to debug the null-subexp.input.hbs fixture or only that particular test case is failing because of an issue.

node --inspect-brk ./node_modules/.bin/codemod-cli test -t 'null-subexp'

Sometimes we need to use --runInBand flag for the debugger statements to be hit when focusing the test with jest

For example:

node --inspect-brk ./node_modules/.bin/jest --testNamePattern "ember-concurrency transforms correctly" --runInBand

Once you run the above command, your tests will start running in debug mode and your breakpoints will be triggered appropriately when that particular block of code gets executed. You can run the debugger inside Chrome browser dev-tools. More details on here

Contributing

Installation

Linting

  • yarn lint:js
  • yarn lint:js --fix

Running tests

  • yarn test

License

This project is licensed under the MIT License.

codemod-cli's People

Contributors

allthesignals avatar dcyriller avatar dependabot[bot] avatar dogoku avatar eventualbuddha avatar jgwhite avatar jsturgis avatar mfeckie avatar nlfurniss avatar nullvoxpopuli avatar rajasegar avatar ro0gr avatar rondale-sc avatar rwjblue avatar simonihmig avatar

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.