2015-04-02 16:09:47 -04:00
|
|
|
var fs = require('fs');
|
2015-02-02 19:25:34 -05:00
|
|
|
var path = require('path');
|
2014-12-05 19:26:30 -05:00
|
|
|
var minimatch = require('minimatch');
|
2015-04-02 16:09:47 -04:00
|
|
|
var path = require('path');
|
|
|
|
var Q = require('q');
|
2014-12-05 19:26:30 -05:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
processToPromise: processToPromise,
|
|
|
|
streamToPromise: streamToPromise,
|
2015-02-02 19:25:34 -05:00
|
|
|
insertSrcFolder: insertSrcFolder,
|
2015-02-20 20:44:23 -05:00
|
|
|
filterByFile: filterByFile,
|
2015-02-23 20:56:47 -05:00
|
|
|
subDirs: subDirs,
|
|
|
|
forEachSubDir: forEachSubDir,
|
|
|
|
forEachSubDirSequential: forEachSubDirSequential
|
2014-12-05 19:26:30 -05:00
|
|
|
};
|
|
|
|
|
2015-02-23 20:56:47 -05:00
|
|
|
function subDirs(dir) {
|
2015-04-02 16:09:47 -04:00
|
|
|
return fs.readdirSync(dir).filter(function(file) {
|
|
|
|
return fs.statSync(path.join(dir, file)).isDirectory();
|
|
|
|
});
|
2015-02-23 20:56:47 -05:00
|
|
|
}
|
2014-12-05 19:26:30 -05:00
|
|
|
|
2015-02-20 20:44:23 -05:00
|
|
|
function forEachSubDir(dir, callback) {
|
2015-02-23 20:56:47 -05:00
|
|
|
var dirs = subDirs(dir);
|
|
|
|
return Q.all(dirs.map(function(subdir) {
|
|
|
|
return callback(path.join(dir, subdir));
|
2015-02-20 20:44:23 -05:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
2015-02-23 20:56:47 -05:00
|
|
|
function forEachSubDirSequential(dir, callback) {
|
|
|
|
var dirs = subDirs(dir);
|
|
|
|
return next(0);
|
|
|
|
|
|
|
|
function next(index) {
|
|
|
|
if (index < dirs.length) {
|
|
|
|
return callback(path.join(dir, dirs[index])).then(function() {
|
|
|
|
return next(index+1);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 19:26:30 -05:00
|
|
|
function processToPromise(process) {
|
|
|
|
var defer = Q.defer();
|
|
|
|
process.on('close', function(code) {
|
|
|
|
if (code) {
|
|
|
|
defer.reject(code);
|
|
|
|
} else {
|
|
|
|
defer.resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return defer.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
function streamToPromise(stream) {
|
|
|
|
var defer = Q.defer();
|
|
|
|
stream.on('end', defer.resolve);
|
|
|
|
stream.on('error', defer.reject);
|
|
|
|
return defer.promise;
|
|
|
|
}
|
|
|
|
|
2015-02-02 19:25:34 -05:00
|
|
|
function filterByFile(pathMapping, folder) {
|
|
|
|
var folderParts = folder.split(path.sep);
|
|
|
|
var match;
|
|
|
|
var lastPattern;
|
|
|
|
for (var pattern in pathMapping) {
|
|
|
|
if (minimatch(folder, pattern)) {
|
|
|
|
if (!lastPattern || lastPattern.length < pattern.length) {
|
|
|
|
match = pathMapping[pattern];
|
|
|
|
lastPattern = pattern;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match !== undefined) {
|
|
|
|
return match;
|
|
|
|
} else {
|
|
|
|
throw new Error('No entry for folder '+folder+' found in '+JSON.stringify(pathMapping));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function insertSrcFolder(plugins, srcFolderInsertion) {
|
2014-12-05 19:26:30 -05:00
|
|
|
return plugins.rename(function(file) {
|
2015-02-02 19:25:34 -05:00
|
|
|
var folder = file.dirname;
|
2015-02-18 17:39:52 -05:00
|
|
|
var srcDir = filterByFile(srcFolderInsertion, path.join(folder, file.basename));
|
2015-02-02 19:25:34 -05:00
|
|
|
if (srcDir) {
|
|
|
|
var folderParts = file.dirname.split(path.sep);
|
|
|
|
folder = [folderParts[0], srcDir].concat(folderParts.slice(1)).join(path.sep);
|
|
|
|
}
|
|
|
|
file.dirname = folder;
|
2014-12-05 19:26:30 -05:00
|
|
|
});
|
|
|
|
}
|