// A small glob matcher, enough for `build.files` and the dev watcher: `**` // crosses directories, `*` and `?` stay within one path segment, `{a,b}` lists // alternatives, and a leading `!` excludes. No dependency, and the same // semantics in the shell and in the CLI. import { readdir, stat } from 'node:fs/promises'; import { join, relative, sep } from 'node:path'; const SPECIAL = /[.+^$()|[\]\\]/g; export const toPosix = (p) => p.split(sep).join('/'); export function globToRegExp (pattern) { const p = pattern.replace(/\\/g, '/').replace(/^\.\//, ''); let re = ''; for (let i = 0; i < p.length; i++) { const c = p[i]; if (c === '*') { if (p[i + 1] === '*') { // `**/` is zero or more directories; a trailing `**` is the rest of the path. if (p[i + 2] === '/') { re += '(?:.*/)?'; i += 2; } else { re += '.*'; i += 1; } } else re += '[^/]*'; } else if (c === '?') re += '[^/]'; else if (c === '{') { const end = p.indexOf('}', i); if (end === -1) { re += '\\{'; continue; } re += `(?:${p.slice(i + 1, end).split(',').map((s) => s.replace(SPECIAL, '\\$&')).join('|')})`; i = end; } else re += c.replace(SPECIAL, '\\$&'); } return new RegExp(`^${re}$`); } // A matcher for a list of patterns: a path is in when some pattern matches it // and no `!` pattern does. export function compile (patterns) { const positive = []; const negative = []; for (const p of patterns) (p.startsWith('!') ? negative : positive).push(globToRegExp(p.replace(/^!/, ''))); return (relPath) => { const path = toPosix(relPath); return positive.some((re) => re.test(path)) && !negative.some((re) => re.test(path)); }; } export const matches = (pattern, relPath) => compile([pattern])(relPath); export const anyMatch = (patterns, relPath) => compile(patterns)(relPath); // Files under `root` matching `patterns`, as relative POSIX paths. A pattern // that names a directory outright (`server`) means everything under it. `.git` // and `dist` at the root, and `node_modules` anywhere, are not entered unless // a pattern names them. export async function expand (root, patterns) { const expanded = []; for (const p of patterns) { const literal = !p.startsWith('!') && !/[*?{]/.test(p); if (literal && (await stat(join(root, p)).catch(() => null))?.isDirectory()) expanded.push(`${p.replace(/\/+$/, '')}/**`); else expanded.push(p); } const match = compile(expanded); const named = (name) => expanded.some((p) => !p.startsWith('!') && p.replace(/^\.\//, '').split('/').includes(name)); const skipAtRoot = new Set(['.git', 'dist'].filter((n) => !named(n))); const skipModules = !named('node_modules'); const out = []; async function walk (dir, depth) { for (const entry of await readdir(dir, { withFileTypes: true })) { const abs = join(dir, entry.name); if (entry.isDirectory()) { if (depth === 0 && skipAtRoot.has(entry.name)) continue; if (skipModules && entry.name === 'node_modules') continue; await walk(abs, depth + 1); } else { const rel = toPosix(relative(root, abs)); if (match(rel)) out.push(rel); } } } await walk(root, 0); return out.sort(); }