@nashiko/gulp-js-parent
Version:
Rebuild only changed js/ts files and all it's dependencies
91 lines (76 loc) • 2.39 kB
JavaScript
;
var path = require('path');
var es = require('event-stream');
var _ = require('lodash');
var fs = require('fs');
var Vinyl = require('vinyl');
var jsGraph = require('@nashiko/js-graph-dependency');
var stream;
function gulpJsParent(options) {
options = options || {};
if (typeof options.dir !== 'string' && !Array.isArray(options.dir)) {
throw new Error('gulp-js-parent: Missing dir in options');
}
if (typeof options.dir === 'string') {
options.dir = [options.dir];
}
var files = [];
function writeStream(currentFile) {
if (currentFile && !currentFile.isDirectory() && currentFile.contents.length) {
files.push(currentFile);
}
}
function recureOnImports(acc,graph,filePath){
var fullpaths = graph.index[filePath].importedBy
return fullpaths.reduce(function(acc,thePath){
return acc.concat(thePath, graph.index[thePath].importedBy.reduce(function(acc, aPath){
return acc.concat(aPath, recureOnImports([], graph, aPath))
},[]))
},acc)
}
function endStream() {
var stream = this;
if (files.length) {
var allPaths = _.map(files, 'path');
var newFiles = files;
_.forEach(options.dir, function(dir) {
var graph = jsGraph.parseDir(dir, options);
_.forEach(files, function(file) {
if (graph.index && graph.index[file.path]) {
var fullpaths = recureOnImports([],graph, file.path);
fullpaths.forEach(function (fp) {
if (!_.includes(allPaths, fp)) {
allPaths.push(fp);
newFiles.push(new Vinyl({
cwd: file.cwd,
base: path.resolve(dir),
path: fp,
stat: fs.statSync(fp),
contents: fs.readFileSync(fp)
}));
}
});
if (options.debug) {
console.log('File', file.path);
console.log(' - importedBy', fullpaths);
}
}
});
});
es.readArray(files)
.pipe(es.through(
function (f) {
stream.emit('data', f);
},
function () {
stream.emit('end');
}
));
} else {
stream.emit('end');
}
}
stream = es.through(writeStream, endStream);
return stream;
}
module.exports = gulpJsParent;