Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

[BUG] should not write package.json file if nothing is being changed #232

Closed
isaacs opened this issue Feb 17, 2021 · 0 comments
Closed

[BUG] should not write package.json file if nothing is being changed #232

isaacs opened this issue Feb 17, 2021 · 0 comments

Comments

@isaacs
Copy link
Contributor

isaacs commented Feb 17, 2021

See npm/cli#2714

We write package.json, even when there's no changes to be made.

Untested patch that may fix this:

diff --git a/lib/update-root-package-json.js b/lib/update-root-package-json.js
index 735ebd1..43e2c40 100644
--- a/lib/update-root-package-json.js
+++ b/lib/update-root-package-json.js
@@ -15,11 +15,18 @@ const depTypes = new Set([
   'peerDependencies',
 ])
 
+const parseJsonSafe = json => {
+  try {
+    return parseJSON(json)
+  } catch (er) {
+    return null
+  }
+}
+
 const updateRootPackageJson = async tree => {
   const filename = resolve(tree.path, 'package.json')
-  const originalContent = await readFile(filename, 'utf8')
-    .then(data => parseJSON(data))
-    .catch(() => null)
+  const originalJson = await readFile(filename, 'utf8')
+  const originalContent = parseJsonSafe(originalJson)
 
   const depsData = orderDeps({
     ...tree.package,
@@ -52,7 +59,8 @@ const updateRootPackageJson = async tree => {
   const content = (JSON.stringify(packageJsonContent, null, format) + '\n')
     .replace(/\n/g, eol)
 
-  return writeFile(filename, content)
+  if (content !== originalJson)
+    return writeFile(filename, content)
 }
 
 module.exports = updateRootPackageJson

Should do something similar in lib/shrinkwrap.js, so it doesn't save if nothing is changing.

isaacs added a commit that referenced this issue Feb 17, 2021
Need a similar fix for Shrinkwrap class.

Fix: #232
@isaacs isaacs closed this as completed in 7e8b824 Feb 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant