Skip to content

Commit

Permalink
feat(debug): log manifest deps changes
Browse files Browse the repository at this point in the history
relates #27
  • Loading branch information
antongolub committed Dec 24, 2020
1 parent a053674 commit 88b4077
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion lib/updateDeps.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 88b4077

Please sign in to comment.