ignorefs
Version:
Ignore common and custom patterns of the file system
161 lines (160 loc) • 6.53 kB
JavaScript
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
const path_1 = require("path");
// external
const 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) {
for (const match of matches) {
if (typeof match === 'string') {
if (path.startsWith(match))
return true;
}
else if (match.test(path))
return true;
}
return false;
}
/** Tests whether the path should be ignored */
function isIgnoredPath(path, 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
const { absolutePath, relativePath, basename } = path;
// custom callback
if (opts.ignoreCustomCallback && opts.ignoreCustomCallback(path) === true)
return true;
// absolute path checks
if (absolutePath) {
// match
if (opts.ignoreAbsolutePaths?.length &&
matches(absolutePath, opts.ignoreAbsolutePaths))
return true;
// custom?
if (opts.ignoreCustomPatterns?.test(absolutePath))
return true;
}
// relative path checks
if (relativePath) {
// match
if (opts.ignoreRelativePaths?.length &&
matches(relativePath, opts.ignoreRelativePaths))
return true;
// custom?
if (opts.ignoreCustomPatterns?.test(relativePath))
return true;
}
// basename checks
if (basename) {
// match
if (opts.ignoreBasenames?.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 (opts.ignoreCustomPatterns?.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 = [
...(opts.ignoreAbsolutePaths || []).map((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;
}),
...(opts.ignorePaths || []).filter((match) => typeof match === 'string' ? (0, path_1.isAbsolute)(match) : true),
];
opts.ignoreRelativePaths = [
...(opts.ignoreRelativePaths || []).map((match) => {
if (typeof match === 'string' && !isRelative(match))
throw new Error('ignorefs: ignoreRelativePaths should only contain relative paths and regular expressions');
return match;
}),
...(opts.ignorePaths || []).filter((match) => typeof match === 'string' ? isRelative(match) : true),
];
opts.ignoreBasenames = [
...(opts.ignoreBasenames || []).map((match) => {
if (typeof match === 'string' && !isBasename(match))
throw new Error('ignorefs: ignoreBasenames should only contain basebanes and regular expressions');
return match;
}),
...(opts.ignorePaths || []).filter((match) => typeof match === 'string' ? isBasename(path_1.sep) : true),
];
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 = {
ignoreUndesiredBasenames: true,
}) {
// adjust path
if (typeof path === 'string') {
if (!path)
throw new Error('ignorefs: path cannot be empty');
const 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;
;