ignorefs
Version:
Ignore common and custom patterns of the file system
208 lines (207 loc) • 8.88 kB
JavaScript
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.upgradeOptions = exports.isIgnoredPath = void 0;
// builtin
var path_1 = require("path");
// external
var ignorepatterns_1 = __importDefault(require("ignorepatterns"));
/** Is the path relative? */
function isRelative(path) {
if (!path)
return false;
return !(0, path_1.isAbsolute)(path);
}
/** Is the path a basename? */
function isBasename(path) {
if (!path)
return false;
return !path.includes(path_1.sep);
}
/** Tests the path against provided prefixes and regular expressions */
function matches(path, matches) {
var e_1, _a;
try {
for (var matches_1 = __values(matches), matches_1_1 = matches_1.next(); !matches_1_1.done; matches_1_1 = matches_1.next()) {
var match = matches_1_1.value;
if (typeof match === 'string') {
if (path.startsWith(match))
return true;
}
else if (match.test(path))
return true;
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (matches_1_1 && !matches_1_1.done && (_a = matches_1.return)) _a.call(matches_1);
}
finally { if (e_1) throw e_1.error; }
}
return false;
}
/** Tests whether the path should be ignored */
function isIgnoredPath(path, opts) {
var _a, _b, _c, _d, _e, _f;
if (opts === void 0) { opts = {
ignoreUndesiredBasenames: true,
}; }
// handle deprecations
if (opts.ignoreHiddenFiles != null)
throw new Error('ignorefs: ignoreHiddenFiles is deprecated, use ignoreHiddenBasenames instead, otherwise use the default export for a compatibility layer');
if (opts.ignoreCommonPatterns != null)
throw new Error('ignorefs: ignoreCommonPatterns is deprecated, use ignoreUndesiredBasenames instead, otherwise use the default export for a compatibility layer');
if (opts.ignorePaths != null)
throw new Error('ignorefs: ignorePaths is deprecated, use ignoreAbsolutePaths, ignoreRelativePaths, and ignoreBasenames instead, otherwise use the default export for a compatibility layer');
// extract components of the path
var absolutePath = path.absolutePath, relativePath = path.relativePath, basename = path.basename;
// custom callback
if (opts.ignoreCustomCallback && opts.ignoreCustomCallback(path) === true)
return true;
// absolute path checks
if (absolutePath) {
// match
if (((_a = opts.ignoreAbsolutePaths) === null || _a === void 0 ? void 0 : _a.length) &&
matches(absolutePath, opts.ignoreAbsolutePaths))
return true;
// custom?
if ((_b = opts.ignoreCustomPatterns) === null || _b === void 0 ? void 0 : _b.test(absolutePath))
return true;
}
// relative path checks
if (relativePath) {
// match
if (((_c = opts.ignoreRelativePaths) === null || _c === void 0 ? void 0 : _c.length) &&
matches(relativePath, opts.ignoreRelativePaths))
return true;
// custom?
if ((_d = opts.ignoreCustomPatterns) === null || _d === void 0 ? void 0 : _d.test(relativePath))
return true;
}
// basename checks
if (basename) {
// match
if (((_e = opts.ignoreBasenames) === null || _e === void 0 ? void 0 : _e.length) && matches(basename, opts.ignoreBasenames))
return true;
// hidden?
if (opts.ignoreHiddenBasenames && basename[0] === '.')
return true;
// common?
if (opts.ignoreUndesiredBasenames &&
ignorepatterns_1.default.test(basename))
return true;
// custom?
if ((_f = opts.ignoreCustomPatterns) === null || _f === void 0 ? void 0 : _f.test(basename))
return true;
}
// not ignored
return false;
}
exports.isIgnoredPath = isIgnoredPath;
/** Verify options and upgrade deprecations, returns dereferenced copy */
function upgradeOptions(opts) {
opts = Object.assign({}, opts);
if (opts.ignoreHiddenFiles != null) {
opts.ignoreHiddenBasenames = opts.ignoreHiddenFiles;
delete opts.ignoreHiddenFiles;
}
if (opts.ignoreCommonPatterns != null) {
opts.ignoreUndesiredBasenames = opts.ignoreCommonPatterns;
delete opts.ignoreCommonPatterns;
}
if (opts.ignorePaths) {
opts.ignoreAbsolutePaths = __spreadArray(__spreadArray([], __read((opts.ignoreAbsolutePaths || []).map(function (match) {
if (typeof match === 'string' && !(0, path_1.isAbsolute)(match))
throw new Error('ignorefs: ignoreAbsolutePaths should only contain absolute paths and regular expressions');
return match;
})), false), __read((opts.ignorePaths || []).filter(function (match) {
return typeof match === 'string' ? (0, path_1.isAbsolute)(match) : true;
})), false);
opts.ignoreRelativePaths = __spreadArray(__spreadArray([], __read((opts.ignoreRelativePaths || []).map(function (match) {
if (typeof match === 'string' && !isRelative(match))
throw new Error('ignorefs: ignoreRelativePaths should only contain relative paths and regular expressions');
return match;
})), false), __read((opts.ignorePaths || []).filter(function (match) {
return typeof match === 'string' ? isRelative(match) : true;
})), false);
opts.ignoreBasenames = __spreadArray(__spreadArray([], __read((opts.ignoreBasenames || []).map(function (match) {
if (typeof match === 'string' && !isBasename(match))
throw new Error('ignorefs: ignoreBasenames should only contain basebanes and regular expressions');
return match;
})), false), __read((opts.ignorePaths || []).filter(function (match) {
return typeof match === 'string' ? isBasename(path_1.sep) : true;
})), false);
delete opts.ignorePaths;
}
return opts;
}
exports.upgradeOptions = upgradeOptions;
/** Compatibility wrapper for {@link isIgnoredPath}, supporting path string, verifying path object and options, and handling option deprecations */
function isIgnoredPathCompatibility(path, opts) {
if (opts === void 0) { opts = {
ignoreUndesiredBasenames: true,
}; }
// adjust path
if (typeof path === 'string') {
if (!path)
throw new Error('ignorefs: path cannot be empty');
var result = {};
if ((0, path_1.isAbsolute)(path))
result.absolutePath = path;
else
result.relativePath = path;
result.basename = (0, path_1.basename)(path);
path = result;
}
else {
// verify
if (path.absolutePath && !(0, path_1.isAbsolute)(path.absolutePath))
throw new Error('ignorefs: path.absolutePath must be an absolute path');
if (path.relativePath && !isRelative(path.relativePath))
throw new Error('ignorefs: path.relativePath must be a relative path');
if (path.basename && !isBasename(path.basename))
throw new Error('ignorefs: path.basename must be a basename');
}
// upgrade options and return result from modern api
return isIgnoredPath(path, upgradeOptions(opts));
}
exports.default = isIgnoredPathCompatibility;
;