tmp.js 696 B

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