Skip to content

Commit 4cca9cb

Browse files
committed
config: add figgyConfig for things that use figgy-pudding
1 parent b86e515 commit 4cca9cb

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

lib/config/figgy-config.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const BB = require('bluebird')
4+
5+
const crypto = require('crypto')
6+
const figgyPudding = require('figgy-pudding')
7+
const log = require('npmlog')
8+
const npm = require('../npm.js')
9+
const pack = require('../pack.js')
10+
const path = require('path')
11+
12+
const npmSession = crypto.randomBytes(8).toString('hex')
13+
log.verbose('npm-session', npmSession)
14+
15+
const NpmConfig = figgyPudding({
16+
log: {default: () => log},
17+
'prefer-offline': {},
18+
'prefer-online': {}
19+
})
20+
21+
let baseConfig
22+
23+
module.exports = mkConfig
24+
function mkConfig (...providers) {
25+
if (!baseConfig) {
26+
baseConfig = NpmConfig(npm.config, {
27+
// Add some non-npm-config opts by hand.
28+
cache: path.join(npm.config.get('cache'), '_cacache'),
29+
dirPacker: pack.packGitDep,
30+
hashAlgorithm: 'sha1',
31+
includeDeprecated: false,
32+
npmSession,
33+
projectScope: npm.projectScope,
34+
refer: npm.registry.refer,
35+
dmode: npm.modes.exec,
36+
fmode: npm.modes.file,
37+
umask: npm.modes.umask,
38+
Promise: BB
39+
})
40+
const ownerStats = calculateOwner()
41+
if (ownerStats.uid != null || ownerStats.gid != null) {
42+
baseConfig = baseConfig.concat(ownerStats)
43+
}
44+
}
45+
let conf = baseConfig.concat(...providers)
46+
// Adapt some other configs if missing
47+
if (!('prefer-online' in conf)) {
48+
conf = conf.concat({
49+
'prefer-online': npm.config.get('cache-max') <= 0
50+
})
51+
}
52+
if (!('prefer-offline' in conf)) {
53+
conf = conf.concat({
54+
'prefer-online': npm.config.get('cache-min') >= 9999
55+
})
56+
}
57+
return conf
58+
}
59+
60+
let effectiveOwner
61+
function calculateOwner () {
62+
if (!effectiveOwner) {
63+
effectiveOwner = { uid: 0, gid: 0 }
64+
65+
// Pretty much only on windows
66+
if (!process.getuid) {
67+
return effectiveOwner
68+
}
69+
70+
effectiveOwner.uid = +process.getuid()
71+
effectiveOwner.gid = +process.getgid()
72+
73+
if (effectiveOwner.uid === 0) {
74+
if (process.env.SUDO_UID) effectiveOwner.uid = +process.env.SUDO_UID
75+
if (process.env.SUDO_GID) effectiveOwner.gid = +process.env.SUDO_GID
76+
}
77+
}
78+
79+
return effectiveOwner
80+
}

0 commit comments

Comments
 (0)