From a9afddacfd78926abb92116a977d43f8993d0d6e Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 9 Aug 2016 20:33:39 +0300 Subject: [PATCH] Read package.json before updating (#284) * Read package.json before updating Using the cached version of package.json can cause ovewriting of changes close #280 * Skip GitHub test on CI The GitHub test fails frequently with 403 error with no good no reason --- lib/fs/require_json.js | 5 +++-- lib/save.js | 3 ++- test/index.js | 23 +++++++++++++---------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/fs/require_json.js b/lib/fs/require_json.js index fe3bf6b51f..528c087c4d 100644 --- a/lib/fs/require_json.js +++ b/lib/fs/require_json.js @@ -4,9 +4,10 @@ var cache = {} * Works identically to require('/path/to/file.json'), but safer. */ -module.exports = function requireJson (path) { +module.exports = function requireJson (path, opts) { + opts = opts || {} path = require('path').resolve(path) - if (cache[path]) return cache[path] + if (!opts.ignoreCache && cache[path]) return cache[path] cache[path] = JSON.parse(require('fs').readFileSync(path, 'utf-8')) return cache[path] } diff --git a/lib/save.js b/lib/save.js index 489c086908..7dcdae3c5a 100644 --- a/lib/save.js +++ b/lib/save.js @@ -3,7 +3,8 @@ var writeJson = require('./fs/write_json') var sortedObject = require('sorted-object') module.exports = function save (pkg, installedPackages, saveType, useExactVersion) { - var packageJson = requireJson(pkg.path) + // Read the latest version of package.json to avoid accidental overwriting + var packageJson = requireJson(pkg.path, { ignoreCache: true }) packageJson[saveType] = packageJson[saveType] || {} installedPackages.forEach(function (dependency) { var semverCharacter = useExactVersion ? '' : '^' diff --git a/test/index.js b/test/index.js index 8e95015dc4..b3d7679dcd 100644 --- a/test/index.js +++ b/test/index.js @@ -207,18 +207,21 @@ test('local file', function (t) { }, t.end) }) -test('from a github repo', function (t) { - prepare() - install(['kevva/is-negative'], { quiet: true }) - .then(function () { - var localPkg = require( - join(process.cwd(), 'node_modules', 'is-negative')) +// Skipping on CI as failing frequently there, due to environment issues +if (!process.env.CI) { + test('from a github repo', function (t) { + prepare() + install(['kevva/is-negative'], { quiet: true }) + .then(function () { + var localPkg = require( + join(process.cwd(), 'node_modules', 'is-negative')) - t.ok(localPkg, 'isNegative() is available') + t.ok(localPkg, 'isNegative() is available') - t.end() - }, t.end) -}) + t.end() + }, t.end) + }) +} test('shrinkwrap compatibility', function (t) { prepare()