From 5bf0cfe89720bf7f94c98fb502f1c4349d3e7e25 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Thu, 8 Sep 2016 12:38:19 -0700 Subject: [PATCH] Fix ignore (#151) * switch to better ignore module, improve debugging * add regression test for prefix bug (#142) * remove old lib --- lib/get-files.js | 33 +++++++++---------- package.json | 1 - test/_fixtures/prefix-regression/.npmignore | 1 + test/_fixtures/prefix-regression/package.json | 6 ++++ test/_fixtures/prefix-regression/woot.js | 1 + test/index.js | 8 +++++ 6 files changed, 32 insertions(+), 18 deletions(-) create mode 100644 test/_fixtures/prefix-regression/.npmignore create mode 100644 test/_fixtures/prefix-regression/package.json create mode 100644 test/_fixtures/prefix-regression/woot.js diff --git a/lib/get-files.js b/lib/get-files.js index 2f697c8..81512be 100644 --- a/lib/get-files.js +++ b/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'); -} +}; diff --git a/package.json b/package.json index 21af2d4..d7a8607 100644 --- a/package.json +++ b/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", diff --git a/test/_fixtures/prefix-regression/.npmignore b/test/_fixtures/prefix-regression/.npmignore new file mode 100644 index 0000000..96b3774 --- /dev/null +++ b/test/_fixtures/prefix-regression/.npmignore @@ -0,0 +1 @@ +woot diff --git a/test/_fixtures/prefix-regression/package.json b/test/_fixtures/prefix-regression/package.json new file mode 100644 index 0000000..196283f --- /dev/null +++ b/test/_fixtures/prefix-regression/package.json @@ -0,0 +1,6 @@ +{ + "name": "prefix-regression", + "version": "0.0.1", + "description": "", + "dependencies": {} +} diff --git a/test/_fixtures/prefix-regression/woot.js b/test/_fixtures/prefix-regression/woot.js new file mode 100644 index 0000000..82ccf67 --- /dev/null +++ b/test/_fixtures/prefix-regression/woot.js @@ -0,0 +1 @@ +// hello diff --git a/test/index.js b/test/index.js index bb35ff3..610e362 100644 --- a/test/index.js +++ b/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'); +});