index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. const path = require("path");
  3. const pMap = require("p-map");
  4. const { Command } = require("@lerna/command");
  5. const { rimrafDir } = require("@lerna/rimraf-dir");
  6. const { promptConfirmation } = require("@lerna/prompt");
  7. const { getFilteredPackages } = require("@lerna/filter-options");
  8. const { pulseTillDone } = require("@lerna/pulse-till-done");
  9. module.exports = factory;
  10. function factory(argv) {
  11. return new CleanCommand(argv);
  12. }
  13. class CleanCommand extends Command {
  14. get requiresGit() {
  15. return false;
  16. }
  17. initialize() {
  18. let chain = Promise.resolve();
  19. chain = chain.then(() => getFilteredPackages(this.packageGraph, this.execOpts, this.options));
  20. chain = chain.then((filteredPackages) => {
  21. this.directoriesToDelete = filteredPackages.map((pkg) => pkg.nodeModulesLocation);
  22. });
  23. return chain.then(() => {
  24. if (this.options.yes) {
  25. return true;
  26. }
  27. this.logger.info("", "Removing the following directories:");
  28. this.logger.info(
  29. "clean",
  30. this.directoriesToDelete.map((dir) => path.relative(this.project.rootPath, dir)).join("\n")
  31. );
  32. return promptConfirmation("Proceed?");
  33. });
  34. }
  35. execute() {
  36. this.enableProgressBar();
  37. const tracker = this.logger.newItem("clean");
  38. const mapper = (dirPath) => {
  39. tracker.info("clean", "removing", dirPath);
  40. return pulseTillDone(rimrafDir(dirPath)).then(() => {
  41. tracker.completeWork(1);
  42. });
  43. };
  44. tracker.addWork(this.directoriesToDelete.length);
  45. return pMap(this.directoriesToDelete, mapper, { concurrency: this.concurrency }).then(() => {
  46. tracker.finish();
  47. this.logger.success("clean", "finished");
  48. });
  49. }
  50. }
  51. module.exports.CleanCommand = CleanCommand;