-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (68 loc) · 2.24 KB
/
index.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
'use strict';
var Readable = require('stream').Readable;
var through2 = require('through2');
var File = require('vinyl');
var Tech = require('./lib/Tech');
var Node = require('./lib/Node');
var EnbNodeConfig = require('enb/lib/config/node-config');
var EnbShim = function(opts) {
this._bundle = opts.bundle;
this._langs = opts.langs;
this._cwd = opts.cwd;
}
EnbShim.prototype.tech = function(originalTech, techOpts, _opts) {
if (!originalTech.buildFlow) {
throw new Error('Tech is not supported');
}
var self = this;
var opts = _opts || {};
var resStream = new Readable({ objectMode: true });
resStream._read = function(){};
var nodeConfig = new EnbNodeConfig();
nodeConfig.setLanguages(this._langs);
var techOptions = nodeConfig._processTechOptions(techOpts);
var node = new Node({
bundle: this._bundle,
cwd: this._cwd,
sources: opts.sources || {},
depsByTech: opts.depsByTech || '*'
});
Promise.all(techOptions.map(function(techOption) {
var tech = Tech.patch(originalTech, techOption);
return self.run(tech, originalTech.buildFlow()._usages, node)
.then(function(file) {
resStream.push(file);
})
}))
.then(function() {
resStream.push(null);
}, function(err) {
console.log('err', err, err.stack)
});
return resStream;
}
EnbShim.prototype.run = function(tech, usages, node) {
tech.init(node);
return node.patchUsages(tech, usages)
.then(function() {
node._registerTarget(tech._target, {
getName: function() { return tech._name }
});
var resPromise = node._getTarget(tech._target).deferred.promise().then(function(res) {
var file = new File({
cwd: undefined,
base: undefined,
path: tech._target,
contents: new Buffer(res)
});
return file;
});
tech.build().then(function() {}, function (err) {
console.log('err', err, err.stack);
});
return resPromise;
});
}
module.exports = function(opts) {
return new EnbShim(opts);
};