• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const Bluebird = require('bluebird')
4
5const checkPlatform = Bluebird.promisify(require('npm-install-checks').checkPlatform)
6const getRequested = require('../get-requested.js')
7const npm = require('../../npm.js')
8const path = require('path')
9const readJson = Bluebird.promisify(require('read-package-json'))
10const updatePackageJson = Bluebird.promisify(require('../update-package-json'))
11
12module.exports = function (staging, pkg, log) {
13  log.silly('refresh-package-json', pkg.realpath)
14
15  return readJson(path.join(pkg.path, 'package.json'), false).then((metadata) => {
16    Object.keys(pkg.package).forEach(function (key) {
17      if (key !== 'version' && key !== 'dependencies' && !isEmpty(pkg.package[key])) {
18        metadata[key] = pkg.package[key]
19      }
20    })
21    if (metadata._resolved == null && pkg.fakeChild) {
22      metadata._resolved = pkg.fakeChild.resolved
23    }
24    // These two sneak in and it's awful
25    delete metadata.readme
26    delete metadata.readmeFilename
27
28    pkg.package = metadata
29    pkg.fakeChild = false
30  }).catch(() => 'ignore').then(() => {
31    return checkPlatform(pkg.package, npm.config.get('force'))
32  }).then(() => {
33    const requested = pkg.package._requested || getRequested(pkg)
34    if (requested.type !== 'directory') {
35      return updatePackageJson(pkg, pkg.path)
36    }
37  })
38}
39
40function isEmpty (value) {
41  if (value == null) return true
42  if (Array.isArray(value)) return !value.length
43  if (typeof value === 'object') return !Object.keys(value).length
44  return false
45}
46