2016-10-04 16:15:49 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2015-04-02 16:09:47 -04:00
|
|
|
var fs = require('fs');
|
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-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);
|
2016-10-04 16:15:49 -04:00
|
|
|
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) {
|
2016-10-04 16:15:49 -04:00
|
|
|
return callback(path.join(dir, dirs[index])).then(function() { return next(index + 1); });
|
2015-02-23 20:56:47 -05:00
|
|
|
} 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 {
|
2016-10-04 16:15:49 -04:00
|
|
|
throw new Error('No entry for folder ' + folder + ' found in ' + JSON.stringify(pathMapping));
|
2015-02-02 19:25:34 -05:00
|
|
|
}
|
|
|
|
}
|