removeIgnores.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var path = require('path');
  2. var rimraf = require('rimraf');
  3. var fstreamIgnore = require('fstream-ignore');
  4. var mout = require('mout');
  5. var Q = require('q');
  6. function removeIgnores(dir, meta) {
  7. var reader;
  8. var applyIgnores;
  9. var deferred = Q.defer();
  10. var ignored = [];
  11. var nonIgnored = ['bower.json'];
  12. // Don't ignore main files
  13. nonIgnored = nonIgnored.concat(meta.main || []);
  14. nonIgnored = nonIgnored.map(function (file) {
  15. return path.join(dir, file);
  16. });
  17. reader = fstreamIgnore({
  18. path: dir,
  19. type: 'Directory'
  20. });
  21. reader.addIgnoreRules(meta.ignore || []);
  22. // Monkey patch applyIgnores such that we get hold of all ignored files
  23. applyIgnores = reader.applyIgnores;
  24. reader.applyIgnores = function (entry) {
  25. var ret = applyIgnores.apply(this, arguments);
  26. if (!ret) {
  27. ignored.push(path.join(dir, entry));
  28. }
  29. return ret;
  30. };
  31. reader
  32. .on('child', function (entry) {
  33. nonIgnored.push(entry.path);
  34. })
  35. .on('error', deferred.reject)
  36. .on('end', function () {
  37. var promises;
  38. // Ensure that we are not ignoring files that should not be ignored!
  39. ignored = mout.array.unique(ignored);
  40. ignored = ignored.filter(function (file) {
  41. return nonIgnored.indexOf(file) === -1;
  42. });
  43. // Delete all the ignored files
  44. promises = ignored.map(function (file) {
  45. return Q.nfcall(rimraf, file);
  46. });
  47. return Q.all(promises)
  48. .then(deferred.resolve, deferred.reject);
  49. });
  50. return deferred.promise;
  51. }
  52. module.exports = removeIgnores;