Skip to content

Commit

Permalink
Implement transform function using jscodeshift
Browse files Browse the repository at this point in the history
- rename paths to babelRC
- create `writeBabelRCJS` for overwriting `.babelrc.js`
  • Loading branch information
hiroppy committed Feb 28, 2018
1 parent b3b7297 commit 117778e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/bin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const path = require('path');
const { isAcceptedNodeVersion, writePackageJSON, writeBabelRC, writeMochaOpts, installDeps } = require('.');
const globby = require('globby');
const {
isAcceptedNodeVersion,
writePackageJSON,
writeBabelRC,
writeMochaOpts,
installDeps,
writeBabelRCJS
} = require('.');

if (!isAcceptedNodeVersion()) {
throw new Error("Babel 7 will only support Node 4 and higher");
Expand All @@ -14,7 +21,8 @@ async function hasFlow() {
// TOOD: allow passing a specific path
(async () => {
// account for nested babelrc's
const paths = await globby(['**/.babelrc', '!**/node_modules/**']);
const babelRC = await globby(['**/.babelrc', '!**/node_modules/**']);
const babelRCJS = await globby(['**/.babelrc.js', '!**/node_modules/**']);
const packages = await globby(['**/package.json', '!**/node_modules/**']);
const mochaOpts = await globby(['**/mocha.opts', '!**/node_modules/**']);
const flow = await hasFlow();
Expand All @@ -25,11 +33,12 @@ async function hasFlow() {

// if not a monorepo
if (packages.length === 1) {
if (paths.length > 1) {
if (babelRC.length > 1) {
console.log("We suggest using the new 'overrides' option instead of nested .babelrc's, can check out http://new.babeljs.io/docs/en/next/babelrc.html#overrides");
console.log("");
}
paths.forEach(p => writeBabelRC(p, upgradeOptions));
babelRC.forEach(p => writeBabelRC(p, upgradeOptions));
babelRCJS.forEach(p => writeBabelRCJS(p));
}

mochaOpts.forEach(p => writeMochaOpts(p, upgradeOptions));
Expand Down
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const writeFile = require('write');
const crossSpawn = require('cross-spawn');
const hasYarn = require('has-yarn');

const transform = require('./transform');
const upgradeDeps = require('./upgradeDeps');
const upgradeConfig = require('./upgradeConfig');

Expand Down Expand Up @@ -118,6 +119,17 @@ async function writeBabelRC(configPath, options) {
}
}

async function writeBabelRCJS(configPath) {
try {
const rawFile = (await pify(fs.readFile)(configPath)).toString('utf8');
const res = transform(rawFile);

await pify(fs.writeFile)(configPath, res); // overwrite
} catch (e) {
console.log(e)
}
}

async function writeMochaOpts(configPath) {
let rawFile = (await pify(fs.readFile)(configPath)).toString('utf8');
await writeFile(configPath, replaceMocha(rawFile));
Expand All @@ -132,4 +144,5 @@ module.exports = {
getLatestVersion,
writeMochaOpts,
installDeps,
writeBabelRCJS
};
31 changes: 31 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const jscodeshift = require('jscodeshift');
const packageData = require('./packageData');

// for .babelrc.js
function transform(src) {
const res = jscodeshift(src)
.find(jscodeshift.Property)
.forEach((node) => {
const type = node.value.key.name;
const value = node.value.value;

if (type === 'plugins') {
if (value.type === 'ArrayExpression') {
value.elements.forEach(el => {
el.value = packageData.plugins[el.value] || el.value;
})
}
} else if (type === 'presets') {
if (value.type === 'ArrayExpression') {
value.elements.forEach(el => {
el.value = packageData.presets[el.value] || el.value;
})
}
}
})
.toSource();

return res;
}

module.exports = transform;

0 comments on commit 117778e

Please sign in to comment.