prune.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var mout = require('mout');
  2. var Project = require('../core/Project');
  3. var cli = require('../util/cli');
  4. var defaultConfig = require('../config');
  5. function prune(logger, options, config) {
  6. var project;
  7. options = options || {};
  8. config = defaultConfig(config);
  9. project = new Project(config, logger);
  10. return clean(project, options);
  11. }
  12. function clean(project, options, removed) {
  13. removed = removed || {};
  14. // Continually call clean until there is no more extraneous
  15. // dependencies to remove
  16. return project.getTree(options)
  17. .spread(function (tree, flattened, extraneous) {
  18. var names = extraneous.map(function (extra) {
  19. return extra.endpoint.name;
  20. });
  21. // Uninstall extraneous
  22. return project.uninstall(names, options)
  23. .then(function (uninstalled) {
  24. // Are we done?
  25. if (!mout.object.size(uninstalled)) {
  26. return removed;
  27. }
  28. // Not yet, recurse!
  29. mout.object.mixIn(removed, uninstalled);
  30. return clean(project, options, removed);
  31. });
  32. });
  33. }
  34. // -------------------
  35. prune.line = function (logger, argv) {
  36. var options = prune.options(argv);
  37. return prune(logger, options);
  38. };
  39. prune.options = function (argv) {
  40. return cli.readOptions({
  41. 'production': { type: Boolean, shorthand: 'p' },
  42. }, argv);
  43. };
  44. prune.completion = function () {
  45. // TODO:
  46. };
  47. module.exports = prune;