Updates
This commit is contained in:
@@ -115,6 +115,12 @@ async function run(ctx, argv) {
|
||||
/** @type {'never' | 'always' | 'auto'} */
|
||||
let colorMode = 'never'
|
||||
let recursive = false
|
||||
/** @type {string[]} */
|
||||
const includeGlobs = []
|
||||
/** @type {string[]} */
|
||||
const excludeGlobs = []
|
||||
/** @type {string[]} */
|
||||
const excludeDirGlobs = []
|
||||
|
||||
const args = argv.slice(1)
|
||||
let i = 0
|
||||
@@ -182,6 +188,36 @@ async function run(ctx, argv) {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '--include' && args[i + 1]) {
|
||||
includeGlobs.push(args[++i])
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a.startsWith('--include=')) {
|
||||
includeGlobs.push(a.slice('--include='.length))
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '--exclude' && args[i + 1]) {
|
||||
excludeGlobs.push(args[++i])
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a.startsWith('--exclude=')) {
|
||||
excludeGlobs.push(a.slice('--exclude='.length))
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a === '--exclude-dir' && args[i + 1]) {
|
||||
excludeDirGlobs.push(args[++i])
|
||||
i++
|
||||
continue
|
||||
}
|
||||
if (a.startsWith('--exclude-dir=')) {
|
||||
excludeDirGlobs.push(a.slice('--exclude-dir='.length))
|
||||
i++
|
||||
continue
|
||||
}
|
||||
ctx.console.error('grep: unknown option ' + a)
|
||||
ctx.exitCode = 2
|
||||
return
|
||||
@@ -393,7 +429,15 @@ async function run(ctx, argv) {
|
||||
continue
|
||||
}
|
||||
if (st.type === 'directory') {
|
||||
await grepWalkFiles(ctx, p, acc, suppressErrors)
|
||||
await grepWalkFiles(ctx, p, acc, suppressErrors, {
|
||||
includeGlobs,
|
||||
excludeGlobs,
|
||||
excludeDirGlobs,
|
||||
filterCap: Number.parseInt(
|
||||
String(ctx.vfs?.env?.BARE_OS_GREP_FILTER_MAX || '32'),
|
||||
10
|
||||
) || 32
|
||||
})
|
||||
} else {
|
||||
acc.push(p)
|
||||
}
|
||||
@@ -589,8 +633,31 @@ const GREP_RECURSE_MAX_DEPTH = 64
|
||||
* @param {string[]} acc
|
||||
* @param {boolean} suppressErrors
|
||||
*/
|
||||
async function grepWalkFiles(ctx, dir, acc, suppressErrors) {
|
||||
/**
|
||||
* @param {string} name
|
||||
* @param {string} pat
|
||||
*/
|
||||
function grepSimpleGlobMatch(name, pat) {
|
||||
if (!pat || pat === '*') return true
|
||||
if (pat.includes('/')) return name === pat
|
||||
if (pat.startsWith('*') && pat.length > 1 && pat.endsWith('*')) {
|
||||
const mid = pat.slice(1, -1)
|
||||
return mid !== '' && name.includes(mid)
|
||||
}
|
||||
if (pat.startsWith('*')) return name.endsWith(pat.slice(1))
|
||||
if (pat.endsWith('*')) return name.startsWith(pat.slice(0, -1))
|
||||
return name === pat
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {{ includeGlobs?: string[], excludeGlobs?: string[], excludeDirGlobs?: string[], filterCap?: number }} [opts]
|
||||
*/
|
||||
async function grepWalkFiles(ctx, dir, acc, suppressErrors, opts = {}) {
|
||||
const vfs = ctx.vfs
|
||||
const cap = opts.filterCap && opts.filterCap > 0 ? opts.filterCap : 32
|
||||
const inc = (opts.includeGlobs || []).slice(0, cap)
|
||||
const exc = (opts.excludeGlobs || []).slice(0, cap)
|
||||
const excd = (opts.excludeDirGlobs || []).slice(0, cap)
|
||||
/** @param {string} d @param {number} depth */
|
||||
const walk = async (d, depth) => {
|
||||
if (depth > GREP_RECURSE_MAX_DEPTH) return
|
||||
@@ -612,12 +679,21 @@ async function grepWalkFiles(ctx, dir, acc, suppressErrors) {
|
||||
continue
|
||||
}
|
||||
if (!st) continue
|
||||
if (st.type === 'directory') await walk(sub, depth + 1)
|
||||
else if (st.type === 'file') acc.push(sub)
|
||||
else if (st.type === 'symlink') {
|
||||
if (st.type === 'directory') {
|
||||
if (excd.some((p) => grepSimpleGlobMatch(n, p))) continue
|
||||
await walk(sub, depth + 1)
|
||||
} else if (st.type === 'file') {
|
||||
if (inc.length && !inc.some((p) => grepSimpleGlobMatch(n, p))) continue
|
||||
if (exc.some((p) => grepSimpleGlobMatch(n, p))) continue
|
||||
acc.push(sub)
|
||||
} else if (st.type === 'symlink') {
|
||||
try {
|
||||
const ft = await vfs.stat(sub)
|
||||
if (ft && ft.type === 'file') acc.push(sub)
|
||||
if (ft && ft.type === 'file') {
|
||||
if (inc.length && !inc.some((p) => grepSimpleGlobMatch(n, p))) continue
|
||||
if (exc.some((p) => grepSimpleGlobMatch(n, p))) continue
|
||||
acc.push(sub)
|
||||
}
|
||||
} catch {
|
||||
/* skip */
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user