npm-packlist 5.0.1 → 5.0.4

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 +54 -4
  2. package/package.json +3 -3
package/lib/index.js CHANGED
@@ -31,6 +31,34 @@ const packageMustHavesRE = new RegExp(`^(${packageMustHaveFileNames})(\\..*[^~$]
31
31
 
32
32
  const fs = require('fs')
33
33
  const glob = require('glob')
34
+ const globify = pattern => pattern.split('\\').join('/')
35
+
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
+ }
34
62
 
35
63
  const pathHasPkg = (input) => {
36
64
  if (!input.startsWith('node_modules/')) {
@@ -118,9 +146,31 @@ class Walker extends IgnoreWalker {
118
146
  this.bundledScopes = Array.from(new Set(
119
147
  this.bundled.filter(f => /^@/.test(f))
120
148
  .map(f => f.split('/')[0])))
121
- const rules = defaultRules.join('\n') + '\n'
122
149
  this.packageJsonCache = this.parent ? this.parent.packageJsonCache
123
150
  : (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
+
124
174
  super.onReadIgnoreFile(rootBuiltinRules, rules, _ => _)
125
175
  } else {
126
176
  this.bundled = []
@@ -241,8 +291,8 @@ class Walker extends IgnoreWalker {
241
291
  if (excl) {
242
292
  pattern = pattern.slice(excl[0].length)
243
293
  }
244
- // strip off any / from the start of the pattern. /foo => foo
245
- pattern = pattern.replace(/^\/+/, '')
294
+ // strip off any / or ./ from the start of the pattern. /foo => foo, ./foo => foo
295
+ pattern = pattern.replace(/^\.?\/+/, '')
246
296
  // an odd number of ! means a negated pattern. !!foo ==> foo
247
297
  const negate = excl && excl[0].length % 2 === 1
248
298
  set.push({ pattern, negate })
@@ -428,7 +478,7 @@ class Walker extends IgnoreWalker {
428
478
  }
429
479
 
430
480
  globFiles (pattern, cb) {
431
- glob(pattern, { dot: true, cwd: this.path, nocase: true }, cb)
481
+ glob(globify(pattern), { dot: true, cwd: this.path, nocase: true }, cb)
432
482
  }
433
483
 
434
484
  readPackageJson (entries) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npm-packlist",
3
- "version": "5.0.1",
3
+ "version": "5.0.4",
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.1",
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.1"
59
+ "version": "3.5.0"
60
60
  }
61
61
  }