deep-package-manager
Version:
DEEP Package Manager
87 lines (67 loc) • 2.09 kB
JavaScript
/**
* Created by AlexanderC on 2/19/16.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.StandardStrategy = undefined;
var _createOutputStream = require('create-output-stream');
var _createOutputStream2 = _interopRequireDefault(_createOutputStream);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _AbstractStrategy = require('./AbstractStrategy');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class StandardStrategy extends _AbstractStrategy.AbstractStrategy {
/**
* @param {*} args
*/
constructor(...args) {
super(...args);
this._advancedMatcher = null;
}
/**
* @param {String} shortDependencyName
* @returns {StandardStrategy}
*/
advancedMatcherFromDeepDepShortName(shortDependencyName) {
let dependencyBase = shortDependencyName.replace(/^deep(-microservices)?-/i, '');
let matchRegexp = new RegExp(`^\/?src\/(deep(-microservices)?-)?${dependencyBase}\/`, 'i');
this._advancedMatcher = filePath => {
return matchRegexp.test(filePath);
};
return this;
}
/**
* @param {String} filePath
* @param {Stream|Writable|Readable|stream.Readable|stream.Writable|*} stream
* @param {Function} cb
*/
extract(filePath, stream, cb) {
if (!this._haveToDump(filePath)) {
stream.resume().on('end', cb);
return;
}
let file = _path2.default.join(this.dumpPath, StandardStrategy.normalizeFilePath(filePath));
let output = (0, _createOutputStream2.default)(file);
output.on('finish', cb);
stream.pipe(output);
}
/**
* @param {String} filePath
* @returns {Boolean}
* @private
*/
_haveToDump(filePath) {
return (/^\/?src\/[A-Z][^\/]+\//.test(filePath) || this._advancedMatcher && this._advancedMatcher(filePath)
);
}
/**
* @param {String} filePath
* @returns {String}
*/
static normalizeFilePath(filePath) {
return filePath.replace(/^(\/?src\/[^\/]+\/)/i, '');
}
}
exports.StandardStrategy = StandardStrategy;