gulp-rmDir.js 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var path = require("path");
  2. var fs = require("fs");
  3. var rmDir = function(dirPath) {
  4. let files = null;
  5. try {
  6. files = fs.readdirSync(dirPath);
  7. }
  8. catch (e) {
  9. return;
  10. }
  11. if (files && files.length > 0) {
  12. for (var i = 0; i < files.length; i++) {
  13. var filePath = path.join(dirPath, files[i]);
  14. if (fs.statSync(filePath).isFile())
  15. fs.unlinkSync(filePath);
  16. else
  17. rmDir(filePath);
  18. }
  19. }
  20. fs.rmdirSync(dirPath);
  21. }
  22. module.exports = function(dirPath) {
  23. // Retry cause sometimes locked on my mac :-)
  24. try {
  25. rmDir(dirPath);
  26. }
  27. catch (e) {
  28. try {
  29. rmDir(dirPath);
  30. }
  31. catch (e) {
  32. try {
  33. rmDir(dirPath);
  34. }
  35. catch (e) {
  36. // Something is definitely wrong here.
  37. throw e;
  38. }
  39. }
  40. }
  41. };