Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
39a62f3
commit a1ae203
Showing
5 changed files
with
87 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const gitIgnore = require('ignore'); | ||
const globby = require('globby'); | ||
const slash = require('slash'); | ||
|
||
const mapGitIgnorePatternTo = base => ignore => { | ||
if (ignore.startsWith('!')) { | ||
return '!' + path.posix.join(base, ignore.substr(1)); | ||
} | ||
|
||
return path.posix.join(base, ignore); | ||
}; | ||
|
||
const parseGitIgnore = (content, opts) => { | ||
const base = slash(path.relative(opts.cwd, path.dirname(opts.fileName))); | ||
|
||
return content | ||
.split(/\r?\n/) | ||
.filter(Boolean) | ||
.filter(l => l.charAt(0) !== '#') | ||
.map(mapGitIgnorePatternTo(base)); | ||
}; | ||
|
||
const getGitIgnoreFilter = opts => { | ||
const ignore = opts.ignores || []; | ||
const cwd = opts.cwd || process.cwd(); | ||
|
||
const i = globby.sync('**/.gitignore', {ignore, cwd}) | ||
.reduce((ignores, file) => { | ||
const fileName = path.join(cwd, file); | ||
const content = fs.readFileSync(fileName, 'utf8'); | ||
ignores.add(parseGitIgnore(content, {cwd, fileName})); | ||
return ignores; | ||
}, gitIgnore()); | ||
|
||
return p => !i.ignores(slash(path.relative(cwd, p))); | ||
}; | ||
|
||
exports.getGitIgnoreFilter = getGitIgnoreFilter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import proxyquire from 'proxyquire'; | ||
|
||
process.chdir(__dirname); | ||
|
||
const manager = proxyquire('../options-manager', { | ||
'resolve-from': (cwd, path) => `cwd/${path}` | ||
}); | ||
|
||
test('gitignore', t => { | ||
const cwd = path.join(__dirname, 'fixtures/gitignore/test'); | ||
const filter = manager.getGitIgnoreFilter({cwd}); | ||
const actual = ['foo.js', 'bar.js'].filter(filter); | ||
const expected = ['bar.js']; | ||
t.deepEqual(actual, expected); | ||
}); | ||
|
||
test('ignore ignored .gitignore', t => { | ||
const opts = { | ||
cwd: path.join(__dirname, 'fixtures/gitignore'), | ||
ignores: ['**/test/**'] | ||
}; | ||
|
||
const filter = manager.getGitIgnoreFilter(opts); | ||
const actual = ['foo.js'].filter(filter); | ||
const expected = ['foo.js']; | ||
t.deepEqual(actual, expected); | ||
}); | ||
|
||
test.serial('patterns should be translated according to process.cwd()', t => { | ||
const previous = process.cwd(); | ||
const cwd = path.join(__dirname, 'fixtures/gitignore'); | ||
process.chdir(cwd); | ||
try { | ||
const filter = manager.getGitIgnoreFilter({}); | ||
const actual = ['bar.js', 'test/foo.js', 'test/bar.js'].filter(filter); | ||
const expected = ['bar.js', 'test/bar.js']; | ||
t.deepEqual(actual, expected); | ||
} finally { | ||
process.chdir(previous); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters