You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. var _ = require('lodash');
  2. var Emitter = require('events').EventEmitter;
  3. var path = require('path');
  4. var grunt = require('grunt');
  5. var BowerAssets = function(bower, cwd) {
  6. this.bower = bower;
  7. this.cwd = cwd;
  8. this.config = bower.config.json;
  9. };
  10. BowerAssets.prototype = Object.create(Emitter.prototype);
  11. BowerAssets.prototype.constructor = BowerAssets;
  12. BowerAssets.prototype.get = function() {
  13. var bower = this.bower;
  14. var bowerConfig = grunt.file.readJSON(path.join(this.cwd, this.config));
  15. var exportsOverride = bowerConfig.exportsOverride;
  16. var paths = bower.commands.list({paths: true});
  17. paths.on('data', function(data) {
  18. this.emit('data', this.mergePaths(data, exportsOverride ? exportsOverride : {}));
  19. }.bind(this));
  20. paths.on('error', function(err) {
  21. this.emit('error', err);
  22. }.bind(this));
  23. return this;
  24. };
  25. BowerAssets.prototype.mergePaths = function(allPaths, overrides) {
  26. var bowerAssets = {'__untyped__': {}};
  27. var cwd = this.cwd;
  28. var componentsLocation = this.bower.config.directory;
  29. _(allPaths).each(function(pkgPaths, pkg) {
  30. var pkgOverrides = overrides[pkg];
  31. if (pkgOverrides) {
  32. _(pkgOverrides).each(function(overriddenPaths, assetType) {
  33. bowerAssets[assetType] = bowerAssets[assetType] || {};
  34. var pkgPath = path.join(componentsLocation, pkg);
  35. var basePath = path.join(cwd, pkgPath);
  36. bowerAssets[assetType][pkg] = _(grunt.file.expand({cwd: basePath}, overriddenPaths)).map(function(expandedPath) {
  37. return path.join(pkgPath, expandedPath);
  38. });
  39. });
  40. } else {
  41. if (!_.isArray(pkgPaths)) {
  42. pkgPaths = [ pkgPaths ];
  43. }
  44. bowerAssets['__untyped__'][pkg] = pkgPaths;
  45. }
  46. }, this);
  47. return bowerAssets;
  48. };
  49. module.exports = BowerAssets;