• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1module.exports = setUser
2
3var assert = require('assert')
4var path = require('path')
5var fs = require('fs')
6var mkdirp = require('gentle-fs').mkdir
7
8function setUser (cb) {
9  var defaultConf = this.root
10  assert(defaultConf !== Object.prototype)
11
12  // If global, leave it as-is.
13  // If not global, then set the user to the owner of the prefix folder.
14  // Just set the default, so it can be overridden.
15  if (this.get('global')) return cb()
16  if (process.env.SUDO_UID) {
17    defaultConf.user = +(process.env.SUDO_UID)
18    return cb()
19  }
20
21  var prefix = path.resolve(this.get('prefix'))
22  mkdirp(prefix, function (er) {
23    if (er) return cb(er)
24    fs.stat(prefix, function (er, st) {
25      defaultConf.user = st && st.uid
26      return cb(er)
27    })
28  })
29}
30