figgy-config.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const crypto = require('crypto')
  4. const figgyPudding = require('figgy-pudding')
  5. const log = require('npmlog')
  6. const npm = require('../npm.js')
  7. const pack = require('../pack.js')
  8. const path = require('path')
  9. const npmSession = npm.session = crypto.randomBytes(8).toString('hex')
  10. log.verbose('npm-session', npmSession)
  11. const SCOPE_REGISTRY_REGEX = /@.*:registry$/gi
  12. const NpmConfig = figgyPudding({}, {
  13. other (key) {
  14. return key.match(SCOPE_REGISTRY_REGEX)
  15. }
  16. })
  17. let baseConfig
  18. module.exports = mkConfig
  19. function mkConfig (...providers) {
  20. if (!baseConfig) {
  21. baseConfig = NpmConfig(npm.config, {
  22. // Add some non-npm-config opts by hand.
  23. cache: path.join(npm.config.get('cache'), '_cacache'),
  24. // NOTE: npm has some magic logic around color distinct from the config
  25. // value, so we have to override it here
  26. color: !!npm.color,
  27. dirPacker: pack.packGitDep,
  28. hashAlgorithm: 'sha1',
  29. includeDeprecated: false,
  30. log,
  31. 'npm-session': npmSession,
  32. 'project-scope': npm.projectScope,
  33. refer: npm.referer,
  34. dmode: npm.modes.exec,
  35. fmode: npm.modes.file,
  36. umask: npm.modes.umask,
  37. npmVersion: npm.version,
  38. tmp: npm.tmp,
  39. Promise: BB
  40. })
  41. const ownerStats = calculateOwner()
  42. if (ownerStats.uid != null || ownerStats.gid != null) {
  43. baseConfig = baseConfig.concat(ownerStats)
  44. }
  45. }
  46. let conf = baseConfig.concat(...providers)
  47. // Adapt some other configs if missing
  48. if (npm.config.get('prefer-online') === undefined) {
  49. conf = conf.concat({
  50. 'prefer-online': npm.config.get('cache-max') <= 0
  51. })
  52. }
  53. if (npm.config.get('prefer-online') === undefined) {
  54. conf = conf.concat({
  55. 'prefer-online': npm.config.get('cache-min') >= 9999
  56. })
  57. }
  58. return conf
  59. }
  60. let effectiveOwner
  61. function calculateOwner () {
  62. if (!effectiveOwner) {
  63. effectiveOwner = { uid: 0, gid: 0 }
  64. // Pretty much only on windows
  65. if (!process.getuid) {
  66. return effectiveOwner
  67. }
  68. effectiveOwner.uid = +process.getuid()
  69. effectiveOwner.gid = +process.getgid()
  70. if (effectiveOwner.uid === 0) {
  71. if (process.env.SUDO_UID) effectiveOwner.uid = +process.env.SUDO_UID
  72. if (process.env.SUDO_GID) effectiveOwner.gid = +process.env.SUDO_GID
  73. }
  74. }
  75. return effectiveOwner
  76. }