wiredep.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. 'use strict';
  2. var $ = {
  3. _: require('lodash'),
  4. 'bower-config': require('bower-config'),
  5. fs: require('fs'),
  6. glob: require('glob'),
  7. lodash: require('lodash'),
  8. path: require('path'),
  9. through2: require('through2')
  10. };
  11. var config;
  12. var helpers = require('./lib/helpers');
  13. var fileTypesDefault = require('./lib/default-file-types');
  14. /**
  15. * Wire up the html files with the Bower packages.
  16. *
  17. * @param {object} config the global configuration object
  18. */
  19. function wiredep(opts) {
  20. opts = opts || {};
  21. var cwd = opts.cwd ? $.path.resolve(opts.cwd) : process.cwd();
  22. config = module.exports.config = helpers.createStore();
  23. config.set
  24. ('on-error', opts.onError || function(err) { throw new Error(err); })
  25. ('on-file-updated', opts.onFileUpdated || function() {})
  26. ('on-main-not-found', opts.onMainNotFound || function() {})
  27. ('on-path-injected', opts.onPathInjected || function() {});
  28. config.set
  29. ('bower.json', opts.bowerJson || JSON.parse($.fs.readFileSync($.path.join(cwd, './bower.json'))))
  30. ('bower-directory', opts.directory || findBowerDirectory(cwd))
  31. ('cwd', cwd)
  32. ('dependencies', opts.dependencies === false ? false : true)
  33. ('detectable-file-types', [])
  34. ('dev-dependencies', opts.devDependencies)
  35. ('exclude', Array.isArray(opts.exclude) ? opts.exclude : [ opts.exclude ])
  36. ('file-types', mergeFileTypesWithDefaults(opts.fileTypes))
  37. ('global-dependencies', helpers.createStore())
  38. ('ignore-path', opts.ignorePath)
  39. ('include-self', opts.includeSelf)
  40. ('overrides', $._.extend({}, config.get('bower.json').overrides, opts.overrides))
  41. ('src', [])
  42. ('stream', opts.stream ? opts.stream : {});
  43. $._.pluck(config.get('file-types'), 'detect').
  44. forEach(function (fileType) {
  45. Object.keys(fileType).
  46. forEach(function (detectableFileType) {
  47. var detectableFileTypes = config.get('detectable-file-types');
  48. if (detectableFileTypes.indexOf(detectableFileType) === -1) {
  49. config.set('detectable-file-types', detectableFileTypes.concat(detectableFileType));
  50. }
  51. });
  52. });
  53. if (!opts.stream && opts.src) {
  54. (Array.isArray(opts.src) ? opts.src : [opts.src]).
  55. forEach(function (pattern) {
  56. config.set('src', config.get('src').concat($.glob.sync(pattern)));
  57. });
  58. }
  59. require('./lib/detect-dependencies')(config);
  60. require('./lib/inject-dependencies')(config);
  61. return config.get('stream').src ||
  62. Object.keys(config.get('global-dependencies-sorted')).
  63. reduce(function (acc, depType) {
  64. if (config.get('global-dependencies-sorted')[depType].length) {
  65. acc[depType] = config.get('global-dependencies-sorted')[depType];
  66. }
  67. return acc;
  68. }, { packages: config.get('global-dependencies').get() });
  69. }
  70. function mergeFileTypesWithDefaults(optsFileTypes) {
  71. var fileTypes = $._.clone(fileTypesDefault, true);
  72. $._(optsFileTypes).each(function (fileTypeConfig, fileType) {
  73. fileTypes[fileType] = fileTypes[fileType] || {};
  74. $._.each(fileTypeConfig, function (config, configKey) {
  75. if ($._.isPlainObject(fileTypes[fileType][configKey])) {
  76. fileTypes[fileType][configKey] =
  77. $._.assign(fileTypes[fileType][configKey], config);
  78. } else {
  79. fileTypes[fileType][configKey] = config;
  80. }
  81. });
  82. });
  83. return fileTypes;
  84. }
  85. function findBowerDirectory(cwd) {
  86. var directory = $.path.join(cwd, ($['bower-config'].read(cwd).directory || 'bower_components'));
  87. if (!$.fs.existsSync(directory)) {
  88. var error = new Error('Cannot find where you keep your Bower packages.');
  89. error.code = 'BOWER_COMPONENTS_MISSING';
  90. config.get('on-error')(error);
  91. }
  92. return directory;
  93. }
  94. wiredep.stream = function (opts) {
  95. opts = opts || {};
  96. return $.through2.obj(function (file, enc, cb) {
  97. if (file.isNull()) {
  98. this.push(file);
  99. return cb();
  100. }
  101. if (file.isStream()) {
  102. this.emit('error', 'Streaming not supported');
  103. return cb();
  104. }
  105. try {
  106. opts.stream = {
  107. src: file.contents.toString(),
  108. path: file.path,
  109. fileType: $.path.extname(file.path).substr(1)
  110. };
  111. file.contents = new Buffer(wiredep(opts));
  112. } catch (err) {
  113. this.emit('error', err);
  114. }
  115. this.push(file);
  116. cb();
  117. });
  118. };
  119. module.exports = wiredep;