A Node.js module to parse raw gitignore data
WARNING: this tool is a work-in-progress and might be unstable or unsuitable for serious use.
This modules parses a raw gitignore content, as specified in the official Git documentation, and returns an array of ignored paths that Git will look against.
npm install git-ignore-parser
Given this .gitignore
file:
# Logs
logs/
*.log
npm-debug.log*
# Dependency directories
/node_modules/
jspm_packages
It would return the following:
const fs = require('fs');
const gitIgnoreParser = require('git-ignore-parser');
fs.readFile('.gitignore', 'utf-8', (error, data) {
if (!error) {
const ignoredPaths = gitIgnoreParser(data);
console.log(ignoredPaths);
// >> ['logs', '*.log', 'npm-debug.log*', 'node_modules', 'jspm_packages']
}
});
The parser will apply the following rules, as specified in the official Git documentation:
- Empty lines are ignored
- Lines starting with a
#
are treated as comments and are ignored - Trailing spaces are ignored
- Trailing slashes are ignored
- Duplicates are ignored
- Globs and negative patterns are treated as is