Skip to content

Commit

Permalink
Prep for calypso
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Nov 14, 2019
1 parent 69b805e commit a19972e
Show file tree
Hide file tree
Showing 52 changed files with 850 additions and 11,484 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
node_modules/
!.eslintrc.js
/server/devdocs/search-index.js
/packages/calypso-codemods/tests

# Built packages
/packages/*/dist/
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
"babel-jest": "24.9.0",
"babel-plugin-dynamic-import-node": "2.3.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.24",
"calypso-codemods": "file:./packages/calypso-codemods",
"chai": "4.2.0",
"chai-enzyme": "1.0.0-beta.1",
"check-node-version": "3.3.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/calypso-codemods/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
rules: {
'import/no-nodejs-modules': 'off',
'import/no-extraneous-dependencies': [ 'error', { packageDir: __dirname } ],
},
};
1 change: 0 additions & 1 deletion packages/calypso-codemods/.gitignore

This file was deleted.

6 changes: 0 additions & 6 deletions packages/calypso-codemods/.prettierc

This file was deleted.

4 changes: 0 additions & 4 deletions packages/calypso-codemods/.travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions packages/calypso-codemods/.vscode/settings.json

This file was deleted.

3 changes: 0 additions & 3 deletions packages/calypso-codemods/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Calypso Codemods

[![Build Status](https://travis-ci.org/Automattic/calypso-codemods.svg?branch=master)](https://travis-ci.org/Automattic/calypso-codemods)


## What are codemods?

Code modification scripts, also known as codemods, are transformation scripts that can simultaneously modify multiple files with precision and reliability. Codemods were popularized by [Facebook's engineering team](https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb) and depends greatly on Facebook's [jscodeshift](https://github.com/facebook/jscodeshift) library, which wraps over a library named [recast](https://github.com/benjamn/recast) (author of which is associated with the [Meteor](https://www.meteor.com/) project).
Expand Down
71 changes: 38 additions & 33 deletions packages/calypso-codemods/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,74 @@
/**
* External dependencies
*/
const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
const fs = require( 'fs' );
const path = require( 'path' );
const child_process = require( 'child_process' );

/**
* Internal dependencies
*/
const config = require(path.join(__dirname, "config"));
const transformsDir = path.join(__dirname, "./transforms");
const config = require( path.join( __dirname, 'config' ) );
const transformsDir = path.join( __dirname, './transforms' );

const jscodeshiftBin = require( 'module' )
.createRequireFromPath( require.resolve( 'jscodeshift' ) )
.resolve( require( 'jscodeshift/package.json' ).bin.jscodeshift );

function getLocalCodemodFileNames() {
const jsFiles = fs
.readdirSync(transformsDir)
.filter(filename => filename.endsWith(".js"))
.map(name => path.basename(name, ".js")); // strip path and extension from filename
.readdirSync( transformsDir )
.filter( filename => filename.endsWith( '.js' ) )
.map( name => path.basename( name, '.js' ) ); // strip path and extension from filename

return jsFiles;
}

function getValidCodemodNames() {
return [...getLocalCodemodFileNames(), ...Object.getOwnPropertyNames(config.codemodArgs)]
.map(name => "- " + name)
return [ ...getLocalCodemodFileNames(), ...Object.getOwnPropertyNames( config.codemodArgs ) ]
.map( name => '- ' + name )
.sort();
}

function generateBinArgs(name) {
if (config.codemodArgs.hasOwnProperty(name)) {
function generateBinArgs( name ) {
if ( Object.prototype.hasOwnProperty.call( config.codemodArgs, name ) ) {
// Is the codemod defined in the codemodArgs object?
return config.codemodArgs[name];
return config.codemodArgs[ name ];
}

if (getLocalCodemodFileNames().includes(name)) {
return [`--transform=${transformsDir}/${name}.js`];
if ( getLocalCodemodFileNames().includes( name ) ) {
return [ `--transform=${ transformsDir }/${ name }.js` ];
}

throw new Error(`"${name}" is an unrecognized codemod.`);
throw new Error( `"${ name }" is an unrecognized codemod.` );
}

function runCodemod(codemodName, transformTargets) {
const binArgs = [...config.jscodeshiftArgs, ...generateBinArgs(codemodName), ...transformTargets];
function runCodemod( codemodName, transformTargets ) {
const binArgs = [
...config.jscodeshiftArgs,
...generateBinArgs( codemodName ),
...transformTargets,
];

process.stdout.write(`\nRunning ${codemodName} on ${transformTargets.join(" ")}\n`);
process.stdout.write( `\nRunning ${ codemodName } on ${ transformTargets.join( ' ' ) }\n` );

const binPath = path.join(".", "node_modules", ".bin", "jscodeshift");
const jscodeshift = child_process.spawnSync(binPath, binArgs, {
stdio: ["ignore", process.stdout, process.stderr],
});
child_process.spawnSync( jscodeshiftBin, binArgs, {
stdio: [ 'ignore', process.stdout, process.stderr ],
} );
}

function runCodemodDry(codemodName, filepath) {
function runCodemodDry( codemodName, filepath ) {
const binArgs = [
...config.jscodeshiftArgs,
...generateBinArgs(codemodName),
"--dry",
"--print",
"--silent",
...generateBinArgs( codemodName ),
'--dry',
'--print',
'--silent',
filepath,
];
const binPath = path.join(".", "node_modules", ".bin", "jscodeshift");

const result = child_process.spawnSync(binPath, binArgs, {
stdio: "pipe",
});
const result = child_process.spawnSync( jscodeshiftBin, binArgs, {
stdio: 'pipe',
} );

return result.stdout.toString();
}
Expand Down
18 changes: 10 additions & 8 deletions packages/calypso-codemods/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,39 @@ const commonArgs = {
const codemodArgs = {
'commonjs-exports': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/exports.js',
`--transform=${ require.resolve( '5to6-codemod/transforms/exports.js' ) }`,
],

'commonjs-imports': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
`--transform=${ require.resolve( '5to6-codemod/transforms/cjs.js' ) }`,
],

'commonjs-imports-hoist': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/cjs.js',
`--transform=${ require.resolve( '5to6-codemod/transforms/cjs.js' ) }`,
'--hoist=true',
],

'named-exports-from-default': [
...commonArgs[ '5to6' ],
'--transform=node_modules/5to6-codemod/transforms/named-export-generation.js',
`--transform=${ require.resolve( '5to6-codemod/transforms/named-export-generation.js' ) }`,
],

'react-create-class': [
...commonArgs[ 'react' ],
'--transform=node_modules/react-codemod/transforms/class.js',
...commonArgs.react,
`--transform=${ require.resolve( 'react-codemod/transforms/class.js' ) }`,

// react-codemod options
'--pure-component=true',
'--mixin-module-name="react-pure-render/mixin"', // Your days are numbered, pure-render-mixin!
],

'react-proptypes': [
...commonArgs[ 'react' ],
'--transform=node_modules/react-codemod/transforms/React-PropTypes-to-prop-types.js',
...commonArgs.react,
`--transform=${ require.resolve(
'react-codemod/transforms/React-PropTypes-to-prop-types.js'
) }`,
],
};

Expand Down
25 changes: 8 additions & 17 deletions packages/calypso-codemods/index.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
#!/usr/bin/env node

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

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

function main() {
const args = process.argv.slice(2);
if (args.length === 0 || args.length === 1) {
const args = process.argv.slice( 2 );
if ( args.length === 0 || args.length === 1 ) {
process.stdout.write(
[
'',
'calypso-codemods codemodName[,additionalCodemods…] target1 [additionalTargets…]',
'',
'Valid transformation names:',
api.getValidCodemodNames().join('\n'),
api.getValidCodemodNames().join( '\n' ),
'',
'Example: "calypso-codemods commonjs-imports client/blocks client/devdocs"',
'',
].join('\n')
].join( '\n' )
);

process.exit(0);
process.exit( 0 ); // eslint-disable-line no-process-exit
}

const [names, ...targets] = args;
names.split(',').forEach(codemodName => api.runCodemod(codemodName, targets));
const [ names, ...targets ] = args;
names.split( ',' ).forEach( codemodName => api.runCodemod( codemodName, targets ) );
}

main();
6 changes: 6 additions & 0 deletions packages/calypso-codemods/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
preset: '@automattic/calypso-build',
rootDir: __dirname,
testMatch: [ '<rootDir>/tests/*/codemod.spec.js' ],
setupFiles: [ '<rootDir>/setup-tests.js' ],
};

0 comments on commit a19972e

Please sign in to comment.