rimraf-posix.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. // the simple recursive removal, where unlink and rmdir are atomic
  3. // Note that this approach does NOT work on Windows!
  4. // We stat first and only unlink if the Dirent isn't a directory,
  5. // because sunos will let root unlink a directory, and some
  6. // SUPER weird breakage happens as a result.
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.rimrafPosixSync = exports.rimrafPosix = void 0;
  9. const fs_js_1 = require("./fs.js");
  10. const { lstat, rmdir, unlink } = fs_js_1.promises;
  11. const path_1 = require("path");
  12. const readdir_or_error_js_1 = require("./readdir-or-error.js");
  13. const ignore_enoent_js_1 = require("./ignore-enoent.js");
  14. const rimrafPosix = async (path, opt) => {
  15. if (opt?.signal?.aborted) {
  16. throw opt.signal.reason;
  17. }
  18. try {
  19. return await rimrafPosixDir(path, opt, await lstat(path));
  20. }
  21. catch (er) {
  22. if (er?.code === 'ENOENT')
  23. return true;
  24. throw er;
  25. }
  26. };
  27. exports.rimrafPosix = rimrafPosix;
  28. const rimrafPosixSync = (path, opt) => {
  29. if (opt?.signal?.aborted) {
  30. throw opt.signal.reason;
  31. }
  32. try {
  33. return rimrafPosixDirSync(path, opt, (0, fs_js_1.lstatSync)(path));
  34. }
  35. catch (er) {
  36. if (er?.code === 'ENOENT')
  37. return true;
  38. throw er;
  39. }
  40. };
  41. exports.rimrafPosixSync = rimrafPosixSync;
  42. const rimrafPosixDir = async (path, opt, ent) => {
  43. if (opt?.signal?.aborted) {
  44. throw opt.signal.reason;
  45. }
  46. const entries = ent.isDirectory() ? await (0, readdir_or_error_js_1.readdirOrError)(path) : null;
  47. if (!Array.isArray(entries)) {
  48. // this can only happen if lstat/readdir lied, or if the dir was
  49. // swapped out with a file at just the right moment.
  50. /* c8 ignore start */
  51. if (entries) {
  52. if (entries.code === 'ENOENT') {
  53. return true;
  54. }
  55. if (entries.code !== 'ENOTDIR') {
  56. throw entries;
  57. }
  58. }
  59. /* c8 ignore stop */
  60. if (opt.filter && !(await opt.filter(path, ent))) {
  61. return false;
  62. }
  63. await (0, ignore_enoent_js_1.ignoreENOENT)(unlink(path));
  64. return true;
  65. }
  66. const removedAll = (await Promise.all(entries.map(ent => rimrafPosixDir((0, path_1.resolve)(path, ent.name), opt, ent)))).reduce((a, b) => a && b, true);
  67. if (!removedAll) {
  68. return false;
  69. }
  70. // we don't ever ACTUALLY try to unlink /, because that can never work
  71. // but when preserveRoot is false, we could be operating on it.
  72. // No need to check if preserveRoot is not false.
  73. if (opt.preserveRoot === false && path === (0, path_1.parse)(path).root) {
  74. return false;
  75. }
  76. if (opt.filter && !(await opt.filter(path, ent))) {
  77. return false;
  78. }
  79. await (0, ignore_enoent_js_1.ignoreENOENT)(rmdir(path));
  80. return true;
  81. };
  82. const rimrafPosixDirSync = (path, opt, ent) => {
  83. if (opt?.signal?.aborted) {
  84. throw opt.signal.reason;
  85. }
  86. const entries = ent.isDirectory() ? (0, readdir_or_error_js_1.readdirOrErrorSync)(path) : null;
  87. if (!Array.isArray(entries)) {
  88. // this can only happen if lstat/readdir lied, or if the dir was
  89. // swapped out with a file at just the right moment.
  90. /* c8 ignore start */
  91. if (entries) {
  92. if (entries.code === 'ENOENT') {
  93. return true;
  94. }
  95. if (entries.code !== 'ENOTDIR') {
  96. throw entries;
  97. }
  98. }
  99. /* c8 ignore stop */
  100. if (opt.filter && !opt.filter(path, ent)) {
  101. return false;
  102. }
  103. (0, ignore_enoent_js_1.ignoreENOENTSync)(() => (0, fs_js_1.unlinkSync)(path));
  104. return true;
  105. }
  106. let removedAll = true;
  107. for (const ent of entries) {
  108. const p = (0, path_1.resolve)(path, ent.name);
  109. removedAll = rimrafPosixDirSync(p, opt, ent) && removedAll;
  110. }
  111. if (opt.preserveRoot === false && path === (0, path_1.parse)(path).root) {
  112. return false;
  113. }
  114. if (!removedAll) {
  115. return false;
  116. }
  117. if (opt.filter && !opt.filter(path, ent)) {
  118. return false;
  119. }
  120. (0, ignore_enoent_js_1.ignoreENOENTSync)(() => (0, fs_js_1.rmdirSync)(path));
  121. return true;
  122. };
  123. //# sourceMappingURL=rimraf-posix.js.map