Skip to content

Commit d5b653c

Browse files
authored
fix: account for directories and files prefixed with ./ (#140)
1 parent 2ec75c0 commit d5b653c

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

lib/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,13 @@ class PackWalker extends IgnoreWalker {
294294

295295
// if we have a files array in our package, we need to pull rules from it
296296
if (files) {
297-
for (const file of files) {
297+
for (let file of files) {
298298
// invert the rule because these are things we want to include
299+
if (file.startsWith('/')) {
300+
file = file.slice(1)
301+
} else if (file.startsWith('./')) {
302+
file = file.slice(2)
303+
}
299304
const inverse = `!${file}`
300305
try {
301306
// 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 {
305310
// if we have a file and we know that, it's strictly required
306311
if (stat.isFile()) {
307312
strict.unshift(inverse)
308-
this.requiredFiles.push(file.startsWith('/') ? file.slice(1) : file)
313+
this.requiredFiles.push(file)
309314
} else if (stat.isDirectory()) {
310315
// otherwise, it's a default ignore, and since we got here we know it's not a pattern
311316
// so we include the directory contents
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// In v1, this would exclude the 'lib/two.js' file, because
2+
// the .npmignore is deeper in the tree and thus had higher
3+
// precedence. In v2, because /lib/two.js is in the files
4+
// list as a file path, it will be included no matter what.
5+
'use strict'
6+
7+
const Arborist = require('@npmcli/arborist')
8+
const t = require('tap')
9+
const packlist = require('../')
10+
11+
const pkg = t.testdir({
12+
'package.json': JSON.stringify({
13+
files: [
14+
'/lib',
15+
'./lib2',
16+
],
17+
}),
18+
lib: {
19+
'one.js': 'one',
20+
'two.js': 'two',
21+
'tre.js': 'tre',
22+
'for.js': 'for',
23+
'.npmignore': 'two.js',
24+
},
25+
lib2: {
26+
'fiv.js': 'fiv',
27+
'.DS_Store': 'a store of ds',
28+
},
29+
})
30+
31+
t.test('package with slash directories', async (t) => {
32+
const arborist = new Arborist({ path: pkg })
33+
const tree = await arborist.loadActual()
34+
const files = await packlist(tree)
35+
t.same(files, [
36+
'lib2/fiv.js',
37+
'lib/for.js',
38+
'lib/one.js',
39+
'lib/tre.js',
40+
'package.json',
41+
])
42+
})

test/package-json-files-with-slashes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const pkg = t.testdir({
2727
},
2828
})
2929

30-
t.test('package with negated files', async (t) => {
30+
t.test('package with slash files', async (t) => {
3131
const arborist = new Arborist({ path: pkg })
3232
const tree = await arborist.loadActual()
3333
const files = await packlist(tree)

0 commit comments

Comments
 (0)