-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add figgyConfig for things that use figgy-pudding
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'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 NpmConfig = figgyPudding({ | ||
log: {default: () => log}, | ||
'prefer-offline': {}, | ||
'prefer-online': {} | ||
}) | ||
|
||
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'), | ||
dirPacker: pack.packGitDep, | ||
hashAlgorithm: 'sha1', | ||
includeDeprecated: false, | ||
npmSession, | ||
projectScope: npm.projectScope, | ||
refer: npm.registry.refer, | ||
dmode: npm.modes.exec, | ||
fmode: npm.modes.file, | ||
umask: npm.modes.umask, | ||
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 (!('prefer-online' in conf)) { | ||
conf = conf.concat({ | ||
'prefer-online': npm.config.get('cache-max') <= 0 | ||
}) | ||
} | ||
if (!('prefer-offline' in conf)) { | ||
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 | ||
} |