opt-arg.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const typeOrUndef = (val, t) => typeof val === 'undefined' || typeof val === t;
  2. export const isRimrafOptions = (o) => !!o &&
  3. typeof o === 'object' &&
  4. typeOrUndef(o.preserveRoot, 'boolean') &&
  5. typeOrUndef(o.tmp, 'string') &&
  6. typeOrUndef(o.maxRetries, 'number') &&
  7. typeOrUndef(o.retryDelay, 'number') &&
  8. typeOrUndef(o.backoff, 'number') &&
  9. typeOrUndef(o.maxBackoff, 'number') &&
  10. (typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) &&
  11. typeOrUndef(o.filter, 'function');
  12. export const assertRimrafOptions = (o) => {
  13. if (!isRimrafOptions(o)) {
  14. throw new Error('invalid rimraf options');
  15. }
  16. };
  17. const optArgT = (opt) => {
  18. assertRimrafOptions(opt);
  19. const { glob, ...options } = opt;
  20. if (!glob) {
  21. return options;
  22. }
  23. const globOpt = glob === true ?
  24. opt.signal ?
  25. { signal: opt.signal }
  26. : {}
  27. : opt.signal ?
  28. {
  29. signal: opt.signal,
  30. ...glob,
  31. }
  32. : glob;
  33. return {
  34. ...options,
  35. glob: {
  36. ...globOpt,
  37. // always get absolute paths from glob, to ensure
  38. // that we are referencing the correct thing.
  39. absolute: true,
  40. withFileTypes: false,
  41. },
  42. };
  43. };
  44. export const optArg = (opt = {}) => optArgT(opt);
  45. export const optArgSync = (opt = {}) => optArgT(opt);
  46. //# sourceMappingURL=opt-arg.js.map