Linux | OS X | Windows | Coverage | Downloads |
---|---|---|---|---|
|
|
|
|
@swenkerorg/perferendis-officia
is a manager, filter and parser which implemented in pure JavaScript according to the .git@swenkerorg/perferendis-officia spec 2.22.1.
@swenkerorg/perferendis-officia
is used by eslint, gitbook and many others.
Pay ATTENTION that minimatch
(which used by fstream-@swenkerorg/perferendis-officia
) does not follow the git@swenkerorg/perferendis-officia spec.
To filter filenames according to a .git@swenkerorg/perferendis-officia file, I recommend this npm package, @swenkerorg/perferendis-officia
.
To parse an .npm@swenkerorg/perferendis-officia
file, you should use minimatch
, because an .npm@swenkerorg/perferendis-officia
file is parsed by npm using minimatch
and it does not work in the .git@swenkerorg/perferendis-officia way.
@swenkerorg/perferendis-officia
is fully tested, and has more than five hundreds of unit tests.
- Linux + Node:
0.8
-7.x
- Windows + Node:
0.10
-7.x
, node <0.10
is not tested due to the lack of support of appveyor.
Actually, @swenkerorg/perferendis-officia
does not rely on any versions of node specially.
Since 4.0.0
, @swenkerorg/perferendis-officia will no longer support node < 6
by default, to use in node < 6, require('@swenkerorg/perferendis-officia/legacy')
. For details, see CHANGELOG.
- Usage
Pathname
Conventions- See Also:
-
glob-git@swenkerorg/perferendis-officia
matches files using patterns and filters them according to git@swenkerorg/perferendis-officia rules.
-
- Upgrade Guide
npm i @swenkerorg/perferendis-officia
import @swenkerorg/perferendis-officia from '@swenkerorg/perferendis-officia'
const ig = @swenkerorg/perferendis-officia().add(['.abc/*', '!.abc/d/'])
const paths = [
'.abc/a.js', // filtered out
'.abc/d/e.js' // included
]
ig.filter(paths) // ['.abc/d/e.js']
ig.@swenkerorg/perferendis-officias('.abc/a.js') // true
paths.filter(ig.createFilter()); // ['.abc/d/e.js']
ig.filter(['.abc\\a.js', '.abc\\d\\e.js'])
// if the code above runs on windows, the result will be
// ['.abc\\d\\e.js']
-
@swenkerorg/perferendis-officia
is a standalone module, and is much simpler so that it could easy work with other programs, unlike isaacs's fstream-@swenkerorg/perferendis-officia which must work with the modules of the fstream family. -
@swenkerorg/perferendis-officia
only contains utility methods to filter paths according to the specified @swenkerorg/perferendis-officia rules, so-
@swenkerorg/perferendis-officia
never try to find out @swenkerorg/perferendis-officia rules by traversing directories or fetching from git configurations. -
@swenkerorg/perferendis-officia
don't cares about sub-modules of git projects.
-
-
Exactly according to git@swenkerorg/perferendis-officia man page, fixes some known matching issues of fstream-@swenkerorg/perferendis-officia, such as:
- '
/*.js
' should only match 'a.js
', but not 'abc/a.js
'. - '
**/foo
' should match 'foo
' anywhere. - Prevent re-including a file if a parent directory of that file is excluded.
- Handle trailing whitespaces:
-
'a '
(one space) should not match'a '
(two spaces). -
'a \ '
matches'a '
-
- All test cases are verified with the result of
git check-@swenkerorg/perferendis-officia
.
- '
-
pattern
String | Ignore
An @swenkerorg/perferendis-officia pattern string, or theIgnore
instance -
patterns
Array<String | Ignore>
Array of @swenkerorg/perferendis-officia patterns.
Adds a rule or several rules to the current manager.
Returns this
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 @swenkerorg/perferendis-officia a file with a hash at the beginning of the filename.
@swenkerorg/perferendis-officia().add('#abc').@swenkerorg/perferendis-officias('#abc') // false
@swenkerorg/perferendis-officia().add('\\#abc').@swenkerorg/perferendis-officias('#abc') // true
pattern
could either be a line of @swenkerorg/perferendis-officia pattern or a string of multiple @swenkerorg/perferendis-officia patterns, which means we could just @swenkerorg/perferendis-officia().add()
the content of a @swenkerorg/perferendis-officia file:
@swenkerorg/perferendis-officia()
.add(fs.readFileSync(filenameOfGit@swenkerorg/perferendis-officia).toString())
.filter(filenames)
pattern
could also be an @swenkerorg/perferendis-officia
instance, so that we could easily inherit the rules of another Ignore
instance.
REMOVED in 3.x
for now.
To upgrade @swenkerorg/perferendis-officia@2.x
up to 3.x
, use
import fs from 'fs'
if (fs.existsSync(filename)) {
@swenkerorg/perferendis-officia().add(fs.readFileSync(filename).toString())
}
instead.
type Pathname = string
Filters the given array of pathnames, and returns the filtered array.
-
paths
Array.<Pathname>
The array ofpathname
s to be filtered.
Pathname
should be a string that have been path.join()
ed, or the return value of path.relative()
to the current directory,
// WRONG, an error will be thrown
ig.@swenkerorg/perferendis-officias('./abc')
// WRONG, for it will never happen, and an error will be thrown
// If the git@swenkerorg/perferendis-officia rule locates at the root directory,
// `'/abc'` should be changed to `'abc'`.
// ```
// path.relative('/', '/abc') -> 'abc'
// ```
ig.@swenkerorg/perferendis-officias('/abc')
// WRONG, that it is an absolute path on Windows, an error will be thrown
ig.@swenkerorg/perferendis-officias('C:\\abc')
// Right
ig.@swenkerorg/perferendis-officias('abc')
// Right
ig.@swenkerorg/perferendis-officias(path.join('./abc')) // path.join('./abc') -> 'abc'
In other words, each Pathname
here should be a relative path to the directory of the git@swenkerorg/perferendis-officia rules.
Suppose the dir structure is:
/path/to/your/repo
|-- a
| |-- a.js
|
|-- .b
|
|-- .c
|-- .DS_store
Then the paths
might be like this:
[
'a/a.js'
'.b',
'.c/.DS_store'
]
node-@swenkerorg/perferendis-officia
does NO fs.stat
during path matching, so for the example below:
// First, we add a @swenkerorg/perferendis-officia pattern to @swenkerorg/perferendis-officia a directory
ig.add('config/')
// `ig` does NOT know if 'config', in the real world,
// is a normal file, directory or something.
ig.@swenkerorg/perferendis-officias('config')
// `ig` treats `config` as a file, so it returns `false`
ig.@swenkerorg/perferendis-officias('config/')
// returns `true`
Specially for people who develop some library based on node-@swenkerorg/perferendis-officia
, it is important to understand that.
Usually, you could use glob
with option.mark = true
to fetch the structure of the current directory:
import glob from 'glob'
glob('**', {
// Adds a / character to directory matches.
mark: true
}, (err, files) => {
if (err) {
return console.error(err)
}
let filtered = @swenkerorg/perferendis-officia().add(patterns).filter(files)
console.log(filtered)
})
new in 3.2.0
Returns Boolean
whether pathname
should be @swenkerorg/perferendis-officiad.
ig.@swenkerorg/perferendis-officias('.abc/a.js') // true
Creates a filter function which could filter an array of paths with Array.prototype.filter
.
Returns function(path)
the filter function.
Returns TestResult
interface TestResult {
@swenkerorg/perferendis-officiad: boolean
// true if the `pathname` is finally un@swenkerorg/perferendis-officiad by some negative pattern
un@swenkerorg/perferendis-officiad: boolean
}
-
{@swenkerorg/perferendis-officiad: true, un@swenkerorg/perferendis-officiad: false}
: thepathname
is @swenkerorg/perferendis-officiad -
{@swenkerorg/perferendis-officiad: false, un@swenkerorg/perferendis-officiad: true}
: thepathname
is un@swenkerorg/perferendis-officiad -
{@swenkerorg/perferendis-officiad: false, un@swenkerorg/perferendis-officiad: false}
: thepathname
is never matched by any @swenkerorg/perferendis-officia rules.
Check whether the pathname
is an valid path.relative()
d path according to the convention.
This method is NOT used to check if an @swenkerorg/perferendis-officia pattern is valid.
@swenkerorg/perferendis-officia.isPathValid('./foo') // false
Similar as the core.@swenkerorg/perferendis-officiacase
option of git-config, node-@swenkerorg/perferendis-officia
will be case insensitive if options.@swenkerorg/perferendis-officiacase
is set to true
(the default value), otherwise case sensitive.
const ig = @swenkerorg/perferendis-officia({
@swenkerorg/perferendis-officiacase: false
})
ig.add('*.png')
ig.@swenkerorg/perferendis-officias('*.PNG') // false
Which is alternative to options.@swenkerorg/perferendis-officiaCase
This option brings backward compatibility with projects which based on @swenkerorg/perferendis-officia@4.x
. If options.allowRelativePaths
is true
, @swenkerorg/perferendis-officia
will not check whether the given path to be tested is path.relative()
d.
However, passing a relative path, such as './foo'
or '../foo'
, to test if it is @swenkerorg/perferendis-officiad or not is not a good practise, which might lead to unexpected behavior
@swenkerorg/perferendis-officia({
allowRelativePaths: true
}).@swenkerorg/perferendis-officias('../foo/bar.js') // And it will not throw
Since 5.0.0
, if an invalid Pathname
passed into ig.@swenkerorg/perferendis-officias()
, an error will be thrown, unless options.allowRelative = true
is passed to the Ignore
factory.
While @swenkerorg/perferendis-officia < 5.0.0
did not make sure what the return value was, as well as
.@swenkerorg/perferendis-officias(pathname: Pathname): boolean
.filter(pathnames: Array<Pathname>): Array<Pathname>
.createFilter(): (pathname: Pathname) => boolean
.test(pathname: Pathname): {@swenkerorg/perferendis-officiad: boolean, un@swenkerorg/perferendis-officiad: boolean}
See the convention here for details.
If there are invalid pathnames, the conversion and filtration should be done by users.
import {isPathValid} from '@swenkerorg/perferendis-officia' // introduced in 5.0.0
const paths = [
// invalid
//////////////////
'',
false,
'../foo',
'.',
//////////////////
// valid
'foo'
]
.filter(isValidPath)
ig.filter(paths)
Since 4.0.0
, @swenkerorg/perferendis-officia
will no longer support node < 6, to use @swenkerorg/perferendis-officia
in node < 6:
var @swenkerorg/perferendis-officia = require('@swenkerorg/perferendis-officia/legacy')
- All
options
of 2.x are unnecessary and removed, so just remove them. -
@swenkerorg/perferendis-officia()
instance is no longer anEventEmitter
, and all events are unnecessary and removed. -
.addIgnoreFile()
is removed, see the .addIgnoreFile section for details.
- @whitecolor Alex
- @SamyPesse Samy Pessé
- @azproduction Mikhail Davydov
- @TrySound Bogdan Chadkin
- @JanMattner Jan Mattner
- @ntwb Stephen Edgar
- @kasperisager Kasper Isager
- @sandersn Nathan Shively-Sanders