From 9fdf64baa217c2001a36fae0bf6fb23f5f8447c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Wed, 28 Jun 2017 21:26:32 -0700 Subject: [PATCH] save: only write package.json and locks if files have changed Fixes: https://github.com/npm/npm/issues/17173 --- lib/install/save.js | 7 ++++++- lib/shrinkwrap.js | 21 ++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/install/save.js b/lib/install/save.js index 56a4a892ad4..66b4a4ea9ad 100644 --- a/lib/install/save.js +++ b/lib/install/save.js @@ -123,7 +123,12 @@ function savePackageJson (tree, next) { } var json = JSON.stringify(tree.package, null, indent) + '\n' - writeFileAtomic(saveTarget, json, next) + if (json === packagejson) { + log.verbose('shrinkwrap', 'skipping write for package.json because there were no changes.') + next() + } else { + writeFileAtomic(saveTarget, json, next) + } })) } diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index 48c32486385..2a1c01d3aa1 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -163,14 +163,20 @@ function save (dir, pkginfo, opts, cb) { ) const updated = updateLockfileMetadata(pkginfo, pkg && pkg.data) const swdata = JSON.stringify(updated, null, info.indent) + '\n' - writeFileAtomic(info.path, swdata, (err) => { - if (err) return cb(err) - if (opts.silent) return cb(null, pkginfo) - if (!shrinkwrap && !lockfile) { - log.notice('', `created a lockfile as ${path.basename(info.path)}. You should commit this file.`) - } + if (swdata === info.raw) { + // skip writing if file is identical + log.verbose('shrinkwrap', `skipping write for ${path.basename(info.path)} because there were no changes.`) cb(null, pkginfo) - }) + } else { + writeFileAtomic(info.path, swdata, (err) => { + if (err) return cb(err) + if (opts.silent) return cb(null, pkginfo) + if (!shrinkwrap && !lockfile) { + log.notice('', `created a lockfile as ${path.basename(info.path)}. You should commit this file.`) + } + cb(null, pkginfo) + }) + } } ).then((file) => { }, cb) @@ -216,6 +222,7 @@ function checkPackageFile (dir, name) { ).then((data) => { return { path: file, + raw: data, data: JSON.parse(data), indent: detectIndent(data).indent || 2 }