dat-ignore
Advanced tools
Comparing version 0.0.0 to 1.0.0
35
index.js
@@ -1,13 +0,18 @@ | ||
var assert = require('assert') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var match = require('anymatch') | ||
var xtend = require('xtend') | ||
module.exports = ignore | ||
function ignore (dir) { | ||
var opts = { | ||
datignore: path.join(dir, '.datignore') | ||
function ignore (dir, opts) { | ||
if (typeof dir !== 'string') { | ||
opts = dir | ||
dir = null | ||
} | ||
opts.ignore = opts.ignore // we want an array here | ||
opts = xtend({ | ||
datignorePath: dir ? path.join(dir, '.datignore') : '.datignore' | ||
}, opts) | ||
var ignoreMatches = opts.ignore // we end up with array of ignores here | ||
? Array.isArray(opts.ignore) | ||
@@ -17,13 +22,14 @@ ? opts.ignore | ||
: [] | ||
var defaultIgnore = [/^(?:\/.*)?\.dat(?:\/.*)?$/] // ignore .dat | ||
var ignoreHidden = [/[/\\]\./] | ||
var datIgnore = !(opts.useDatIgnore === false) ? readDatIgnore() : [] | ||
var defaultIgnore = [/^(?:\/.*)?\.dat(?:\/.*)?$/, '.DS_Store'] // ignore .dat (and DS_Store) | ||
var ignoreHidden = !(opts.ignoreHidden === false) ? [/(^\.|\/\.).*/] : null // ignore hidden files anywhere | ||
var datIgnore = !(opts.useDatIgnore === false) ? readDatIgnore() : null | ||
// Add ignore options | ||
opts.ignore = opts.ignore.concat(defaultIgnore) // always ignore .dat folder | ||
if (datIgnore) opts.ignore = opts.ignore.concat(datIgnore) // add .datignore | ||
if (opts.ignoreHidden !== false) opts.ignore = opts.ignore.concat(ignoreHidden) // ignore all hidden things | ||
ignoreMatches = ignoreMatches.concat(defaultIgnore) // always ignore .dat folder | ||
if (datIgnore) ignoreMatches = ignoreMatches.concat(datIgnore) // add .datignore | ||
if (ignoreHidden) ignoreMatches = ignoreMatches.concat(ignoreHidden) // ignore all hidden things | ||
return function (file) { | ||
return match(opts.ignore, file) | ||
return match(ignoreMatches, file) | ||
} | ||
@@ -33,3 +39,6 @@ | ||
try { | ||
return fs.readFileSync(opts.datignore, 'utf8') | ||
var ignores = opts.datignore || fs.readFileSync(opts.datignorePath, 'utf8') | ||
if (ignores && typeof opts.datignore !== 'string') ignores = ignores.toString() | ||
return ignores | ||
.trim() | ||
.split('\n') | ||
@@ -36,0 +45,0 @@ .filter(function (str) { |
{ | ||
"name": "dat-ignore", | ||
"description": "default ignore for dat", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"author": "Joe Hand <joe@hand.email>", | ||
@@ -28,4 +28,5 @@ "bugs": { | ||
"dependencies": { | ||
"anymatch": "^1.3.0" | ||
"anymatch": "^1.3.0", | ||
"xtend": "^4.0.1" | ||
} | ||
} |
@@ -9,9 +9,9 @@ # dat-ignore | ||
[npm-image]: https://img.shields.io/npm/v/dat-ignore.svg?style=flat-square | ||
[npm-url]: https://www.npmjs.com/package/dat-ignore | ||
[travis-image]: https://img.shields.io/travis/joehand/dat-ignore.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/joehand/dat-ignore | ||
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square | ||
[standard-url]: http://npm.im/standard | ||
Check if a file should be ignored for Dat: | ||
* Ignore `.dat` by default | ||
* Use the `.datignore` file | ||
* Optionally ignore all hidden files | ||
* Add in other custom ignore matches | ||
## Install | ||
@@ -35,4 +35,79 @@ | ||
Uses [anymatch](https://github.com/es128/anymatch) to match file paths. | ||
### Example Options | ||
Common configuration options. | ||
#### Add custom ignore | ||
```js | ||
var ignore = datIgnore('/data/dir', { | ||
ignore: [ | ||
'**/node_modules/**', | ||
'path/to/file.js', | ||
'path/anyjs/**/*.js' | ||
] | ||
}) | ||
``` | ||
#### Allow Hidden Files | ||
```js | ||
var ignore = datIgnore('/data/dir', { ignoreHidden: false }) | ||
``` | ||
#### Change Dat Ignore Path | ||
```js | ||
var ignore = datIgnore('/data/dir', { | ||
datignorePath: '~/.datignore' | ||
}) | ||
``` | ||
#### `.datignore` as string/buffer | ||
Pass in a string as a newline delimited list of things to ignore. | ||
```js | ||
var datIgnoreFile = fs.readFileSync('~/.datignore') | ||
datIgnoreFile += '\n' + fs.readFileSync(path.join(dir, '.datignore')) | ||
datIgnoreFile += '\n' + fs.readFileSync(path.join(dir, '.gitignore')) | ||
var ignore = datIgnore('/data/dir', { datignore: datIgnoreFile }) | ||
``` | ||
## API | ||
### `var ignore = datIgnore([dir], [opts])` | ||
Returns a function that checks if a path should be ignored: | ||
```js | ||
ignore('.dat') // true | ||
ignore('.git') // true | ||
ignore('data/cats.csv') // false | ||
``` | ||
#### `dir` | ||
`dir` is used to find `.datignore` and is prepended to paths found in `.datignore`. | ||
#### Options: | ||
* `opts.ignore` - Extend custom ignore with any anymatch string or array. | ||
* `opts.useDatIgnore` - Use the `.datignore` file in `dir` (default: true) | ||
* `opts.ignoreHidden` - Ignore all hidden files/folders (default: true) | ||
* `opts.datignorePath` - Path to `.datignore` file (default: `dir/.datignore`) | ||
* `opts.datignore` - Pass `.datignore` as buffer or string | ||
## License | ||
[MIT](LICENSE.md) | ||
[npm-image]: https://img.shields.io/npm/v/dat-ignore.svg?style=flat-square | ||
[npm-url]: https://www.npmjs.com/package/dat-ignore | ||
[travis-image]: https://img.shields.io/travis/joehand/dat-ignore.svg?style=flat-square | ||
[travis-url]: https://travis-ci.org/joehand/dat-ignore | ||
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square | ||
[standard-url]: http://npm.im/standard |
@@ -0,6 +1,107 @@ | ||
var fs = require('fs') | ||
var path = require('path') | ||
var test = require('tape') | ||
test('Example Test', function (t) { | ||
t.plan(1) | ||
t.error('No tests defined.') | ||
var datIgnore = require('..') | ||
test('default ignore with dir', function (t) { | ||
var ignore = datIgnore(__dirname) | ||
checkDefaults(t, ignore) | ||
// Dat Ignore stuff | ||
t.notOk(ignore('index.js'), 'index.js without path not ignored by .datignore') | ||
t.ok(ignore(path.join(__dirname, 'index.js')), 'full path index.js is ignored by .datignore') | ||
t.end() | ||
}) | ||
test('custom ignore extends default (string)', function (t) { | ||
var ignore = datIgnore(__dirname, {ignore: '**/*.js'}) | ||
t.ok(ignore('.dat'), '.dat folder ignored') | ||
t.ok(ignore('foo/bar.js'), 'custom ignore works') | ||
t.notOk(ignore('foo/bar.txt'), 'txt file gets to come along =)') | ||
t.end() | ||
}) | ||
test('custom ignore extends default (array)', function (t) { | ||
var ignore = datIgnore(__dirname, {ignore: ['super_secret_stuff/*', '**/*.txt']}) | ||
t.ok(ignore('.dat'), '.dat still feeling left out =(') | ||
t.ok(ignore('password.txt'), 'file ignored') | ||
t.ok(ignore('super_secret_stuff/file.js'), 'secret stuff stays secret') | ||
t.notOk(ignore('foo/bar.js'), 'js file joins the party =)') | ||
t.end() | ||
}) | ||
test('ignore hidden option turned off', function (t) { | ||
var ignore = datIgnore(__dirname, {ignoreHidden: false}) | ||
t.ok(ignore('.dat'), '.dat still feeling left out =(') | ||
t.notOk(ignore('.other-hidden'), 'hidden file NOT ignored') | ||
t.notOk(ignore('dir/.git'), 'hidden folders with dir NOT ignored') | ||
t.end() | ||
}) | ||
test('useDatIgnore false', function (t) { | ||
var ignore = datIgnore(__dirname, {useDatIgnore: false}) | ||
t.ok(ignore('.dat'), '.dat ignored') | ||
t.notOk(ignore(path.join(__dirname, 'index.js')), 'file in datignore not ignored') | ||
t.end() | ||
}) | ||
test('change datignorePath', function (t) { | ||
var ignore = datIgnore(path.join(__dirname, '..'), {datignorePath: path.join(__dirname, '.datignore')}) | ||
t.ok(ignore('.dat'), '.dat ignored') | ||
t.notOk(ignore('index.js'), 'wrong full path in datignore not ignored') | ||
t.notOk(ignore(path.join(__dirname, 'index.js')), 'wrong full path in datignore not ignored') | ||
t.ok(ignore(path.join(__dirname, '..', 'index.js')), 'file in datignore ignored') | ||
t.end() | ||
}) | ||
test('datignore as buf', function (t) { | ||
var ignore = datIgnore(__dirname, {datignore: fs.readFileSync(path.join(__dirname, '.datignore'))}) | ||
t.ok(ignore('.dat'), '.dat ignored') | ||
t.notOk(ignore('index.js'), 'wrong full path in datignore not ignored') | ||
t.ok(ignore(path.join(__dirname, 'index.js')), 'file in datignore ignored') | ||
t.end() | ||
}) | ||
test('datignore as str', function (t) { | ||
var ignore = datIgnore(__dirname, {datignore: fs.readFileSync(path.join(__dirname, '.datignore'), 'utf-8')}) | ||
t.ok(ignore('.dat'), '.dat ignored') | ||
t.notOk(ignore('index.js'), 'wrong full path in datignore not ignored') | ||
t.ok(ignore(path.join(__dirname, 'index.js')), 'file in datignore ignored') | ||
t.end() | ||
}) | ||
test('without dir ok', function (t) { | ||
var ignore = datIgnore() | ||
checkDefaults(t, ignore) | ||
t.end() | ||
}) | ||
function checkDefaults (t, ignore) { | ||
// Default Ignore | ||
t.ok( | ||
['.dat', '/.dat', '.dat/', 'sub/.dat'].filter(ignore).length === 4, | ||
'always ignore .dat folder regardless of /') | ||
t.ok( | ||
['.dat/foo.bar', '/.dat/foo.bar', '.dat/dir/foo'].filter(ignore).length === 3, | ||
'files in .dat folder ignored') | ||
t.ok(ignore('.DS_Store'), 'no thanks DS_Store') | ||
// Hidden Folder/Files Ignored | ||
t.ok( | ||
[ | ||
'.git', '/.git', '.git/', | ||
'.git/sub', '.git/file.txt', 'dir/.git', 'dir/.git/test.txt' | ||
].filter(ignore).length === 7, 'files in .dat folder ignored') | ||
// Dat Ignore stuff | ||
t.ok(ignore('.datignore'), 'let .datignore through') | ||
// Things to Allow | ||
t.notOk(ignore('folder/asdf.data/file.txt'), 'weird data folder is ok') | ||
t.notOk( | ||
['file.dat', 'file.dat.jpg', 'the.dat-thing'].filter(ignore).length !== 0, | ||
'does not ignore files/folders with .dat in it') | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
13166
10
136
0
112
2
2
+ Addedxtend@^4.0.1
+ Addedxtend@4.0.2(transitive)