Skip to content

Instantly share code, notes, and snippets.

@jimisaacs
Last active October 21, 2021 15:43
Show Gist options
  • Save jimisaacs/47f7c9ff823b054f979fa16d34348a03 to your computer and use it in GitHub Desktop.
Save jimisaacs/47f7c9ff823b054f979fa16d34348a03 to your computer and use it in GitHub Desktop.
node-ignore stdin
#!/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