parse-gitignore
Advanced tools
Comparing version 0.1.2 to 0.2.0
115
index.js
@@ -10,16 +10,111 @@ /*! | ||
var unique = require('array-unique'); | ||
var filter = require('arr-filter'); | ||
var fs = require('fs'); | ||
var isGlob = require('is-glob'); | ||
var cache = {}; | ||
module.exports = function gitignore(str, patterns) { | ||
if (!str) return null; | ||
var lines = str.split('\n'); | ||
function gitignore(fp, patterns, options) { | ||
if (!fp || !fs.existsSync(fp)) return []; | ||
var res = filter(lines, function (line) { | ||
line = line.trim(); | ||
return line.charAt(0) !== '#' && line !== ''; | ||
}); | ||
if (cache.hasOwnProperty(fp)) { | ||
return cache[fp]; | ||
} | ||
return unique(res.concat(patterns || [])); | ||
if (typeof pattern !== 'string' && !Array.isArray(patterns)) { | ||
options = patterns; | ||
patterns = []; | ||
} | ||
var str = fs.readFileSync(fp, 'utf8'); | ||
var lines = str.split(/\r\n?|\n/).concat(patterns || []); | ||
var res = gitignore.parse(lines, options || {}); | ||
return (cache[fp] = res); | ||
} | ||
gitignore.parse = function parse(arr, opts) { | ||
arr = arrayify(arr); | ||
var len = arr.length, i = -1; | ||
var res = []; | ||
while (++i < len) { | ||
var str = arr[i]; | ||
str = (str || '').trim(); | ||
if (!str || str.charAt(0) === '#') { | ||
continue; | ||
} | ||
var parsed = gitignore.toGlob(str); | ||
addPattern(res, parsed.patterns, parsed.stats, opts); | ||
} | ||
return res; | ||
}; | ||
gitignore.toGlob = function toGlob(str) { | ||
var parsed = {}, stats = {}; | ||
stats.first = str.charAt(0); | ||
stats.last = str.slice(-1); | ||
stats.isNegated = stats.first === '!'; | ||
stats.isGlob = isGlob(str); | ||
if (stats.isNegated) { | ||
str = str.slice(1); | ||
stats.first = str.charAt(0); | ||
} | ||
if (stats.first === '/') { | ||
str = str.slice(1); | ||
} | ||
if (/\w\/[*]{2}\/\w/.test(str)) { | ||
str += '|' + str.split('/**/').join('/'); | ||
} | ||
if (/^[\w.]/.test(str) && /\w$/.test(str) && !stats.isGlob) { | ||
str += '|' + str + '/**'; | ||
} else if (/\/$/.test(str)) { | ||
str += '**'; | ||
} | ||
parsed.stats = stats; | ||
parsed.patterns = str.split('|'); | ||
return parsed; | ||
}; | ||
function addPattern(res, arr, stats, options) { | ||
arr = arrayify(arr); | ||
var len = arr.length, i = -1; | ||
while (++i < len) { | ||
var str = arr[i]; | ||
if (stats.isNegated) { | ||
str = '!' + str; | ||
} | ||
if (options.invert) { | ||
str = invert(str); | ||
} | ||
if (res.indexOf(str) === -1) { | ||
res.push(str); | ||
} | ||
} | ||
return res; | ||
} | ||
function invert(str) { | ||
if (str.charAt(0) === '!') { | ||
return str.slice(1); | ||
} | ||
return '!' + str; | ||
} | ||
function arrayify(val) { | ||
return Array.isArray(val) ? val : [val]; | ||
} | ||
/** | ||
* Expose `gitignore` | ||
*/ | ||
module.exports = gitignore; |
{ | ||
"name": "parse-gitignore", | ||
"description": "Parse a gitignore file into an array of patterns. Comments and empty lines are stripped.", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/parse-gitignore", | ||
"author": { | ||
"name": "Jon Schlinkert", | ||
"url": "https://github.com/jonschlinkert" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/jonschlinkert/parse-gitignore.git" | ||
}, | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"repository": "jonschlinkert/parse-gitignore", | ||
"bugs": { | ||
"url": "https://github.com/jonschlinkert/parse-gitignore/issues" | ||
}, | ||
"license": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/parse-gitignore/blob/master/LICENSE" | ||
}, | ||
"license": "MIT", | ||
"files": [ | ||
@@ -31,8 +22,3 @@ "index.js" | ||
}, | ||
"dependencies": { | ||
"arr-filter": "^1.1.0", | ||
"array-unique": "^0.1.1" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "^5.0.1" | ||
@@ -44,3 +30,8 @@ }, | ||
"parse" | ||
] | ||
], | ||
"dependencies": { | ||
"ends-with": "^0.2.0", | ||
"is-glob": "^2.0.0", | ||
"starts-with": "^1.0.0" | ||
} | ||
} |
@@ -1,9 +0,11 @@ | ||
# parse-gitignore [![NPM version](https://badge.fury.io/js/parse-gitignore.svg)](http://badge.fury.io/js/parse-gitignore) [![Build Status](https://travis-ci.org/jonschlinkert/parse-gitignore.svg)](https://travis-ci.org/jonschlinkert/parse-gitignore) | ||
# parse-gitignore [![NPM version](https://badge.fury.io/js/parse-gitignore.svg)](http://badge.fury.io/js/parse-gitignore) [![Build Status](https://travis-ci.org/jonschlinkert/parse-gitignore.svg)](https://travis-ci.org/jonschlinkert/parse-gitignore) | ||
> Parse a gitignore file into an array of patterns. Comments and empty lines are stripped. | ||
## Install with [npm](npmjs.org) | ||
## Install | ||
```bash | ||
npm i parse-gitignore --save | ||
Install with [npm](https://www.npmjs.com/) | ||
```sh | ||
$ npm i parse-gitignore --save | ||
``` | ||
@@ -16,6 +18,5 @@ | ||
// pass a string | ||
var str = fs.readFileSync('.gitignore', 'utf8'); | ||
gitignore(str); | ||
//=> ['*.DS_Store', 'node_modules', ...] | ||
// pass a filepath | ||
var patterns = gitignore('.gitignore'); | ||
//=> ['*.DS_Store', 'node_modules', ...]; | ||
``` | ||
@@ -25,33 +26,41 @@ | ||
Since the function already does unique-ifying, you can optionally pass an array of patterns to combine additional patterns to whatever is in `.gitignore` and get an array of unique patterns: | ||
Since the function already does unique-ifying on patterns, you can optionally pass an additional array of patterns to add to the patterns parsed from `.gitignore`: | ||
```js | ||
gitignore(str, ['foo', 'bar']); | ||
gitignore('.gitignore', ['foo', 'bar']); | ||
//=> ['*.DS_Store', 'node_modules', 'foo', 'bar', ...] | ||
``` | ||
## Run tests | ||
## Related projects | ||
* [glob-fs](https://github.com/jonschlinkert/glob-fs): file globbing for node.js. speedy and powerful alternative to node-glob. | ||
* [is-glob](https://github.com/jonschlinkert/is-glob): Returns `true` if the given string looks like a glob pattern. | ||
* [micromatch](https://github.com/jonschlinkert/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. Just… [more](https://github.com/jonschlinkert/micromatch) | ||
## Running tests | ||
Install dev dependencies: | ||
```bash | ||
npm i -d && npm test | ||
```sh | ||
$ npm i -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/parse-gitignore/issues) | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/parse-gitignore/issues/new) | ||
## Author | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright (c) 2015 Jon Schlinkert | ||
Released under the MIT license | ||
Copyright © 2015 Jon Schlinkert | ||
Released under the MIT license. | ||
*** | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on February 27, 2015._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 09, 2015._ |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
6206
1
94
64
3
2
+ Addedends-with@^0.2.0
+ Addedis-glob@^2.0.0
+ Addedstarts-with@^1.0.0
+ Addedends-with@0.2.0(transitive)
+ Addedis-extglob@1.0.0(transitive)
+ Addedis-glob@2.0.1(transitive)
+ Addedstarts-with@1.0.2(transitive)
- Removedarr-filter@^1.1.0
- Removedarray-unique@^0.1.1
- Removedarr-filter@1.1.2(transitive)
- Removedarray-unique@0.1.1(transitive)
- Removedkind-of@6.0.3(transitive)
- Removedmake-iterator@1.0.1(transitive)