npm-packlist 5.0.3 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +53 -1
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -33,6 +33,36 @@ const fs = require('fs')
33
33
  const glob = require('glob')
34
34
  const globify = pattern => pattern.split('\\').join('/')
35
35
 
36
+ const readOutOfTreeIgnoreFiles = (root, rel, result = '') => {
37
+ for (const file of ['.npmignore', '.gitignore']) {
38
+ try {
39
+ const ignoreContent = fs.readFileSync(path.join(root, file), { encoding: 'utf8' })
40
+ 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
44
+ } catch (err) {
45
+ // we ignore ENOENT errors completely because we don't care if the file doesn't exist
46
+ // but we throw everything else because failing to read a file that does exist is
47
+ // something that the user likely wants to know about. we don't need to test this.
48
+ /* istanbul ignore next */
49
+ if (err.code !== 'ENOENT') {
50
+ throw err
51
+ }
52
+ }
53
+ }
54
+
55
+ if (!rel) {
56
+ return result
57
+ }
58
+
59
+ const firstRel = rel.split(path.sep)[0]
60
+ const newRoot = path.join(root, firstRel)
61
+ const newRel = path.relative(newRoot, path.join(root, rel))
62
+
63
+ return readOutOfTreeIgnoreFiles(newRoot, newRel, result)
64
+ }
65
+
36
66
  const pathHasPkg = (input) => {
37
67
  if (!input.startsWith('node_modules/')) {
38
68
  return false
@@ -119,9 +149,31 @@ class Walker extends IgnoreWalker {
119
149
  this.bundledScopes = Array.from(new Set(
120
150
  this.bundled.filter(f => /^@/.test(f))
121
151
  .map(f => f.split('/')[0])))
122
- const rules = defaultRules.join('\n') + '\n'
123
152
  this.packageJsonCache = this.parent ? this.parent.packageJsonCache
124
153
  : (opt.packageJsonCache || new Map())
154
+ let rules = defaultRules.join('\n') + '\n'
155
+
156
+ if (opt.prefix && opt.workspaces) {
157
+ const gPath = globify(opt.path)
158
+ const gPrefix = globify(opt.prefix)
159
+ const gWorkspaces = opt.workspaces.map((ws) => globify(ws))
160
+ // if opt.path and opt.prefix are not the same directory, and opt.workspaces has opt.path
161
+ // in it, then we know that opt.path is a workspace directory. in order to not drop ignore
162
+ // rules from directories between the workspace root (opt.prefix) and the workspace itself
163
+ // (opt.path), we need to find and read those now
164
+ /* istanbul ignore else */
165
+ if (gPath !== gPrefix && gWorkspaces.includes(gPath)) {
166
+ // relpath is the relative path between the prefix and the parent of opt.path
167
+ // we use the parent because ignore-walk will read the files in opt.path already
168
+ const relpath = path.relative(opt.prefix, path.dirname(opt.path))
169
+ rules += readOutOfTreeIgnoreFiles(opt.prefix, relpath)
170
+ } else if (gPath === gPrefix) {
171
+ // on the other hand, if the path and the prefix are the same, then we ignore workspaces
172
+ // so that we don't pack workspaces inside of a root project
173
+ rules += opt.workspaces.map((ws) => globify(path.relative(opt.path, ws))).join('\n')
174
+ }
175
+ }
176
+
125
177
  super.onReadIgnoreFile(rootBuiltinRules, rules, _ => _)
126
178
  } else {
127
179
  this.bundled = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npm-packlist",
3
- "version": "5.0.3",
3
+ "version": "5.1.1",
4
4
  "description": "Get a list of the files to add from a folder into an npm package",
5
5
  "directories": {
6
6
  "test": "test"
@@ -20,7 +20,7 @@
20
20
  ],
21
21
  "devDependencies": {
22
22
  "@npmcli/eslint-config": "^3.0.1",
23
- "@npmcli/template-oss": "3.4.2",
23
+ "@npmcli/template-oss": "3.5.0",
24
24
  "mutate-fs": "^2.1.1",
25
25
  "tap": "^16.0.1"
26
26
  },
@@ -56,6 +56,6 @@
56
56
  },
57
57
  "templateOSS": {
58
58
  "//@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"
60
60
  }
61
61
  }