tmp.js 839 B

1234567891011121314151617181920212223242526272829303132333435
  1. 'use strict'
  2. const fs = require('@npmcli/fs')
  3. const fixOwner = require('./fix-owner')
  4. const path = require('path')
  5. module.exports.mkdir = mktmpdir
  6. function mktmpdir (cache, opts = {}) {
  7. const { tmpPrefix } = opts
  8. const tmpDir = path.join(cache, 'tmp')
  9. return fs.mkdir(tmpDir, { recursive: true, owner: 'inherit' })
  10. .then(() => {
  11. // do not use path.join(), it drops the trailing / if tmpPrefix is unset
  12. const target = `${tmpDir}${path.sep}${tmpPrefix || ''}`
  13. return fs.mkdtemp(target, { owner: 'inherit' })
  14. })
  15. }
  16. module.exports.withTmp = withTmp
  17. function withTmp (cache, opts, cb) {
  18. if (!cb) {
  19. cb = opts
  20. opts = {}
  21. }
  22. return fs.withTempDir(path.join(cache, 'tmp'), cb, opts)
  23. }
  24. module.exports.fix = fixtmpdir
  25. function fixtmpdir (cache) {
  26. return fixOwner(cache, path.join(cache, 'tmp'))
  27. }