Skip to content

Commit

Permalink
handle polyfill,register, look for mocha.opts
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed Feb 21, 2018
1 parent 10c6889 commit e81cf7c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion __tests__/__snapshots__/packageData.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ Object {
"scripts": Object {
"coverage": "NODE_ENV=test nyc --reporter=lcov --reporter=text _mocha --require @babel/register test/*.js",
"prepublish": "npm run test",
"test": "mocha --compilers js:@babel/register test/*Test.js",
"test": "mocha --compilers js:@babel/register --require @babel/polyfill test/*Test.js",
"test2": "mocha --compilers js:@babel/register --require @babel/polyfill test/*Test.js",
},
}
`;
Expand Down
3 changes: 2 additions & 1 deletion fixtures/scripts-mocha.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "mocha-scripts-test",
"scripts": {
"prepublish": "npm run test",
"test": "mocha --compilers js:babel-register test/*Test.js",
"test": "mocha --compilers js:babel-register --require babel-polyfill test/*Test.js",
"test2": "mocha --compilers js:babel-core/register --require babel-polyfill test/*Test.js",
"coverage": "NODE_ENV=test nyc --reporter=lcov --reporter=text _mocha --require babel-register test/*.js"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"read-pkg-up": "^3.0.0",
"semver": "^5.5.0",
"sort-keys": "^2.0.0",
"write": "^1.0.3",
"write-json-file": "^2.3.0",
"write-pkg": "^3.1.0"
},
Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ npx babel-upgrade
- [ ] Update test files that use babel directly (`babel-types`, `babel-core`)
- Update all requires/imports
- Update the use of the Babel API (plugins, integrations)
- [ ] Modify misc files as we go (`karma.conf.js`, `mocha.opts`)
- [ ] Modify misc files as we go
- [x] `mocha.opts`
- [ ] Add to the upgrade guide which parts are autofixable and the command (if we care enough to make this individually runnable too infrastructure wise)
- [ ] May need to add a warning on any 3rd party plugins since they might not be compatible
- [ ] Handle the differences in plugins in v7 for default/loose/spec
Expand Down
5 changes: 4 additions & 1 deletion src/bin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
const { isAcceptedNodeVersion, writePackageJSON, writeBabelRC } = require('.');
const { isAcceptedNodeVersion, writePackageJSON, writeBabelRC, writeMochaOpts } = require('.');
const globby = require('globby');
const cwd = process.cwd();

Expand All @@ -12,6 +12,7 @@ if (!isAcceptedNodeVersion()) {
// account for nested babelrc's
const paths = await globby(['**/.babelrc', '!./node_modules/**']);
const packages = await globby(['**/package.json', '!./node_modules/**']);
const mochaOpts = await globby(['**/mocha.opts', '!./node_modules/**']);

// if not a monorepo
if (packages.length === 1) {
Expand All @@ -20,6 +21,8 @@ if (!isAcceptedNodeVersion()) {
}
paths.forEach(p => writeBabelRC(p));
}

mochaOpts.forEach(p => writeMochaOpts(p));
})();

// TOOD: allow passing a specific path
Expand Down
21 changes: 17 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const pify = require('pify');
const JSON5 = require('json5');
const writeJsonFile = require('write-json-file');
const semver = require('semver');
const writeFile = require('write');

const upgradeDeps = require('./upgradeDeps');
const upgradeConfig = require('./upgradeConfig');
Expand All @@ -18,11 +19,17 @@ function getLatestVersion() {
return "7.0.0-beta.39";
}

function replaceMocha(str) {
return str
.replace('--compilers js:babel-core/register', '--compilers js:@babel/register')
.replace('--compilers js:babel-register', '--compilers js:@babel/register')
.replace('--require babel-register', '--require @babel/register')
.replace('--require babel-polyfill', '--require @babel/polyfill');
}

function upgradeScripts(scripts) {
for (let script of Object.keys(scripts)) {
// mocha --compilers js:@babel/register
scripts[script] = scripts[script].replace('--compilers js:babel-register', '--compilers js:@babel/register');
scripts[script] = scripts[script].replace('--require babel-register', '--require @babel/register');
scripts[script] = replaceMocha(scripts[script]);
}
return scripts;
}
Expand Down Expand Up @@ -109,11 +116,17 @@ async function writeBabelRC(configPath) {
}
}

async function writeMochaOpts(configPath) {
let rawFile = (await pify(fs.readFile)(configPath)).toString('utf8');
await writeFile(configPath, replaceMocha(rawFile));
}

module.exports = {
isAcceptedNodeVersion,
updatePackageJSON,
writePackageJSON,
readBabelRC,
writeBabelRC,
getLatestVersion
getLatestVersion,
writeMochaOpts
};

0 comments on commit e81cf7c

Please sign in to comment.