-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add codemods: CommonJS -> ES2015 module (#12520)
* 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
Showing
8 changed files
with
699 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
Oops, something went wrong.