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