mkdtemp.js 891 B

12345678910111213141516171819202122232425262728
  1. const { dirname, sep } = require('path')
  2. const fs = require('./fs.js')
  3. const getOptions = require('./common/get-options.js')
  4. const owner = require('./common/owner.js')
  5. const mkdtemp = async (prefix, opts) => {
  6. const options = getOptions(opts, {
  7. copy: ['encoding', 'owner'],
  8. wrap: 'encoding',
  9. })
  10. // mkdtemp relies on the trailing path separator to indicate if it should
  11. // create a directory inside of the prefix. if that's the case then the root
  12. // we infer ownership from is the prefix itself, otherwise it's the dirname
  13. // /tmp -> /tmpABCDEF, infers from /
  14. // /tmp/ -> /tmp/ABCDEF, infers from /tmp
  15. const root = prefix.endsWith(sep) ? prefix : dirname(prefix)
  16. const { uid, gid } = await owner.validate(root, options.owner)
  17. const result = await fs.mkdtemp(prefix, options)
  18. await owner.update(result, uid, gid)
  19. return result
  20. }
  21. module.exports = mkdtemp