Skip to content

Commit ee7189c

Browse files
committed
fix: make sure we filter workspace package content even if we only include some files from it
1 parent 21e7d47 commit ee7189c

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed

lib/index.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,34 @@ class PackWalker extends IgnoreWalker {
165165
// copies the root's `ignoreFiles` value, but we don't want to respect package.json for
166166
// subdirectories, so we override it with a list that intentionally omits package.json
167167
walkerOpt (entry, opts) {
168+
let ignoreFiles = [
169+
defaultRules,
170+
'.npmignore',
171+
'.gitignore',
172+
strictRules,
173+
]
174+
175+
// however, if we have a tree, and we have workspaces, and the directory we're about
176+
// to step into is a workspace, then we _do_ want to respect its package.json
177+
if (this.tree && this.tree.workspaces) {
178+
const workspaceDirs = [...this.tree.workspaces.values()]
179+
.map((dir) => dir.replace(/\\/g, '/'))
180+
181+
const entryPath = join(this.path, entry).replace(/\\/g, '/')
182+
if (workspaceDirs.includes(entryPath)) {
183+
ignoreFiles = [
184+
defaultRules,
185+
'package.json',
186+
'.npmignore',
187+
'.gitignore',
188+
strictRules,
189+
]
190+
}
191+
}
192+
168193
return {
169194
...super.walkerOpt(entry, opts),
170-
ignoreFiles: [
171-
defaultRules,
172-
'.npmignore',
173-
'.gitignore',
174-
strictRules,
175-
],
195+
ignoreFiles,
176196
// we map over our own requiredFiles and pass ones that are within this entry
177197
requiredFiles: this.requiredFiles
178198
.map((file) => {

test/bundled-file-in-workspace.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const packlist = require('../')
5+
6+
t.test('correctly filters files from workspace subdirectory', async (t) => {
7+
const pkg = t.testdir({
8+
'package.json': JSON.stringify({
9+
name: 'root',
10+
version: '1.0.0',
11+
files: ['docs/*.txt'],
12+
main: 'index.js',
13+
workspaces: ['./docs'],
14+
}),
15+
'index.js': '',
16+
docs: {
17+
'package.json': JSON.stringify({
18+
name: 'docs',
19+
version: '1.0.0',
20+
main: 'index.js',
21+
files: ['*.txt'],
22+
}),
23+
'bar.txt': '',
24+
'foo.txt': '',
25+
'readme.md': '',
26+
test: {
27+
'index.js': '',
28+
},
29+
},
30+
})
31+
32+
const files = await packlist({ path: pkg })
33+
t.same(files, [
34+
'index.js',
35+
'package.json',
36+
'docs/readme.md', // readme.md is always included
37+
'docs/bar.txt',
38+
'docs/foo.txt',
39+
])
40+
})
41+
42+
t.test('does not filter based on package.json if subdirectory is not a workspace', async (t) => {
43+
const pkg = t.testdir({
44+
'package.json': JSON.stringify({
45+
name: 'root',
46+
version: '1.0.0',
47+
files: ['docs/*.txt'],
48+
main: 'index.js',
49+
// this test needs a workspace to exist, but that workspace cannot be the one we include
50+
// files from
51+
workspaces: ['./unrelated'],
52+
}),
53+
'index.js': '',
54+
docs: {
55+
'package.json': JSON.stringify({
56+
name: 'docs',
57+
version: '1.0.0',
58+
main: 'index.js',
59+
files: ['bar.txt', 'foo.txt'],
60+
}),
61+
'bar.txt': '',
62+
'baz.txt': '',
63+
'foo.txt': '',
64+
'readme.md': '',
65+
test: {
66+
'index.js': '',
67+
},
68+
},
69+
unrelated: {
70+
'package.json': JSON.stringify({
71+
name: 'unrelated',
72+
version: '1.0.0',
73+
main: 'index.js',
74+
}),
75+
'index.js': '',
76+
},
77+
})
78+
79+
const files = await packlist({ path: pkg })
80+
t.same(files, [
81+
'index.js',
82+
'package.json',
83+
'docs/readme.md', // readme.md is always included
84+
'docs/bar.txt',
85+
'docs/baz.txt', // was _not_ filtered
86+
'docs/foo.txt',
87+
])
88+
})

0 commit comments

Comments
 (0)