tmp.js 808 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const fixOwner = require('./fix-owner')
  4. const path = require('path')
  5. const rimraf = BB.promisify(require('rimraf'))
  6. const uniqueFilename = require('unique-filename')
  7. module.exports.mkdir = mktmpdir
  8. function mktmpdir (cache, opts) {
  9. opts = opts || {}
  10. const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
  11. return fixOwner.mkdirfix(tmpTarget, opts.uid, opts.gid).then(() => {
  12. return tmpTarget
  13. })
  14. }
  15. module.exports.withTmp = withTmp
  16. function withTmp (cache, opts, cb) {
  17. if (!cb) {
  18. cb = opts
  19. opts = null
  20. }
  21. opts = opts || {}
  22. return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb)
  23. }
  24. module.exports.fix = fixtmpdir
  25. function fixtmpdir (cache, opts) {
  26. return fixOwner(path.join(cache, 'tmp'), opts.uid, opts.gid)
  27. }