delete.js 865 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. var path = require('path');
  3. var globby = require('globby');
  4. var multimatch = require('multimatch');
  5. var util = require('../util');
  6. function deleteFile(path, store) {
  7. var file = store.get(path);
  8. file.state = 'deleted';
  9. file.contents = null;
  10. store.add(file);
  11. }
  12. module.exports = function (paths, options) {
  13. if (!Array.isArray(paths)) {
  14. paths = [paths];
  15. }
  16. paths = paths.map(function (filePath) {
  17. return path.resolve(filePath);
  18. });
  19. paths = util.globify(paths);
  20. options = options || {};
  21. var globOptions = options.globOptions || {};
  22. var files = globby.sync(paths, globOptions);
  23. files.forEach(function (file) {
  24. deleteFile(file, this.store);
  25. }.bind(this));
  26. this.store.each(function (file) {
  27. if (multimatch([file.path], paths).length !== 0) {
  28. deleteFile(file.path, this.store);
  29. }
  30. }.bind(this));
  31. };