From d5b653ce860f6601fb5007c7fcec903c8d58c1c8 Mon Sep 17 00:00:00 2001 From: Gar Date: Wed, 26 Oct 2022 09:19:22 -0700 Subject: [PATCH] fix: account for directories and files prefixed with `./` (#140) --- lib/index.js | 9 ++++-- test/package-json-dir-with-slashes.js | 42 +++++++++++++++++++++++++ test/package-json-files-with-slashes.js | 2 +- 3 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 test/package-json-dir-with-slashes.js diff --git a/lib/index.js b/lib/index.js index 853a99c..91606e4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -294,8 +294,13 @@ class PackWalker extends IgnoreWalker { // if we have a files array in our package, we need to pull rules from it if (files) { - for (const file of files) { + for (let file of files) { // invert the rule because these are things we want to include + if (file.startsWith('/')) { + file = file.slice(1) + } else if (file.startsWith('./')) { + file = file.slice(2) + } const inverse = `!${file}` try { // if an entry in the files array is a specific file, then we need to include it as a @@ -305,7 +310,7 @@ class PackWalker extends IgnoreWalker { // if we have a file and we know that, it's strictly required if (stat.isFile()) { strict.unshift(inverse) - this.requiredFiles.push(file.startsWith('/') ? file.slice(1) : file) + this.requiredFiles.push(file) } else if (stat.isDirectory()) { // otherwise, it's a default ignore, and since we got here we know it's not a pattern // so we include the directory contents diff --git a/test/package-json-dir-with-slashes.js b/test/package-json-dir-with-slashes.js new file mode 100644 index 0000000..b3fa466 --- /dev/null +++ b/test/package-json-dir-with-slashes.js @@ -0,0 +1,42 @@ +// In v1, this would exclude the 'lib/two.js' file, because +// the .npmignore is deeper in the tree and thus had higher +// precedence. In v2, because /lib/two.js is in the files +// list as a file path, it will be included no matter what. +'use strict' + +const Arborist = require('@npmcli/arborist') +const t = require('tap') +const packlist = require('../') + +const pkg = t.testdir({ + 'package.json': JSON.stringify({ + files: [ + '/lib', + './lib2', + ], + }), + lib: { + 'one.js': 'one', + 'two.js': 'two', + 'tre.js': 'tre', + 'for.js': 'for', + '.npmignore': 'two.js', + }, + lib2: { + 'fiv.js': 'fiv', + '.DS_Store': 'a store of ds', + }, +}) + +t.test('package with slash directories', async (t) => { + const arborist = new Arborist({ path: pkg }) + const tree = await arborist.loadActual() + const files = await packlist(tree) + t.same(files, [ + 'lib2/fiv.js', + 'lib/for.js', + 'lib/one.js', + 'lib/tre.js', + 'package.json', + ]) +}) diff --git a/test/package-json-files-with-slashes.js b/test/package-json-files-with-slashes.js index 3901fe2..7505ebb 100644 --- a/test/package-json-files-with-slashes.js +++ b/test/package-json-files-with-slashes.js @@ -27,7 +27,7 @@ const pkg = t.testdir({ }, }) -t.test('package with negated files', async (t) => { +t.test('package with slash files', async (t) => { const arborist = new Arborist({ path: pkg }) const tree = await arborist.loadActual() const files = await packlist(tree)