Comparing version 1.1.3 to 2.2.1
84
index.js
@@ -6,4 +6,5 @@ 'use strict'; | ||
var EE = require('events').EventEmitter; | ||
var EE = require('events').EventEmitter; | ||
var node_util = require('util'); | ||
var node_fs = require('fs'); | ||
@@ -14,3 +15,17 @@ function ignore (options){ | ||
// Select the first existing file of the file list | ||
ignore.select = function (files) { | ||
var exists; | ||
files.some(function (file) { | ||
if( node_fs.existsSync(file) ){ | ||
exists = file; | ||
return true; | ||
} | ||
}); | ||
return exists; | ||
}; | ||
// @param {Object} options | ||
@@ -20,7 +35,6 @@ // - ignore: {Array} | ||
// If false, ignore patterns with two globstars will be omitted | ||
// - noCase: {boolean=true} case sensitive. | ||
// - matchCase: {boolean=true} case sensitive. | ||
// By default, git is case-insensitive | ||
function Ignore (options){ | ||
options = options || {}; | ||
options.noCase = 'noCase' in options ? options.noCase : true; | ||
@@ -30,4 +44,12 @@ this.options = options; | ||
this._rules = []; | ||
this._ignoreFiles = []; | ||
this.add(options.ignore); | ||
options.ignore = options.ignore || [ | ||
// Some files or directories which we should ignore for most cases. | ||
'.git', | ||
'.svn', | ||
'.DS_Store' | ||
]; | ||
this.addPattern(options.ignore); | ||
} | ||
@@ -51,4 +73,4 @@ | ||
// @param {Array.<string>|string} pattern | ||
Ignore.prototype.add = function(pattern) { | ||
makeArray(pattern).forEach(this._add, this); | ||
Ignore.prototype.addPattern = function(pattern) { | ||
makeArray(pattern).forEach(this._addPattern, this); | ||
return this; | ||
@@ -58,8 +80,3 @@ }; | ||
Ignore.prototype.filter = function(paths) { | ||
return paths.filter(this._filter, this); | ||
}; | ||
Ignore.prototype._add = function(pattern) { | ||
Ignore.prototype._addPattern = function(pattern) { | ||
if(this._simpleTest(pattern)){ | ||
@@ -71,2 +88,8 @@ var rule = this._createRule(pattern); | ||
Ignore.prototype.filter = function(paths) { | ||
return paths.filter(this._filter, this); | ||
}; | ||
Ignore.prototype._simpleTest = function(pattern) { | ||
@@ -114,3 +137,2 @@ var pass = | ||
} | ||
@@ -210,3 +232,3 @@ pattern = pattern | ||
return new RegExp(source, this.options.noCase ? 'i' : ''); | ||
return new RegExp(source, this.options.matchCase ? '' : 'i'); | ||
}; | ||
@@ -243,3 +265,3 @@ | ||
return function (path) { | ||
return self._filter(path); | ||
return self._filter(path); | ||
}; | ||
@@ -249,1 +271,33 @@ }; | ||
// @param {Array.<path>|path} a | ||
Ignore.prototype.addIgnoreFile = function(files) { | ||
makeArray(files).forEach(this._addIgnoreFile, this); | ||
return this; | ||
}; | ||
Ignore.prototype._addIgnoreFile = function (file) { | ||
if(this._checkRuleFile(file)){ | ||
this._ignoreFiles.push(file); | ||
var content; | ||
try { | ||
content = node_fs.readFileSync(file); | ||
} catch(e) { | ||
} | ||
if(content){ | ||
this.addPattern( content.toString().split(/\r?\n/) ); | ||
} | ||
} | ||
}; | ||
Ignore.prototype._checkRuleFile = function(file) { | ||
return file !== '.' && | ||
file !== '..' && | ||
! ~ this._ignoreFiles.indexOf(file); | ||
}; | ||
{ | ||
"name": "ignore", | ||
"version": "1.1.3", | ||
"version": "2.2.1", | ||
"description": "Ignore is a manager and filter for .gitignore rules.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,3 +5,3 @@ [![Build Status](https://travis-ci.org/kaelzhang/node-ignore.png?branch=master)](https://travis-ci.org/kaelzhang/node-ignore) | ||
`ignore` is a manager and filter for .gitignore rules. | ||
`ignore` is a manager and filter according to the .gitignore [spec](http://git-scm.com/docs/gitignore). | ||
@@ -16,9 +16,8 @@ # Installation | ||
var ignore = require('ignore'); | ||
var ig = ignore({ | ||
ignore: [ | ||
'.abc/*' // or use the `.add()` method | ||
] | ||
var ig = ignore(options).addPattern(['.abc/*', '!.abc/d/']); | ||
``` | ||
}).add('!.abc/d/'); | ||
## Filter the given paths | ||
```js | ||
var paths = [ | ||
@@ -30,8 +29,27 @@ '.abc/a.js', // filtered out | ||
ig.filter(paths); // ['.abc/d/e.js'] | ||
``` | ||
## As the filter function | ||
```js | ||
paths.filter(ig.createFilter()); // ['.abc/d/e.js'] | ||
``` | ||
## With ignore files | ||
For most cases, we'd better use only one ignore file. We could use `ignore.select` to select the first existing file. | ||
```js | ||
ignore().addIgnoreFile( | ||
ignore.select([ | ||
'.xxxignore', | ||
'.gitignore', | ||
'.ignore' | ||
]) | ||
); | ||
``` | ||
# Why another ignore? | ||
1. `ignore` is a standalone module, and is much simpler so that it could easily work with other programs, unlike [isaacs](https://npmjs.org/~isaacs)'s [fstream-ignore](https://npmjs.org/package/fstream-ignore) which must work with the modules of the fstream family. | ||
1. `ignore` is a standalone module, and is much simpler so that it could easy work with other programs, unlike [isaacs](https://npmjs.org/~isaacs)'s [fstream-ignore](https://npmjs.org/package/fstream-ignore) which must work with the modules of the fstream family. | ||
@@ -48,3 +66,3 @@ 2. `ignore` only contains utility methods to filter paths according to the specified ignore rules. | ||
## .add(rule) | ||
## .addPattern(pattern) | ||
@@ -55,6 +73,23 @@ Adds a rule or several rules to the current manager. | ||
#### Rule `String|Array.<String>` | ||
#### pattern `String|Array.<String>` | ||
The ignore rule or a array of rules. | ||
Notice that a line starting with `'#'`(hash) is treated as a comment. Put a backslash (`'\'`) in front of the first hash for patterns that begin with a hash, if you want to ignore a file with a hash at the beginning of the filename. | ||
```js | ||
ignore().addPattern('#abc').filter(['#abc']); // ['abc'] | ||
ignore().addPattern('\#abc').filter(['#abc']); // [] | ||
``` | ||
## .addIgnoreFile(path) | ||
Adds rules from a ignore file or several files | ||
#### Returns `this` | ||
#### Rule `String|Array.<String>` | ||
## .filter(paths) | ||
@@ -70,3 +105,3 @@ | ||
Creates a filter function which could be used with `Array.prototype.filter`. | ||
Creates a filter function which could filter an array of paths with `Array.prototype.filter`. | ||
@@ -85,3 +120,3 @@ #### Returns `function(path)` | ||
#### options.noCase `boolean=true` | ||
#### options.matchCase `boolean=false` | ||
@@ -98,5 +133,13 @@ By default, all ignore rules will be treated as case-insensitive ones as well as the git does. | ||
The ignore rules to be added. | ||
The ignore rules to be added. Default to `['.git', '.svn', '.DS_Store']` | ||
You can also use `.add()` method to do this. | ||
If you want those directories to be included, you could | ||
```js | ||
ignore({ | ||
ignore: [] | ||
}); | ||
``` | ||
You can also use `.addPattern()` method to do this. | ||
@@ -182,2 +182,17 @@ 'use strict'; | ||
describe(".addPattern(), leading #", function(){ | ||
it("will treat leading # as comments", function(){ | ||
var result = ignore().addPattern('#abc').filter(['#abc']); | ||
expect( result ).to.deep.equal(['#abc']); | ||
}); | ||
it("\\#", function(){ | ||
var result = ignore().addPattern('\\#abc').filter(['#abc']); | ||
expect( result ).to.deep.equal([]); | ||
}); | ||
}); | ||
describe(".filter()", function(){ | ||
@@ -220,8 +235,6 @@ it("could filter paths", function(){ | ||
it("test context", function(){ | ||
var ig = ignore({ | ||
ignore: [ | ||
var ig = ignore().addPattern([ | ||
'abc', | ||
'!abc/b' | ||
] | ||
}); | ||
]); | ||
@@ -238,18 +251,6 @@ var filtered = [ | ||
describe("_private properties", function(){ | ||
describe("constructor", function(){ | ||
it("will not mess up, if `options.ignore` not specified", function(){ | ||
var ig = ignore(); | ||
describe(".addPattern()", function(){ | ||
it(".addPattern(rule), chained", function(){ | ||
var ig = ignore().addPattern('abc').addPattern('!abc/b'); | ||
expect(ig._patterns.length).to.equal(0); | ||
expect(ig._rules.length).to.equal(0); | ||
}); | ||
}); | ||
}); | ||
describe(".add()", function(){ | ||
it(".add(rule), chained", function(){ | ||
var ig = ignore().add('abc').add('!abc/b'); | ||
var filtered = [ | ||
@@ -263,4 +264,4 @@ 'abc/a.js', | ||
it(".add(rule), chained", function(){ | ||
var ig = ignore().add(['abc', '!abc/b']); | ||
it(".addPattern(rule), chained", function(){ | ||
var ig = ignore().addPattern(['abc', '!abc/b']); | ||
@@ -277,2 +278,32 @@ var filtered = [ | ||
describe("ignore.select()", function(){ | ||
it("returns the first existing file", function(){ | ||
var file = ignore.select([ | ||
'test/fixtures/.ignore', | ||
'test/fixtures/.aignore' | ||
]); | ||
expect(file).to.equal('test/fixtures/.aignore'); | ||
}); | ||
}); | ||
describe(".addIgnoreFile(), complex testing", function(){ | ||
it("will add patterns from the file", function(){ | ||
var result = ignore().addIgnoreFile( | ||
ignore.select([ | ||
'test/fixtures/.aignore', | ||
'test/fixtures/.bignore' | ||
]) | ||
).filter([ | ||
'abc/a.js', | ||
'abc/b/b.js', | ||
'#e', | ||
'#f' | ||
]); | ||
expect(result).to.deep.equal(['abc/b/b.js', '#e']); | ||
}); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
21465
10
450
138
0
1