928ae6e1c794960177339f18b83efd1888c4c65a9e27a5f69bb44f745dd03887ae02356751ed13c85ec59360b251368efa1ae59a434aa3e1f1867f6f31be4a 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { mkdir, mkdirSync, stat, statSync, } from 'fs';
  2. export const optsArg = (opts) => {
  3. if (!opts) {
  4. opts = { mode: 0o777 };
  5. }
  6. else if (typeof opts === 'object') {
  7. opts = { mode: 0o777, ...opts };
  8. }
  9. else if (typeof opts === 'number') {
  10. opts = { mode: opts };
  11. }
  12. else if (typeof opts === 'string') {
  13. opts = { mode: parseInt(opts, 8) };
  14. }
  15. else {
  16. throw new TypeError('invalid options argument');
  17. }
  18. const resolved = opts;
  19. const optsFs = opts.fs || {};
  20. opts.mkdir = opts.mkdir || optsFs.mkdir || mkdir;
  21. opts.mkdirAsync = opts.mkdirAsync
  22. ? opts.mkdirAsync
  23. : async (path, options) => {
  24. return new Promise((res, rej) => resolved.mkdir(path, options, (er, made) => er ? rej(er) : res(made)));
  25. };
  26. opts.stat = opts.stat || optsFs.stat || stat;
  27. opts.statAsync = opts.statAsync
  28. ? opts.statAsync
  29. : async (path) => new Promise((res, rej) => resolved.stat(path, (err, stats) => (err ? rej(err) : res(stats))));
  30. opts.statSync = opts.statSync || optsFs.statSync || statSync;
  31. opts.mkdirSync = opts.mkdirSync || optsFs.mkdirSync || mkdirSync;
  32. return resolved;
  33. };
  34. //# sourceMappingURL=opts-arg.js.map