babel-upgrade
Advanced tools
Comparing version 0.0.17 to 0.0.18
"use strict"; | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _next(value) { step("next", value); } function _throw(err) { step("throw", err); } _next(); }); }; } | ||
@@ -22,5 +30,4 @@ | ||
return _hasFlow.apply(this, arguments); | ||
} // TODO: allow passing a specific path | ||
} | ||
function _hasFlow() { | ||
@@ -52,6 +59,23 @@ _hasFlow = _asyncToGenerator( | ||
function parseOptions(args, availableOptions) { | ||
return Object.entries(availableOptions).map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
option = _ref2[0], | ||
flags = _ref2[1]; | ||
return { | ||
[option]: args.some(function (arg) { | ||
return flags.includes(arg); | ||
}) | ||
}; | ||
}).reduce(function (options, option) { | ||
return Object.assign(options, option); | ||
}, {}); | ||
} // TODO: allow passing a specific path | ||
_asyncToGenerator( | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee() { | ||
var paths, packages, mochaOpts, flow, upgradeOptions; | ||
var paths, packages, mochaOpts, flow, cliOptions, upgradeOptions; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -81,4 +105,9 @@ while (1) { | ||
flow = _context.sent; | ||
cliOptions = parseOptions(process.argv, { | ||
installDeps: ['--install'], | ||
write: ['--write', '-w'] | ||
}); | ||
upgradeOptions = { | ||
hasFlow: flow | ||
hasFlow: flow, | ||
write: cliOptions.write | ||
}; // if not a monorepo | ||
@@ -101,16 +130,28 @@ | ||
_context.next = 17; | ||
_context.next = 18; | ||
return writePackageJSON(upgradeOptions); | ||
case 17: | ||
if (!(process.argv[2] === '--install')) { | ||
_context.next = 21; | ||
case 18: | ||
if (!cliOptions.installDeps) { | ||
_context.next = 26; | ||
break; | ||
} | ||
if (!cliOptions.write) { | ||
_context.next = 25; | ||
break; | ||
} | ||
console.log('Installing new dependencies'); | ||
_context.next = 21; | ||
_context.next = 23; | ||
return installDeps(); | ||
case 21: | ||
case 23: | ||
_context.next = 26; | ||
break; | ||
case 25: | ||
console.log('Run babel-upgrade with --write (or) -w and --install for it to install the newly added dependencies'); | ||
case 26: | ||
case "end": | ||
@@ -117,0 +158,0 @@ return _context.stop(); |
@@ -27,2 +27,4 @@ "use strict"; | ||
var diff = require('diff'); | ||
var upgradeDeps = require('./upgradeDeps'); | ||
@@ -108,2 +110,11 @@ | ||
function prettyPrint(json) { | ||
return JSON.stringify(json, null, 2); | ||
} | ||
function showPatch(filename, before, after) { | ||
console.log(diff.createPatch(filename, before, after, "Before Upgrade", "After Upgrade")); | ||
console.log(""); | ||
} | ||
function writePackageJSON(_x3) { | ||
@@ -117,3 +128,3 @@ return _writePackageJSON.apply(this, arguments); | ||
regeneratorRuntime.mark(function _callee2(options) { | ||
var _ref, pkg, path; | ||
var _ref, pkg, path, oldPkg; | ||
@@ -133,6 +144,7 @@ return regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
path = _ref.path; | ||
_context2.next = 7; | ||
oldPkg = prettyPrint(pkg); | ||
_context2.next = 8; | ||
return updatePackageJSON(pkg, options); | ||
case 7: | ||
case 8: | ||
pkg = _context2.sent; | ||
@@ -145,3 +157,10 @@ | ||
_context2.next = 11; | ||
showPatch(path, oldPkg, prettyPrint(pkg)); | ||
if (!options.write) { | ||
_context2.next = 14; | ||
break; | ||
} | ||
_context2.next = 14; | ||
return writeJsonFile(path, pkg, { | ||
@@ -151,3 +170,3 @@ detectIndent: true | ||
case 11: | ||
case 14: | ||
case "end": | ||
@@ -236,3 +255,3 @@ return _context2.stop(); | ||
regeneratorRuntime.mark(function _callee5(configPath, options) { | ||
var json; | ||
var json, oldJson; | ||
return regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
@@ -257,3 +276,3 @@ while (1) { | ||
if (!json) { | ||
_context5.next = 13; | ||
_context5.next = 17; | ||
break; | ||
@@ -263,4 +282,12 @@ } | ||
console.log(`Updating .babelrc config at ${configPath}`); | ||
oldJson = prettyPrint(json); | ||
json = upgradeConfig(json, options); | ||
_context5.next = 13; | ||
showPatch(configPath, oldJson, prettyPrint(json)); | ||
if (!options.write) { | ||
_context5.next = 16; | ||
break; | ||
} | ||
_context5.next = 16; | ||
return writeJsonFile(configPath, json, { | ||
@@ -270,3 +297,6 @@ detectIndent: true | ||
case 13: | ||
case 16: | ||
; | ||
case 17: | ||
case "end": | ||
@@ -281,3 +311,3 @@ return _context5.stop(); | ||
function writeMochaOpts(_x7) { | ||
function writeMochaOpts(_x7, _x8) { | ||
return _writeMochaOpts.apply(this, arguments); | ||
@@ -289,3 +319,3 @@ } | ||
/*#__PURE__*/ | ||
regeneratorRuntime.mark(function _callee6(configPath) { | ||
regeneratorRuntime.mark(function _callee6(configPath, options) { | ||
var rawFile; | ||
@@ -301,6 +331,13 @@ return regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
rawFile = _context6.sent.toString('utf8'); | ||
_context6.next = 5; | ||
showPatch(configPath, rawFile, replaceMocha(rawFile)); | ||
if (!options.write) { | ||
_context6.next = 7; | ||
break; | ||
} | ||
_context6.next = 7; | ||
return writeFile(configPath, replaceMocha(rawFile)); | ||
case 5: | ||
case 7: | ||
case "end": | ||
@@ -307,0 +344,0 @@ return _context6.stop(); |
@@ -81,3 +81,3 @@ "use strict"; | ||
if ((dependencies['jest'] || depsWebpack1 && dependencies['babel-loader']) && !dependencies['babel-core']) { | ||
if ((dependencies['jest'] || dependencies['jest-cli'] || depsWebpack1 && dependencies['babel-loader']) && !dependencies['babel-core']) { | ||
dependencies['babel-core'] = '^7.0.0-bridge.0'; | ||
@@ -84,0 +84,0 @@ } |
{ | ||
"name": "babel-upgrade", | ||
"version": "0.0.17", | ||
"version": "0.0.18", | ||
"description": "Upgrade tool for Babel", | ||
@@ -31,2 +31,3 @@ "bin": "./bin/babel-upgrade", | ||
"cross-spawn": "^6.0.4", | ||
"diff": "^3.5.0", | ||
"globby": "^8.0.1", | ||
@@ -33,0 +34,0 @@ "has-yarn": "^1.0.0", |
@@ -13,13 +13,15 @@ # babel-upgrade | ||
# npx lets you run babel-upgrade without installing it locally | ||
npx babel-upgrade | ||
npx babel-upgrade --write | ||
# or install globally and run | ||
npm install babel-upgrade -g | ||
babel-upgrade | ||
babel-upgrade --write | ||
``` | ||
Optionally use `--install` to run `yarn` or `npm` after the upgrade. | ||
Without the `--write` (or `-w`) flag, `babel-upgrade` will print a diff without writing any changes. | ||
Optionally, add `--install` as well to run `yarn` or `npm` after writing the upgrade. | ||
```bash | ||
npx babel-upgrade --install | ||
npx babel-upgrade --write --install | ||
``` | ||
@@ -66,3 +68,3 @@ | ||
- [x] use `"babel-core": "^7.0.0-bridge-0"` if jest is a dependency ([#14](https://github.com/babel/babel-upgrade/pull/14)) | ||
- [x] use `"babel-core": "^7.0.0-bridge-0"` if jest or jest-cli is a dependency ([#14](https://github.com/babel/babel-upgrade/pull/14)) | ||
@@ -69,0 +71,0 @@ ```diff |
@@ -14,2 +14,10 @@ const path = require('path'); | ||
function parseOptions(args, availableOptions) { | ||
return Object.entries(availableOptions) | ||
.map(([option, flags]) => ({ | ||
[option]: args.some(arg => flags.includes(arg)) | ||
})) | ||
.reduce((options, option) => Object.assign(options, option), {}); | ||
} | ||
// TODO: allow passing a specific path | ||
@@ -22,5 +30,9 @@ (async () => { | ||
const flow = await hasFlow(); | ||
const cliOptions = parseOptions(process.argv, { | ||
installDeps: ['--install'], | ||
write: ['--write', '-w'] | ||
}) | ||
const upgradeOptions = { | ||
hasFlow: flow, | ||
write: cliOptions.write | ||
}; | ||
@@ -43,6 +55,10 @@ | ||
// TODO: add smarter CLI option handling if we support more options | ||
if (process.argv[2] === '--install') { | ||
console.log('Installing new dependencies'); | ||
await installDeps(); | ||
if (cliOptions.installDeps) { | ||
if (cliOptions.write) { | ||
console.log('Installing new dependencies'); | ||
await installDeps(); | ||
} else { | ||
console.log('Run babel-upgrade with --write (or) -w and --install for it to install the newly added dependencies'); | ||
} | ||
} | ||
})(); |
@@ -12,3 +12,3 @@ const path = require('path'); | ||
const hasYarn = require('has-yarn'); | ||
const diff = require('diff'); | ||
const upgradeDeps = require('./upgradeDeps'); | ||
@@ -60,3 +60,2 @@ const upgradeConfig = require('./upgradeConfig'); | ||
if (pkg.devDependencies) { | ||
@@ -81,5 +80,22 @@ pkg.devDependencies = sortKeys(upgradeDeps( | ||
function prettyPrint(json) { | ||
return JSON.stringify(json, null, 2); | ||
} | ||
function showPatch(filename, before, after) { | ||
console.log( | ||
diff.createPatch( | ||
filename, | ||
before, | ||
after, | ||
"Before Upgrade", | ||
"After Upgrade" | ||
) | ||
); | ||
console.log(""); | ||
} | ||
async function writePackageJSON(options) { | ||
let { pkg, path } = await readPkgUp({ normalize: false }); | ||
let oldPkg = prettyPrint(pkg); | ||
pkg = await updatePackageJSON(pkg, options); | ||
@@ -91,4 +107,7 @@ | ||
} | ||
showPatch(path, oldPkg, prettyPrint(pkg)); | ||
await writeJsonFile(path, pkg, { detectIndent: true }); | ||
if (options.write) { | ||
await writeJsonFile(path, pkg, { detectIndent: true }); | ||
} | ||
} | ||
@@ -120,10 +139,19 @@ | ||
console.log(`Updating .babelrc config at ${configPath}`); | ||
let oldJson = prettyPrint(json); | ||
json = upgradeConfig(json, options); | ||
await writeJsonFile(configPath, json, { detectIndent: true }); | ||
showPatch(configPath, oldJson, prettyPrint(json)); | ||
if (options.write) { | ||
await writeJsonFile(configPath, json, { detectIndent: true }); | ||
}; | ||
} | ||
} | ||
async function writeMochaOpts(configPath) { | ||
async function writeMochaOpts(configPath, options) { | ||
let rawFile = (await pify(fs.readFile)(configPath)).toString('utf8'); | ||
await writeFile(configPath, replaceMocha(rawFile)); | ||
showPatch(configPath, rawFile, replaceMocha(rawFile)); | ||
if (options.write) { | ||
await writeFile(configPath, replaceMocha(rawFile)); | ||
} | ||
} | ||
@@ -130,0 +158,0 @@ |
@@ -78,3 +78,3 @@ const semver = require('semver'); | ||
if ( | ||
(dependencies['jest'] || (depsWebpack1 && dependencies['babel-loader'])) && | ||
(dependencies['jest'] || dependencies['jest-cli'] || (depsWebpack1 && dependencies['babel-loader'])) && | ||
!dependencies['babel-core'] | ||
@@ -81,0 +81,0 @@ ) { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
73210
1393
205
13
1
+ Addeddiff@^3.5.0
+ Addeddiff@3.5.0(transitive)