direct-dev
Version:
Yandex Direct dev tools
54 lines (42 loc) • 1.77 kB
JavaScript
/** [transporter](https://github.com/direct-adv-interfaces/direct-dev/TECHS.md#transporter) */
var buildFlow = require('enb/lib/build-flow');
var require2 = require('../utils/require2');
var vow = require2('enb/node_modules/vow', 'vow');
var vowFs = require2('enb/node_modules/vow-fs', 'vow-fs');
var lodash = require('lodash');
var streamify = require('stream-array');
var enbDest = require('./transporter-plugins/enb-dest');
var cache = {};
function readFile(filePath) {
return vowFs.read(filePath, 'utf8').then(function (contents) {
return { path: filePath, contents: contents };
});
}
function getCachedFile(filePath) {
return cache[filePath] || (cache[filePath] = readFile(filePath));
}
function provideStream(paths) {
return vow.all(paths.map(function (file) {
return readFile(file.fullname);
})).then(function (files) {
return streamify(files);
}, function (err) {
console.log(err);
});
}
module.exports = function (ext) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var transporter = buildFlow.create().name('transporter: ' + ext).target('target', '?.merged.js').defineOption('apply').useFileList(ext);
options.noCache && (transporter = transporter.needRebuild(lodash.constant(true)));
return transporter.builder(function (files) {
var deferred = vow.defer();
var plugins = [].concat(this.getOption('apply', [])).filter(Boolean);
provideStream(files).then(function (fStream) {
plugins.reduce(function (stream, plugin) {
return stream.pipe(plugin);
}, fStream).pipe(enbDest(deferred));
});
return deferred.promise();
}).createTech();
};
;