Skip to content

Commit da1ba4a

Browse files
authored
fix: correctly ignore .gitignore when a .npmignore is present (#108)
1 parent 9393e2e commit da1ba4a

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ const glob = require('glob')
3434
const globify = pattern => pattern.split('\\').join('/')
3535

3636
const readOutOfTreeIgnoreFiles = (root, rel, result = '') => {
37-
for (const file of ['.gitignore', '.npmignore']) {
37+
for (const file of ['.npmignore', '.gitignore']) {
3838
try {
3939
const ignoreContent = fs.readFileSync(path.join(root, file), { encoding: 'utf8' })
4040
result += ignoreContent + '\n'
41+
// break the loop immediately after concatting, this allows us to prioritize the
42+
// .npmignore and discard the .gitignore if one exists
43+
break
4144
} catch (err) {
4245
// we ignore ENOENT errors completely because we don't care if the file doesn't exist
4346
// but we throw everything else because failing to read a file that does exist is

test/workspace.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,67 @@ t.test('packing a workspace root does not include children', async (t) => {
109109
'workspaces/foo/package.json',
110110
])
111111
})
112+
113+
t.test('.gitignore is discarded if .npmignore exists outside of tree', async (t) => {
114+
const root = t.testdir({
115+
'package.json': JSON.stringify({
116+
name: 'workspace-root',
117+
version: '1.0.0',
118+
main: 'root.js',
119+
workspaces: ['./workspaces/foo'],
120+
}),
121+
'root.js': `console.log('hello')`,
122+
'.gitignore': 'dont-ignore-me',
123+
'.npmignore': 'only-ignore-me',
124+
'dont-ignore-me': 'should not be ignored',
125+
'only-ignore-me': 'should be ignored',
126+
workspaces: {
127+
'.gitignore': 'dont-ignore-me-either',
128+
'.npmignore': 'ignore-me-also',
129+
'dont-ignore-me': 'should not be ignored',
130+
'dont-ignore-me-either': 'should not be ignored',
131+
'only-ignore-me': 'should be ignored',
132+
'ignore-me-also': 'should be ignored',
133+
foo: {
134+
'package.json': JSON.stringify({
135+
name: 'workspace-child',
136+
version: '1.0.0',
137+
main: 'child.js',
138+
}),
139+
'child.js': `console.log('hello')`,
140+
'dont-ignore-me': 'should not be ignored',
141+
'dont-ignore-me-either': 'should not be ignored',
142+
'only-ignore-me': 'should be ignored',
143+
'ignore-me-also': 'should also be ignored',
144+
},
145+
},
146+
})
147+
148+
const workspacePath = path.join(root, 'workspaces', 'foo')
149+
// this simulates what it looks like when a user does i.e. npm pack -w ./workspaces/foo
150+
const files = await packlist({
151+
path: workspacePath,
152+
prefix: root,
153+
workspaces: [workspacePath],
154+
})
155+
t.same(files, [
156+
'dont-ignore-me',
157+
'dont-ignore-me-either',
158+
'child.js',
159+
'package.json',
160+
])
161+
162+
// here we leave off workspaces to satisfy coverage
163+
const secondFiles = await packlist({
164+
path: workspacePath,
165+
prefix: root,
166+
})
167+
t.same(secondFiles, [
168+
'dont-ignore-me',
169+
'dont-ignore-me-either',
170+
'ignore-me-also',
171+
'only-ignore-me',
172+
'child.js',
173+
'package.json',
174+
])
175+
})

0 commit comments

Comments
 (0)