tmp.js 800 B

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