index.js 981 B

1234567891011121314151617181920212223242526272829303132
  1. const fs = require('../fs.js')
  2. const getOptions = require('../common/get-options.js')
  3. const node = require('../common/node.js')
  4. const owner = require('../common/owner.js')
  5. const polyfill = require('./polyfill.js')
  6. // node 10.12.0 added the options parameter, which allows recursive and mode
  7. // properties to be passed
  8. const useNative = node.satisfies('>=10.12.0')
  9. // extends mkdir with the ability to specify an owner of the new dir
  10. const mkdir = async (path, opts) => {
  11. const options = getOptions(opts, {
  12. copy: ['mode', 'recursive', 'owner'],
  13. wrap: 'mode',
  14. })
  15. const { uid, gid } = await owner.validate(path, options.owner)
  16. // the polyfill is tested separately from this module, no need to hack
  17. // process.version to try to trigger it just for coverage
  18. // istanbul ignore next
  19. const result = useNative
  20. ? await fs.mkdir(path, options)
  21. : await polyfill(path, options)
  22. await owner.update(path, uid, gid)
  23. return result
  24. }
  25. module.exports = mkdir