Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codemods: CommonJS -> ES2015 module #12520

Merged
merged 6 commits into from
Apr 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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