n8n/.github/scripts/glob.mjs
Charlie Kolb ccb935478c
ci: Run owners and PR quality scripts without a Node.js setup (no-changelog) (#38349)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-14 11:40:39 +00:00

50 lines
1.3 KiB
JavaScript

/**
* Minimal glob matcher for changed-file paths.
*
* Supports `**` (any number of path segments), `*` (any run of characters
* inside one segment) and literal text. Dot segments match like any other,
* so patterns reach files under `.github/`. Only node builtins, so scripts
* that run through `run-workflow-script` can use it without an install.
*/
/** @type { Map<string, RegExp> } */
const cache = new Map();
/**
* @param { string } pattern
* @returns { RegExp }
*/
export function globToRegExp(pattern) {
const cached = cache.get(pattern);
if (cached) return cached;
let source = '';
const segments = pattern.split('/');
for (const [index, segment] of segments.entries()) {
const last = index === segments.length - 1;
if (segment === '**') {
// `**/` matches zero or more segments; a trailing `**` matches the rest.
source += last ? '.*' : '(?:[^/]+/)*';
continue;
}
source += segment
.split('*')
.map((literal) => literal.replace(/[.+?^${}()|[\]\\]/g, '\\$&'))
.join('[^/]*');
if (!last) source += '/';
}
const regExp = new RegExp(`^${source}$`);
cache.set(pattern, regExp);
return regExp;
}
/**
* @param { string } file Repository-relative path with `/` separators.
* @param { string } pattern
* @returns { boolean }
*/
export function matchesGlob(file, pattern) {
return globToRegExp(pattern).test(file);
}