putout
Version:
🐊 Pluggable and configurable code transformer with built-in ESLint, Babel and support of js, jsx, typescript, flow, markdown, yaml and json
29 lines (20 loc) • 803 B
JavaScript
import {relative} from 'node:path';
import ignore from 'ignore';
const isNegative = (a) => !a.indexOf('!');
const positive = (a) => a.replace(/^!/, '');
export const ignores = (dirOpt, resolvedName, options = {}) => {
const relativeName = relative(dirOpt, resolvedName);
const ignorer = ignore();
const ignoreList = mergeIgnores(options.ignore || []);
ignorer.add(ignoreList);
return dirOpt && ignorer.ignores(relativeName);
};
function mergeIgnores(ignores) {
for (const [i, str] of ignores.entries()) {
const positiveIndex = ignores.indexOf(positive(str));
if (isNegative(str) && positiveIndex > i)
ignores[positiveIndex] = str;
}
const noDuplicates = new Set(ignores);
return Array.from(noDuplicates);
}