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
This commit is contained in:
Zoltan Kochan
2016-08-09 20:33:39 +03:00
committed by GitHub
parent 74db89a7d8
commit a9afddacfd
3 changed files with 18 additions and 13 deletions

View File

@@ -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]
}

View File

@@ -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 ? '' : '^'

View File

@@ -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()