set-user.js 758 B

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