You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
879 B

4 years ago
  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. })
  11. module.exports.mkdir = mktmpdir
  12. function mktmpdir (cache, opts) {
  13. opts = TmpOpts(opts)
  14. const tmpTarget = uniqueFilename(path.join(cache, 'tmp'), opts.tmpPrefix)
  15. return fixOwner.mkdirfix(cache, tmpTarget).then(() => {
  16. return tmpTarget
  17. })
  18. }
  19. module.exports.withTmp = withTmp
  20. function withTmp (cache, opts, cb) {
  21. if (!cb) {
  22. cb = opts
  23. opts = null
  24. }
  25. opts = TmpOpts(opts)
  26. return BB.using(mktmpdir(cache, opts).disposer(rimraf), cb)
  27. }
  28. module.exports.fix = fixtmpdir
  29. function fixtmpdir (cache) {
  30. return fixOwner(cache, path.join(cache, 'tmp'))
  31. }