opts-arg.js 784 B

1234567891011121314151617181920212223
  1. const { promisify } = require('util')
  2. const fs = require('fs')
  3. const optsArg = opts => {
  4. if (!opts)
  5. opts = { mode: 0o777, fs }
  6. else if (typeof opts === 'object')
  7. opts = { mode: 0o777, fs, ...opts }
  8. else if (typeof opts === 'number')
  9. opts = { mode: opts, fs }
  10. else if (typeof opts === 'string')
  11. opts = { mode: parseInt(opts, 8), fs }
  12. else
  13. throw new TypeError('invalid options argument')
  14. opts.mkdir = opts.mkdir || opts.fs.mkdir || fs.mkdir
  15. opts.mkdirAsync = promisify(opts.mkdir)
  16. opts.stat = opts.stat || opts.fs.stat || fs.stat
  17. opts.statAsync = promisify(opts.stat)
  18. opts.statSync = opts.statSync || opts.fs.statSync || fs.statSync
  19. opts.mkdirSync = opts.mkdirSync || opts.fs.mkdirSync || fs.mkdirSync
  20. return opts
  21. }
  22. module.exports = optsArg