Skip to content

Commit dc38ab9

Browse files
authored
1 parent 0df3011 commit dc38ab9

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

node_modules/npm-packlist/lib/index.js

+50-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,33 @@ const fs = require('fs')
3333
const glob = require('glob')
3434
const globify = pattern => pattern.split('\\').join('/')
3535

36+
const readOutOfTreeIgnoreFiles = (root, rel, result = '') => {
37+
for (const file of ['.gitignore', '.npmignore']) {
38+
try {
39+
const ignoreContent = fs.readFileSync(path.join(root, file), { encoding: 'utf8' })
40+
result += ignoreContent + '\n'
41+
} catch (err) {
42+
// we ignore ENOENT errors completely because we don't care if the file doesn't exist
43+
// but we throw everything else because failing to read a file that does exist is
44+
// something that the user likely wants to know about. we don't need to test this.
45+
/* istanbul ignore next */
46+
if (err.code !== 'ENOENT') {
47+
throw err
48+
}
49+
}
50+
}
51+
52+
if (!rel) {
53+
return result
54+
}
55+
56+
const firstRel = rel.split(path.sep)[0]
57+
const newRoot = path.join(root, firstRel)
58+
const newRel = path.relative(newRoot, path.join(root, rel))
59+
60+
return readOutOfTreeIgnoreFiles(newRoot, newRel, result)
61+
}
62+
3663
const pathHasPkg = (input) => {
3764
if (!input.startsWith('node_modules/')) {
3865
return false
@@ -119,9 +146,31 @@ class Walker extends IgnoreWalker {
119146
this.bundledScopes = Array.from(new Set(
120147
this.bundled.filter(f => /^@/.test(f))
121148
.map(f => f.split('/')[0])))
122-
const rules = defaultRules.join('\n') + '\n'
123149
this.packageJsonCache = this.parent ? this.parent.packageJsonCache
124150
: (opt.packageJsonCache || new Map())
151+
let rules = defaultRules.join('\n') + '\n'
152+
153+
if (opt.prefix && opt.workspaces) {
154+
const gPath = globify(opt.path)
155+
const gPrefix = globify(opt.prefix)
156+
const gWorkspaces = opt.workspaces.map((ws) => globify(ws))
157+
// if opt.path and opt.prefix are not the same directory, and opt.workspaces has opt.path
158+
// in it, then we know that opt.path is a workspace directory. in order to not drop ignore
159+
// rules from directories between the workspace root (opt.prefix) and the workspace itself
160+
// (opt.path), we need to find and read those now
161+
/* istanbul ignore else */
162+
if (gPath !== gPrefix && gWorkspaces.includes(gPath)) {
163+
// relpath is the relative path between the prefix and the parent of opt.path
164+
// we use the parent because ignore-walk will read the files in opt.path already
165+
const relpath = path.relative(opt.prefix, path.dirname(opt.path))
166+
rules += readOutOfTreeIgnoreFiles(opt.prefix, relpath)
167+
} else if (gPath === gPrefix) {
168+
// on the other hand, if the path and the prefix are the same, then we ignore workspaces
169+
// so that we don't pack workspaces inside of a root project
170+
rules += opt.workspaces.map((ws) => globify(path.relative(opt.path, ws))).join('\n')
171+
}
172+
}
173+
125174
super.onReadIgnoreFile(rootBuiltinRules, rules, _ => _)
126175
} else {
127176
this.bundled = []

node_modules/npm-packlist/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "npm-packlist",
3-
"version": "5.0.3",
3+
"version": "5.0.4",
44
"description": "Get a list of the files to add from a folder into an npm package",
55
"directories": {
66
"test": "test"
@@ -20,7 +20,7 @@
2020
],
2121
"devDependencies": {
2222
"@npmcli/eslint-config": "^3.0.1",
23-
"@npmcli/template-oss": "3.4.2",
23+
"@npmcli/template-oss": "3.5.0",
2424
"mutate-fs": "^2.1.1",
2525
"tap": "^16.0.1"
2626
},
@@ -56,6 +56,6 @@
5656
},
5757
"templateOSS": {
5858
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
59-
"version": "3.4.2"
59+
"version": "3.5.0"
6060
}
6161
}

package-lock.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -5136,9 +5136,9 @@
51365136
}
51375137
},
51385138
"node_modules/npm-packlist": {
5139-
"version": "5.0.3",
5140-
"resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-5.0.3.tgz",
5141-
"integrity": "sha512-KuSbzgejxdsAWbNNyEs8EsyDHsO+nJF6k+9WuWzFbSNh5tFHs4lDApXw7kntKpuehfp8lKRzJkMtz0+WmGvTIw==",
5139+
"version": "5.0.4",
5140+
"resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-5.0.4.tgz",
5141+
"integrity": "sha512-G4sCWzzHokHC5oxGD46Q9vCe+eN2tFb+3RfADD/eZbx4nKa7Y1eku1xvIWrw5R3F3hWX7IM2BgdqbQsyBUa3IA==",
51425142
"inBundle": true,
51435143
"dependencies": {
51445144
"glob": "^8.0.1",
@@ -13604,9 +13604,9 @@
1360413604
}
1360513605
},
1360613606
"npm-packlist": {
13607-
"version": "5.0.3",
13608-
"resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-5.0.3.tgz",
13609-
"integrity": "sha512-KuSbzgejxdsAWbNNyEs8EsyDHsO+nJF6k+9WuWzFbSNh5tFHs4lDApXw7kntKpuehfp8lKRzJkMtz0+WmGvTIw==",
13607+
"version": "5.0.4",
13608+
"resolved": "https://registry.npmjs.org/npm-packlist/-/npm-packlist-5.0.4.tgz",
13609+
"integrity": "sha512-G4sCWzzHokHC5oxGD46Q9vCe+eN2tFb+3RfADD/eZbx4nKa7Y1eku1xvIWrw5R3F3hWX7IM2BgdqbQsyBUa3IA==",
1361013610
"requires": {
1361113611
"glob": "^8.0.1",
1361213612
"ignore-walk": "^5.0.1",

0 commit comments

Comments
 (0)