This is a simple wrapper around jscodeshift that give you a migration like interface.
Install in your project: npm install jscodemigrate --save-dev
Generate your first codemod:
jscodemigrate g switch-to-es6-classes
And you're off! Look in codemods/
to see your newly generated jscodemigration.
I would recommended taking a look at the template, and also js-codemod for ideas.
jscodemigrate deps
module.exports = {
// Function to export is called transform
transform: ({file, root, api, options}) => {
// All the variables are passed in an options hash
// Included with the usually is a root variable
// (so that each migration doens't need to reparse the file)
const j = api.jscodeshift;
const {expression, statement, statements} = j.template;
const printOptions = options.printOptions || {};
const didTransform = true;
root.find(j.Identifier).replaceWith(
p => j.identifier(p.node.name.split('').reverse().join(''))
)
// You'll notice a return hash instead of the usual resulting source string
return {
didTransform,
root,
printOptions
};
}
}
{ // These options mostly mirror jscodeshift's command line args
"paths": [ "src/", "tests/", "special/file.js" ], // Paths to search when doing codemods
"extensions": "js,es6" // Comma separated extensions to consider
}
module.exports = {
moduleApiChange: true, // Tell jscodemigrate to pull this into dependencies
paths: [ "tests/" ], // even if your .codemodrc file looks in all your files, this will only run in 'tests/' directory
// Tranform is the only thing that isn't optional
transform: ({file, root, api, options}) => {
...
}
}
- Codeshift and everyone who made it possible
- js-codemod, for the great library of examples