diffjam
Version:
cli for diffjam.com
145 lines (144 loc) • 6.72 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Policy = void 0;
var lodash_1 = require("lodash");
var micromatch_1 = __importDefault(require("micromatch"));
var hasProp_1 = require("./hasProp");
var regexPrefix = "regex:";
var inversePrefix = "-:";
var escapeStringRegexp = function (str) {
return str
.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&")
.replace(/-/g, "\\x2d");
};
var Policy = /** @class */ (function () {
function Policy(name, description, filePattern, search, baseline, ignoreFilePatterns, hiddenFromOutput) {
if (hiddenFromOutput === void 0) { hiddenFromOutput = false; }
this.name = name;
this.description = description;
this.baseline = baseline;
this.hiddenFromOutput = hiddenFromOutput;
this.ran = false;
this.search = Array.isArray(search) ? search : [search];
this.filePattern = Array.isArray(filePattern) ? filePattern : [filePattern];
try {
this.needles = Policy.searchConfigToNeedles(this.search);
}
catch (err) {
if (err instanceof Error) {
throw new Error("Error in policy (".concat(name, "): ").concat(err.message));
}
else {
throw new Error("Error in policy (".concat(name, ")"));
}
}
if (ignoreFilePatterns && ignoreFilePatterns.length) {
if (!Array.isArray(ignoreFilePatterns)) {
this.ignoreFilePatterns = [ignoreFilePatterns];
}
else {
this.ignoreFilePatterns = ignoreFilePatterns;
}
}
}
Policy.prototype.toJson = function () {
var json = {
description: this.description,
filePattern: this.filePattern.length === 1 ? this.filePattern[0] : this.filePattern,
search: this.search.length === 1 ? this.search[0] : this.search,
baseline: this.baseline,
};
if (this.hiddenFromOutput)
json.hiddenFromOutput = this.hiddenFromOutput;
if (this.ignoreFilePatterns)
json.ignoreFilePatterns = this.ignoreFilePatterns;
return json;
};
Policy.prototype.isFileUnderPolicy = function (filePath) {
var _this = this;
return this.filePattern.some(function (filePattern) {
return micromatch_1.default.any(filePath, filePattern) && (!_this.ignoreFilePatterns ||
!micromatch_1.default.any(filePath, _this.ignoreFilePatterns));
});
};
Policy.prototype.processFile = function (file, onMatch) {
var _this = this;
if (!this.isFileUnderPolicy(file.path))
return;
return file.findMatches(this.needles, function (match) { return onMatch(match, _this); });
};
Policy.prototype.isCountAcceptable = function (matches) {
return matches.length <= this.baseline;
};
Policy.prototype.isCountCinchable = function (matches) {
return matches.length < this.baseline;
};
// Converts array of `search` strings to a `Needles` object. This will include a regex to search for
// but also includes other search terms that may be used that will determine if we either include or exclude
// a match.
Policy.searchConfigToNeedles = function (search) {
var _a = (0, lodash_1.partition)(search, function (term) { return term.startsWith(inversePrefix); }), inverseTerms = _a[0], positiveTerms = _a[1];
var _b = (0, lodash_1.partition)(positiveTerms, function (term) { return term.startsWith(regexPrefix); }), regexTerms = _b[0], simplePositiveTerms = _b[1];
if (regexTerms.length) {
if (inverseTerms.length) {
throw new Error("regex search terms (".concat(regexTerms[0], ") cannot be combined with inverse search terms (").concat(inverseTerms[0], ")"));
}
}
else if (!simplePositiveTerms.length) {
throw new Error("no positive search terms found");
}
var _c = regexTerms.map(function (term) { return term.slice(regexPrefix.length); }), firstRegexTerm = _c[0], otherRegexTerms = _c.slice(1);
var positive = simplePositiveTerms.map(escapeStringRegexp);
return {
regex: new RegExp(firstRegexTerm || positive.shift(), "gm"),
negative: inverseTerms.map(function (term) { return term.slice(inversePrefix.length); }),
positive: positive,
otherRegexes: otherRegexTerms.map(function (term) { return new RegExp(term); }),
};
};
Policy.fromJson = function (name, obj) {
try {
if (!obj) {
throw new Error("input was empty");
}
if (!(0, hasProp_1.hasProp)(obj, "baseline")) {
throw new Error("baseline is required");
}
if (!(0, lodash_1.isNumber)(obj.baseline)) {
throw new Error("baseline must be a number");
}
if (!(0, hasProp_1.hasProp)(obj, "search")) {
throw new Error("search is required");
}
if (!((0, lodash_1.isString)(obj.search) || (Array.isArray(obj.search) && obj.search.every(lodash_1.isString)))) {
throw new Error("search must be a string or an array of strings");
}
if (!((0, lodash_1.isString)(obj.filePattern) || (Array.isArray(obj.filePattern) && obj.filePattern.every(lodash_1.isString)))) {
throw new Error("filePattern must be a string or an array of strings");
}
if (!(0, hasProp_1.hasProp)(obj, "description")) {
throw new Error("description is required");
}
if (!(0, lodash_1.isString)(obj.description)) {
throw new Error("description must be a string");
}
if ((0, hasProp_1.hasProp)(obj, "hiddenFromOutput") && !(0, lodash_1.isBoolean)(obj.hiddenFromOutput)) {
throw new Error("hiddenFromOutput must be a boolean");
}
return new Policy(name, obj.description, obj.filePattern, obj.search, obj.baseline, obj.ignoreFilePatterns, Boolean(obj.hiddenFromOutput));
}
catch (err) {
if (err instanceof Error) {
throw new Error("Error in policy (".concat(name, "): ").concat(err.message));
}
else {
throw new Error("Error in policy (".concat(name, ")"));
}
}
};
return Policy;
}());
exports.Policy = Policy;