index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { optArg, optArgSync } from './opt-arg.js';
  2. import pathArg from './path-arg.js';
  3. import { glob, globSync } from 'glob';
  4. const typeOrUndef = (val, t) => typeof val === 'undefined' || typeof val === t;
  5. export const isRimrafOptions = (o) => !!o &&
  6. typeof o === 'object' &&
  7. typeOrUndef(o.preserveRoot, 'boolean') &&
  8. typeOrUndef(o.tmp, 'string') &&
  9. typeOrUndef(o.maxRetries, 'number') &&
  10. typeOrUndef(o.retryDelay, 'number') &&
  11. typeOrUndef(o.backoff, 'number') &&
  12. typeOrUndef(o.maxBackoff, 'number') &&
  13. (typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) &&
  14. typeOrUndef(o.filter, 'function');
  15. export const assertRimrafOptions = (o) => {
  16. if (!isRimrafOptions(o)) {
  17. throw new Error('invalid rimraf options');
  18. }
  19. };
  20. import { rimrafManual, rimrafManualSync } from './rimraf-manual.js';
  21. import { rimrafMoveRemove, rimrafMoveRemoveSync } from './rimraf-move-remove.js';
  22. import { rimrafNative, rimrafNativeSync } from './rimraf-native.js';
  23. import { rimrafPosix, rimrafPosixSync } from './rimraf-posix.js';
  24. import { rimrafWindows, rimrafWindowsSync } from './rimraf-windows.js';
  25. import { useNative, useNativeSync } from './use-native.js';
  26. const wrap = (fn) => async (path, opt) => {
  27. const options = optArg(opt);
  28. if (options.glob) {
  29. path = await glob(path, options.glob);
  30. }
  31. if (Array.isArray(path)) {
  32. return !!(await Promise.all(path.map(p => fn(pathArg(p, options), options)))).reduce((a, b) => a && b, true);
  33. }
  34. else {
  35. return !!(await fn(pathArg(path, options), options));
  36. }
  37. };
  38. const wrapSync = (fn) => (path, opt) => {
  39. const options = optArgSync(opt);
  40. if (options.glob) {
  41. path = globSync(path, options.glob);
  42. }
  43. if (Array.isArray(path)) {
  44. return !!path
  45. .map(p => fn(pathArg(p, options), options))
  46. .reduce((a, b) => a && b, true);
  47. }
  48. else {
  49. return !!fn(pathArg(path, options), options);
  50. }
  51. };
  52. export const nativeSync = wrapSync(rimrafNativeSync);
  53. export const native = Object.assign(wrap(rimrafNative), { sync: nativeSync });
  54. export const manualSync = wrapSync(rimrafManualSync);
  55. export const manual = Object.assign(wrap(rimrafManual), { sync: manualSync });
  56. export const windowsSync = wrapSync(rimrafWindowsSync);
  57. export const windows = Object.assign(wrap(rimrafWindows), { sync: windowsSync });
  58. export const posixSync = wrapSync(rimrafPosixSync);
  59. export const posix = Object.assign(wrap(rimrafPosix), { sync: posixSync });
  60. export const moveRemoveSync = wrapSync(rimrafMoveRemoveSync);
  61. export const moveRemove = Object.assign(wrap(rimrafMoveRemove), {
  62. sync: moveRemoveSync,
  63. });
  64. export const rimrafSync = wrapSync((path, opt) => useNativeSync(opt) ? rimrafNativeSync(path, opt) : rimrafManualSync(path, opt));
  65. export const sync = rimrafSync;
  66. export const rimraf = Object.assign(wrap((path, opt) => useNative(opt) ? rimrafNative(path, opt) : rimrafManual(path, opt)), {
  67. // this weirdness because it's easier than explicitly declaring
  68. rimraf: manual,
  69. sync: rimrafSync,
  70. rimrafSync: rimrafSync,
  71. manual,
  72. manualSync,
  73. native,
  74. nativeSync,
  75. posix,
  76. posixSync,
  77. windows,
  78. windowsSync,
  79. moveRemove,
  80. moveRemoveSync,
  81. });
  82. rimraf.rimraf = rimraf;
  83. export default rimraf;
  84. //# sourceMappingURL=index.js.map