Browse Source

Fix ignore (#151)

* switch to better ignore module, improve debugging

* add regression test for prefix bug (#142)

* remove old lib
master
Guillermo Rauch 8 years ago
committed by GitHub
parent
commit
5bf0cfe897
  1. 33
      lib/get-files.js
  2. 1
      package.json
  3. 1
      test/_fixtures/prefix-regression/.npmignore
  4. 6
      test/_fixtures/prefix-regression/package.json
  5. 1
      test/_fixtures/prefix-regression/woot.js
  6. 8
      test/index.js

33
lib/get-files.js

@ -1,10 +1,9 @@
import flatten from 'arr-flatten';
import unique from 'array-unique';
import minimatch from 'minimatch';
import IGNORED from './ignored';
import { resolve } from 'path';
import { stat, readdir, readFile } from 'fs-promise';
import parser from 'gitignore-parser';
import ignore from 'ignore';
/**
* Returns a list of files in the given
@ -41,19 +40,19 @@ export async function npm (path, pkg, {
// compile list of ignored patterns and files
const npmIgnore = await maybeRead(resolve(path, '.npmignore'), null);
const ignored = parser.compile(
const filter = ignore().add(
IGNORED +
'\n' +
clearRelative(null != npmIgnore
? npmIgnore
: await maybeRead(resolve(path, '.gitignore')))
);
).createFilter();
// if debugging is turned on, describe which files
// are being ignored
const prefixLength = path.length + 1;
const accepts = function (file) {
const accepted = ignored.accepts(file.substr(prefixLength));
const relativePath = file.substr(prefixLength);
if ('' === relativePath) return true;
const accepted = filter(relativePath);
if (!accepted && debug) {
console.log('> [debug] ignoring "%s"', file);
}
@ -61,9 +60,9 @@ export async function npm (path, pkg, {
};
// locate files
if (debug) console.time('> [debug] locating files');
if (debug) console.time(`> [debug] locating files ${path}`);
const files = await explode(search, { accepts, limit, debug });
if (debug) console.timeEnd('> [debug] locating files');
if (debug) console.timeEnd(`> [debug] locating files ${path}`);
// always include manifest as npm does not allow ignoring it
// source: https://docs.npmjs.com/files/package.json#files
@ -97,17 +96,17 @@ export async function docker (path, {
const search = search_.map((file) => asAbsolute(file, path));
// compile list of ignored patterns and files
const ignored = parser.compile(
const filter = ignore().add(
IGNORED +
'\n' +
await maybeRead(resolve(path, '.dockerignore'))
);
).createFilter();
// if debugging is turned on, describe which files
// are being ignored
const prefixLength = path.length + 1;
const accepts = function (file) {
const accepted = ignored.accepts(file.substr(prefixLength));
const relativePath = file.substr(prefixLength);
if ('' === relativePath) return true;
const accepted = filter(relativePath);
if (!accepted && debug) {
console.log('> [debug] ignoring "%s"', file);
}
@ -115,9 +114,9 @@ export async function docker (path, {
};
// locate files
if (debug) console.time('> [debug] locating files');
if (debug) console.time(`> [debug] locating files ${path}`);
const files = await explode(search, { accepts, limit, debug });
if (debug) console.timeEnd('> [debug] locating files');
if (debug) console.timeEnd(`> [debug] locating files ${path}`);
// always include manifest as npm does not allow ignoring it
// source: https://docs.npmjs.com/files/package.json#files
@ -215,4 +214,4 @@ const maybeRead = async function (path, default_ = '') {
const clearRelative = function (str) {
return str.replace(/(\n|^)\.\//g, '$1');
}
};

1
package.json

@ -66,7 +66,6 @@
"email-prompt": "0.1.8",
"email-validator": "1.0.5",
"fs-promise": "0.5.0",
"gitignore-parser": "0.0.2",
"graceful-fs": "4.1.6",
"ini": "1.3.4",
"minimatch": "3.0.3",

1
test/_fixtures/prefix-regression/.npmignore

@ -0,0 +1 @@
woot

6
test/_fixtures/prefix-regression/package.json

@ -0,0 +1,6 @@
{
"name": "prefix-regression",
"version": "0.0.1",
"description": "",
"dependencies": {}
}

1
test/_fixtures/prefix-regression/woot.js

@ -0,0 +1 @@
// hello

8
test/index.js

@ -120,3 +120,11 @@ test('support docker', async t => {
t.is(base(files[0]), 'dockerfile/Dockerfile');
t.is(base(files[1]), 'dockerfile/a.js');
});
test('prefix regression', async t => {
let files = await getNpmFiles(fixture('prefix-regression'));
files = files.sort(alpha);
t.is(files.length, 2);
t.is(base(files[0]), 'prefix-regression/package.json');
t.is(base(files[1]), 'prefix-regression/woot.js');
});

Loading…
Cancel
Save