tmp.js 956 B

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