summaryrefslogtreecommitdiff
path: root/deps/npm/lib/config/figgy-config.js
blob: 9e9ca0ba561efbb926660996593e10b608851e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'

const BB = require('bluebird')

const crypto = require('crypto')
const figgyPudding = require('figgy-pudding')
const log = require('npmlog')
const npm = require('../npm.js')
const pack = require('../pack.js')
const path = require('path')

const npmSession = crypto.randomBytes(8).toString('hex')
log.verbose('npm-session', npmSession)

const SCOPE_REGISTRY_REGEX = /@.*:registry$/gi
const NpmConfig = figgyPudding({}, {
  other (key) {
    return key.match(SCOPE_REGISTRY_REGEX)
  }
})

let baseConfig

module.exports = mkConfig
function mkConfig (...providers) {
  if (!baseConfig) {
    baseConfig = NpmConfig(npm.config, {
      // Add some non-npm-config opts by hand.
      cache: path.join(npm.config.get('cache'), '_cacache'),
      // NOTE: npm has some magic logic around color distinct from the config
      // value, so we have to override it here
      color: !!npm.color,
      dirPacker: pack.packGitDep,
      hashAlgorithm: 'sha1',
      includeDeprecated: false,
      log,
      'npm-session': npmSession,
      'project-scope': npm.projectScope,
      refer: npm.referer,
      dmode: npm.modes.exec,
      fmode: npm.modes.file,
      umask: npm.modes.umask,
      npmVersion: npm.version,
      tmp: npm.tmp,
      Promise: BB
    })
    const ownerStats = calculateOwner()
    if (ownerStats.uid != null || ownerStats.gid != null) {
      baseConfig = baseConfig.concat(ownerStats)
    }
  }
  let conf = baseConfig.concat(...providers)
  // Adapt some other configs if missing
  if (npm.config.get('prefer-online') === undefined) {
    conf = conf.concat({
      'prefer-online': npm.config.get('cache-max') <= 0
    })
  }
  if (npm.config.get('prefer-online') === undefined) {
    conf = conf.concat({
      'prefer-online': npm.config.get('cache-min') >= 9999
    })
  }
  return conf
}

let effectiveOwner
function calculateOwner () {
  if (!effectiveOwner) {
    effectiveOwner = { uid: 0, gid: 0 }

    // Pretty much only on windows
    if (!process.getuid) {
      return effectiveOwner
    }

    effectiveOwner.uid = +process.getuid()
    effectiveOwner.gid = +process.getgid()

    if (effectiveOwner.uid === 0) {
      if (process.env.SUDO_UID) effectiveOwner.uid = +process.env.SUDO_UID
      if (process.env.SUDO_GID) effectiveOwner.gid = +process.env.SUDO_GID
    }
  }

  return effectiveOwner
}