-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathresolver.js
355 lines (304 loc) · 6.68 KB
/
resolver.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* Module dependencies.
*/
var debug = require('debug')('component:builder:resolver');
var resolve = require('path').resolve;
var exists = require('fs').exists;
var read = require('fs').readFile;
var join = require('path').join;
var Batch = require('batch');
var utils = require('./utils');
/**
* Expose `Resolver`
*/
module.exports = Resolver;
/**
* Initialize a new `Resolver`.
*
* @param {String} path
* @param {Resolver} parent
* @param {Function} fn
* @api public
*/
function Resolver(path, parent, fn){
if (!(this instanceof Resolver)) return new Resolver(path, parent, fn);
if ('function' == typeof parent) fn = parent, parent = null;
this.path = path;
this.inherit(parent || {});
if (fn) this.end(fn);
}
/**
* Add global `path`.
*
* @param {String} path
* @return {Resolver}
* @api public
*/
Resolver.prototype.add = function(path){
this.paths.push(resolve(this.path, path));
return this;
};
/**
* Inherit from a `parent` resolver.
*
* @param {Object} parent
* @api private
*/
Resolver.prototype.inherit = function(parent){
this.paths = parent.paths || [this.path + '/components'];
this._ignored = parent._ignored || {};
this.cache = parent.cache || {};
this.list = parent.list || [];
this.parent = parent;
this.locals = [];
};
/**
* Allow `dev` dependencies.
*
* @api public
*/
Resolver.prototype.development = function(){
this.dev = true;
return this;
};
/**
* Check if `dep` is ignored.
*
* @param {String} dep
* @return {Boolean}
* @api private
*/
Resolver.prototype.ignored = function(dep){
dep = dep.replace('/', '-');
return this._ignored[dep];
};
/**
* Ignore `dep`.
*
* @param {String} dep
* @api private
*/
Resolver.prototype.ignore = function(dep){
if (this.ignored(dep)) return;
debug('ignore %s', dep);
dep = dep.replace('/', '-');
this._ignored[dep] = true;
return this;
};
/**
* Get the component conf and invoke `fn(err, obj)`.
*
* @param {Function} fn
* @ap private
*/
Resolver.prototype.json = function(fn){
var path = join(this.path, 'component.json');
var self = this;
read(path, 'utf-8', function(err, str){
if (err) return fn(err);
try {
self.conf = JSON.parse(str);
utils.normalizeConfig(self.conf);
self.configure(self.conf);
debug('pulled %s conf', self.conf.name);
fn(null, self.conf);
} catch (e) {
fn(e);
}
});
};
/**
* Resolve this component and all it's
* dependencies recursively and call `fn(err, all)`.
*
* @param {Function} fn
* @api private
*/
Resolver.prototype.end = function(fn){
var self = this;
self.json(function(err, conf){
if (err) return fn(err);
var batch = new Batch;
self.list.push(self);
batch.push(self.pull('templates'));
batch.push(self.pull('scripts'));
batch.push(self.pull('styles'));
batch.push(self.pull('json'));
batch.push(self.deps.bind(self));
batch.end(function(err){
if (err) return fn(err);
fn(null, self.map());
});
});
};
/**
* Resolve all deps and call `done(err)`.
*
* @param {Function} done
* @api private
*/
Resolver.prototype.deps = function(done){
var deps = this.dependencies();
var names = Object.keys(deps);
var batch = new Batch;
var self = this;
if (0 == deps.length) {
debug('skip %s deps', this.conf.name);
setImmediate(done);
return;
}
names.forEach(function(dep){
if (self.ignored(dep)) return;
self.ignore(dep);
batch.push(function(done){
self.resolve(dep, function(err, dir){
if (err) return done(err);
Resolver(dir, self, done);
});
});
});
batch.end(done);
};
/**
* Pull sources of `type`.
*
* @param {String} type
* @return {Function}
* @api public
*/
Resolver.prototype.pull = function(type){
var all = this.conf[type];
var self = this;
if (!this.conf[type]) {
debug('skip %s %s', this.conf.name, type);
return noop;
}
return function(done){
var batch = new Batch;
var files = [];
debug('%s pull %d %s', self.conf.name, all.length, type);
all.forEach(function(filename){
var resolved = resolve(self.path, filename);
batch.push(function(done){
read(resolved, 'utf8', function(err, str){
if (err) return done(err);
var obj = {};
obj.filename = filename;
obj.contents = str;
files.push(obj);
done();
});
});
});
batch.end(function(err){
if (err) return done(err);
self.conf[type] = files;
debug('%s pulled %d %s', self.conf.name, files.length, type);
done();
});
};
};
/**
* Lookup `dep` dir and invoke `fn(err, dir)`.
*
* @param {String} dep
* @param {Function} fn
* @api private
*/
Resolver.prototype.resolve = function(dep, fn){
this.locals = this.locals.concat(this.parent.locals);
var paths = this.paths.concat(this.locals);
var dep = dep.replace('/', '-');
var name = this.conf.name;
var cache = this.cache;
var self = this;
var i = 0;
function next(){
var path = paths[i++];
if (!path) return fn(new Error('failed to resolve "' + name + '"\'s dependency "' + dep + '"'));
var dir = join(path, dep);
var key = dep + ':' + dir;
if (cache[key]) return fn(null, cache[key]);
var json = join(dir, 'component.json');
exists(json, function(exists){
if (!exists) return next();
cache[key] = dir;
fn(null, dir);
});
}
next();
};
/**
* Map and normalize dependencies.
*
* @return {Array}
* @api public
*/
Resolver.prototype.dependencies = function(){
var deps = this.conf.dependencies || {};
if (this.dev) merge(deps, this.conf.development);
merge(deps, this.local());
return deps;
};
/**
* Get all local deps.
*
* @return {Object}
* @api private
*/
Resolver.prototype.local = function(){
var local = this.conf.local
|| this.conf.locals
|| [];
return local.reduce(function(ret, name){
ret[name] = '*';
return ret;
}, {});
};
/**
* Map component's `json`
*
* @return {Array}
* @api private
*/
Resolver.prototype.map = function(){
return this.list.map(function(resolver){
return resolver.conf;
});
};
/**
* Configure `conf`.
*
* @param {Object} conf
* @api private
*/
Resolver.prototype.configure = function(conf){
var paths = conf.paths || [];
var self = this;
this.locals = paths.map(function(path){
return resolve(self.path, path);
});
conf.path = function(path){
return 1 == arguments.length
? resolve(self.path, path)
: self.path;
};
};
/**
* Merge `a`, `b`.
*
* @param {Object} a
* @param {Object} b
* @return {Object}
* @api private
*/
function merge(a, b){
for (var k in b) a[k] = b[k];
return a;
}
/**
* Noop
*/
function noop(fn){
setImmediate(fn);
}