refresh-package-json.js 1.5 KB

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