diff --git a/lib/updateDeps.js b/lib/updateDeps.js index 015dd45..2e5196b 100644 --- a/lib/updateDeps.js +++ b/lib/updateDeps.js @@ -1,6 +1,9 @@ const { writeFileSync } = require("fs"); -const recognizeFormat = require("./recognizeFormat"); const semver = require("semver"); +const { isObject, isEqual, transform } = require("lodash"); +const recognizeFormat = require("./recognizeFormat"); +const getManifest = require("./getManifest"); +const debug = require("debug")("msr:updateDeps"); /** * Resolve next package version. @@ -176,10 +179,54 @@ const updateManifestDeps = (pkg) => { throw Error(`Cannot release because dependency ${d.name} has not been released`); }); + if (!auditManifestChanges(manifest, path)) { + return; + } + // Write package.json back out. writeFileSync(path, JSON.stringify(manifest, null, indent) + trailingWhitespace); }; +// https://gist.github.com/Yimiprod/7ee176597fef230d1451 +const difference = (object, base) => + transform(object, (result, value, key) => { + if (!isEqual(value, base[key])) { + result[key] = + isObject(value) && isObject(base[key]) ? difference(value, base[key]) : `${base[key]} → ${value}`; + } + }); + +/** + * Clarify what exactly was changed in manifest file. + * @param {object} actualManifest manifest object + * @param {string} path manifest path + * @returns {boolean} has changed or not + * @internal + */ +const auditManifestChanges = (actualManifest, path) => { + const oldManifest = getManifest(path); + const depScopes = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"]; + const changes = depScopes.reduce((res, scope) => { + const diff = difference(actualManifest[scope], oldManifest[scope]); + + if (Object.keys(diff).length) { + res[scope] = diff; + } + + return res; + }, {}); + + debug(actualManifest.name, path); + + if (Object.keys(changes).length) { + debug(changes); + return true; + } + + debug("no deps changes"); + return false; +}; + module.exports = { getNextVersion, getNextPreVersion,