Last active
January 11, 2021 10:00
-
-
Save ankjevel/242dcb40812a997dde1ff8ffe63931c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
":" //# comment; exec /usr/bin/env node --input-type=module - $@ < "$0" | |
import { spawn } from 'child_process'; | |
import { join } from 'path'; | |
import { homedir } from 'os'; | |
const extra_args = []; | |
const default_exclude = [ | |
`--exclude='*test*'`, | |
`--exclude='*.d.ts'`, | |
`--exclude-dir=.yarn`, | |
`--exclude-dir=.git`, | |
`--exclude-dir=node_modules`, | |
`--exclude-dir=intl`, | |
`--exclude-dir=translations`, | |
`--exclude-dir=packages`, | |
`--exclude-dir=fixtures`, | |
`--exclude='*.min.*'`, | |
`--exclude='*.spec.*'`, | |
`--exclude='*-lock.*'`, | |
`--exclude='package.json'`, | |
]; | |
const search_for = process.argv | |
.slice(2) | |
.reduce((list, arg) => { | |
if (arg.startsWith('--')) { | |
if (arg.search(/ignore.{0,1}default/i) !== -1) { | |
default_exclude.splice(0); | |
} else { | |
extra_args.push(arg); | |
} | |
} else { | |
list.push(arg); | |
} | |
return list; | |
}, []) | |
.join(' ') | |
.trim(); | |
const search_dir = '/dev/Springworks'; | |
const dir = join(homedir(), search_dir); | |
const command = async () => { | |
const result_split = /:[ \t\s]{0,}/; | |
let last_dir = null; | |
let last_file = null; | |
const printLine = (line) => { | |
const [, data] = line.split(`${dir}/`); | |
if (!data) { | |
return; | |
} | |
const [path] = data.split(result_split); | |
const result = data.substring(((data.match(result_split) || {}).index || -1) + 1); | |
const [repo, ...file_parts] = path.split('/'); | |
const file = file_parts.join('/'); | |
if (last_dir != repo) { | |
console.log(`\n\x1b[4m\x1b[2m${repo}\x1b[0m`); | |
last_dir = repo; | |
last_file = null; | |
} | |
if (last_file !== file) { | |
console.log(`\x1b[90m${file}`); | |
last_file = file; | |
} | |
console.log(`\x1b[01;38;5;28m ${highlight(result.trim())}\x1b[0m`); | |
}; | |
const onData = (stdout) => { | |
for (const line of stdout.toString().split('\n')) { | |
printLine(line); | |
} | |
}; | |
await new Promise((resolve) => { | |
const subprocess = spawn( | |
'egrep', | |
[ | |
`-ir`, | |
`--include='*.dart'`, | |
`--include='*.erb'`, | |
`--include='*.go'`, | |
`--include='*.js'`, | |
`--include='*.json'`, | |
`--include='*.kt'`, | |
`--include='*.kts'`, | |
`--include='*.md'`, | |
`--include='*.sh'`, | |
`--include='*.swift'`, | |
`--include='*.ts'`, | |
`--include='*.tsx'`, | |
...default_exclude, | |
...extra_args, | |
`'${search_for}'`, | |
dir, | |
], | |
{ shell: true }, | |
); | |
subprocess.stdout.on('data', onData); | |
subprocess.on('close', resolve); | |
}); | |
console.log(`\x1b[0m`); | |
}; | |
function highlight(line) { | |
const regex = new RegExp(search_for, 'gi'); | |
const indices = []; | |
let re = null; | |
do { | |
re = regex.exec(line); | |
if (!re) { | |
break; | |
} | |
if (re) { | |
const [match] = re; | |
const { index } = re; | |
const end = index + match.length; | |
indices.push([match, index, end]); | |
} | |
} while (re); | |
let string = line; | |
indices.reverse().forEach(([match, index, end]) => { | |
string = `${string.substring(0, index)}\x1b[01;38;5;46m${match}\x1b[01;38;5;28m${string.substring(end)}`; | |
}); | |
return string; | |
} | |
command(); |
Author
ankjevel
commented
Jan 7, 2021
•
The search is done using regex, so, escape the characters as needed.
To remove the default exclude-pattern, run command with --ignore-default
, any other argument needed to be passed should be done so using a double-dash, then equal; as so: --include-dir='packages'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment