Last active
October 21, 2021 15:43
-
-
Save jimisaacs/47f7c9ff823b054f979fa16d34348a03 to your computer and use it in GitHub Desktop.
node-ignore stdin
This file contains 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 node | |
import ignore from 'ignore'; | |
/** | |
* Filter stdin with script arguments as filter patterns | |
* | |
* Usage: | |
* ```sh | |
* git ls-files | node ingore.js '/ignore/me/*' '!/ignore/me/not' | |
* find . -type f | cut -d/ -f2- | node ingore.js '/ignore/me/*' '!/ignore/me/not' | |
* ``` | |
*/ | |
const filter = ignore().add(process.argv.slice(2).filter(Boolean)).createFilter(); | |
function printLn(line) { | |
process.stdout.write(line + '\n'); | |
} | |
let buffer = ''; | |
function flush() { | |
buffer.split('\n').filter(Boolean).filter(filter).forEach(printLn); | |
buffer = ''; | |
} | |
process | |
.openStdin() | |
.on('data', function (chunk) { | |
buffer += chunk; | |
if (buffer.endsWith('\n')) flush(); | |
}) | |
.on('end', flush); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment