Skip to content

Commit

Permalink
Add codemods: CommonJS -> ES2015 module (#12520)
Browse files Browse the repository at this point in the history
* Framework: Add codemod dependencies (#12520)

* Framework: Add codemod config and helpers (#12520)

* Framework: Add CommonJS import codemods (#12520)

* Framework: Add CommonJS export codemod (#12520)

* Framework: Add named export codemod (#12520)

* Framework: Generate new shrinkwrap (#12520)
  • Loading branch information
jsnmoon authored and ehg committed Apr 18, 2017
1 parent 04b5a5f commit 4832b8e
Show file tree
Hide file tree
Showing 8 changed files with 699 additions and 53 deletions.
41 changes: 41 additions & 0 deletions bin/codemods/commonjs-exports
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env node

/*
This codemod converts `module.exports` to `export` and `export default`.
How to use:
./bin/codemods/commonjs-exports path-to-transform/
*/

/**
* External dependencies
*/
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require( './config' );
const helpers = require( './helpers' );

const args = process.argv.slice( 2 );
if ( args.length === 0 ) {
process.stdout.write( 'No files to transform\n' );
process.exit( 0 );
}

const binArgs = [
// jscodeshift options
'--transform=node_modules/5to6-codemod/transforms/exports.js',
...config.jscodeshiftArgs,

// Recast options via 5to6
...config.recastArgs,

// Transform target
args[ 0 ],
];
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' );
const jscodeshift = child_process.spawn( binPath, binArgs );
helpers.bindEvents( jscodeshift );
43 changes: 43 additions & 0 deletions bin/codemods/commonjs-imports
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

/*
This codemod converts occurrences of `require( '...' )` to `import ... from '...'`
only if they occurr on the top level scope. CommonJS imports inside block statements,
like conditionals or function definitions, will not be converted.
How to use:
./bin/codemods/commonjs-imports path-to-transform/
*/

/**
* External dependencies
*/
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require( './config' );
const helpers = require( './helpers' );

const args = process.argv.slice( 2 );
if ( args.length === 0 ) {
process.stdout.write( 'No files to transform\n' );
process.exit( 0 );
}

const binArgs = [
// jscodeshift options
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
...config.jscodeshiftArgs,

// Recast options via 5to6
...config.recastArgs,

// Transform target
args[ 0 ],
];
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' );
const jscodeshift = child_process.spawn( binPath, binArgs );
helpers.bindEvents( jscodeshift );
45 changes: 45 additions & 0 deletions bin/codemods/commonjs-imports-hoist
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node

/*
This codemod hoists all occurrences of `require( '...' )` inside if, loop, and
function blocks. This can cause breakage! Use with caution.
How to use:
./bin/codemods/commonjs-imports path-to-transform/
*/

/**
* External dependencies
*/
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require( './config' );
const helpers = require( './helpers' );

const args = process.argv.slice( 2 );
if ( args.length === 0 ) {
process.stdout.write( 'No files to transform\n' );
process.exit( 0 );
}

const binArgs = [
// jscodeshift options
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
...config.jscodeshiftArgs,

// Recast options via 5to6
...config.recastArgs,

// 5to6 transform options
'--hoist=true',

// Transform target
args[ 0 ],
];
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' );
const jscodeshift = child_process.spawn( binPath, binArgs );
helpers.bindEvents( jscodeshift );
14 changes: 14 additions & 0 deletions bin/codemods/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const jscodeshiftArgs = [
'--extensions=js,jsx',
];

// Used primarily by 5to6-codemod transformations
const recastArgs = [
'--useTabs=true',
'--arrayBracketSpacing=true',
];

module.exports = {
jscodeshiftArgs,
recastArgs,
};
13 changes: 13 additions & 0 deletions bin/codemods/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function bindEvents( jscodeshiftProcess ) {
jscodeshiftProcess.stdout.on( 'data', ( data ) => {
process.stdout.write( data );
} );

jscodeshiftProcess.stderr.on( 'data', ( data ) => {
process.stderr.write( data );
} );
}

module.exports = {
bindEvents,
};
43 changes: 43 additions & 0 deletions bin/codemods/named-exports-from-default
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env node

/*
This codemod generates named exports given a `default export { ... }`.
This can be useful in transitioning away from namespace imports
(`import * as blah from 'blah'`) to named imports (`import named from 'blah'`).
How to use:
./bin/codemods/named-export-from-default path-to-transform/
*/

/**
* External dependencies
*/
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require( './config' );
const helpers = require( './helpers' );

const args = process.argv.slice( 2 );
if ( args.length === 0 ) {
process.stdout.write( 'No files to transform\n' );
process.exit( 0 );
}

const binArgs = [
// jscodeshift options
'--transform=node_modules/5to6-codemod/transforms/named-export-generation.js',
...config.jscodeshiftArgs,

// Recast options via 5to6
...config.recastArgs,

// Transform target
args[ 0 ],
];
const binPath = path.join( '.', 'node_modules', '.bin', 'jscodeshift' );
const jscodeshift = child_process.spawn( binPath, binArgs );
helpers.bindEvents( jscodeshift );
Loading

0 comments on commit 4832b8e

Please sign in to comment.