rimraf-dir.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. const log = require("npmlog");
  3. const path = require("path");
  4. const pathExists = require("path-exists");
  5. const childProcess = require("@lerna/child-process");
  6. // NOTE: if rimraf moves the location of its executable, this will need to be updated
  7. const RIMRAF_CLI = require.resolve("rimraf/bin");
  8. module.exports.rimrafDir = rimrafDir;
  9. function rimrafDir(dirPath) {
  10. log.silly("rimrafDir", dirPath);
  11. // Shelling out to a child process for a noop is expensive.
  12. // Checking if `dirPath` exists to be removed is cheap.
  13. // This lets us short-circuit if we don't have anything to do.
  14. return pathExists(dirPath).then((exists) => {
  15. if (!exists) {
  16. return;
  17. }
  18. // globs only return directories with a trailing slash
  19. const slashed = path.normalize(`${dirPath}/`);
  20. const args = [RIMRAF_CLI, "--no-glob", slashed];
  21. // We call this resolved CLI path in the "path/to/node path/to/cli <..args>"
  22. // pattern to avoid Windows hangups with shebangs (e.g., WSH can't handle it)
  23. return childProcess.spawn(process.execPath, args).then(() => {
  24. log.verbose("rimrafDir", "removed", dirPath);
  25. return dirPath;
  26. });
  27. });
  28. }