npm-packlist 2.1.5 → 3.0.0

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 (3) hide show
  1. package/bin/index.js +3 -2
  2. package/index.js +73 -58
  3. package/package.json +15 -5
package/bin/index.js CHANGED
@@ -12,13 +12,14 @@ process.argv.slice(2).forEach(arg => {
12
12
  dirs.push(arg)
13
13
  })
14
14
 
15
- const sort = list => doSort ? list.sort((a, b) => a.localeCompare(b)) : list
15
+ const sort = list => doSort ? list.sort((a, b) => a.localeCompare(b, 'en')) : list
16
16
 
17
17
  const packlist = require('../')
18
18
  if (!dirs.length)
19
19
  console.log(sort(packlist.sync({ path: process.cwd() })).join('\n'))
20
- else
20
+ else {
21
21
  dirs.forEach(path => {
22
22
  console.log(`> ${path}`)
23
23
  console.log(sort(packlist.sync({ path })).join('\n'))
24
24
  })
25
+ }
package/index.js CHANGED
@@ -26,11 +26,10 @@ const normalizePackageBin = require('npm-normalize-package-bin')
26
26
  // localized documentation and other use cases. Adding a `/` to
27
27
  // these rules, while tempting and arguably more "correct", is a
28
28
  // significant change that will break existing use cases.
29
- const packageMustHaveFileNames =
30
- 'readme|copying|license|licence|notice|changes|changelog|history'
29
+ const packageMustHaveFileNames = 'readme|copying|license|licence'
31
30
 
32
31
  const packageMustHaves = `@(${packageMustHaveFileNames}){,.*[^~$]}`
33
- const packageMustHavesRE = new RegExp(`^(${packageMustHaveFileNames})(\\..*[^~\$])?$`, 'i')
32
+ const packageMustHavesRE = new RegExp(`^(${packageMustHaveFileNames})(\\..*[^~$])?$`, 'i')
34
33
 
35
34
  const fs = require('fs')
36
35
  const glob = require('glob')
@@ -76,13 +75,11 @@ const npmWalker = Class => class Walker extends Class {
76
75
  'package.json',
77
76
  '.npmignore',
78
77
  '.gitignore',
79
- packageNecessaryRules
78
+ packageNecessaryRules,
80
79
  ]
81
80
 
82
81
  opt.includeEmpty = false
83
82
  opt.path = opt.path || process.cwd()
84
- const dirName = path.basename(opt.path)
85
- const parentName = path.basename(path.dirname(opt.path))
86
83
 
87
84
  // only follow links in the root node_modules folder, because if those
88
85
  // folders are included, it's because they're bundled, and bundles
@@ -90,7 +87,7 @@ const npmWalker = Class => class Walker extends Class {
90
87
  // This regexp tests to see that we're either a node_modules folder,
91
88
  // or a @scope within a node_modules folder, in the root's node_modules
92
89
  // hierarchy (ie, not in test/foo/node_modules/ or something).
93
- const followRe = /^(?:\/node_modules\/(?:@[^\/]+\/[^\/]+|[^\/]+)\/)*\/node_modules(?:\/@[^\/]+)?$/
90
+ const followRe = /^(?:\/node_modules\/(?:@[^/]+\/[^/]+|[^/]+)\/)*\/node_modules(?:\/@[^/]+)?$/
94
91
  const rootPath = opt.parent ? opt.parent.root : opt.path
95
92
  const followTestPath = opt.path.replace(/\\/g, '/').substr(rootPath.length)
96
93
  opt.follow = followRe.test(followTestPath)
@@ -100,14 +97,15 @@ const npmWalker = Class => class Walker extends Class {
100
97
  // ignore a bunch of things by default at the root level.
101
98
  // also ignore anything in the main project node_modules hierarchy,
102
99
  // except bundled dependencies
103
- if (!this.parent) {
100
+ if (this.isProject) {
104
101
  this.bundled = opt.bundled || []
105
102
  this.bundledScopes = Array.from(new Set(
106
103
  this.bundled.filter(f => /^@/.test(f))
107
- .map(f => f.split('/')[0])))
104
+ .map(f => f.split('/')[0])))
108
105
  const rules = defaultRules.join('\n') + '\n'
109
- this.packageJsonCache = opt.packageJsonCache || new Map()
110
- super.onReadIgnoreFile(rootBuiltinRules, rules, _=>_)
106
+ this.packageJsonCache = this.parent ? this.parent.packageJsonCache
107
+ : (opt.packageJsonCache || new Map())
108
+ super.onReadIgnoreFile(rootBuiltinRules, rules, _ => _)
111
109
  } else {
112
110
  this.bundled = []
113
111
  this.bundledScopes = []
@@ -115,8 +113,12 @@ const npmWalker = Class => class Walker extends Class {
115
113
  }
116
114
  }
117
115
 
116
+ get isProject () {
117
+ return !this.parent || this.parent.follow && this.isSymbolicLink
118
+ }
119
+
118
120
  onReaddir (entries) {
119
- if (!this.parent) {
121
+ if (this.isProject) {
120
122
  entries = entries.filter(e =>
121
123
  e !== '.git' &&
122
124
  !(e === 'node_modules' && this.bundled.length === 0)
@@ -129,7 +131,7 @@ const npmWalker = Class => class Walker extends Class {
129
131
  // to be in the state the user wants to include them, and
130
132
  // a package.json somewhere else might be a template or
131
133
  // test or something else entirely.
132
- if (this.parent || !entries.includes('package.json')) {
134
+ if (!this.isProject || !entries.includes('package.json')) {
133
135
  return super.onReaddir(entries)
134
136
  }
135
137
 
@@ -154,28 +156,32 @@ const npmWalker = Class => class Walker extends Class {
154
156
  }
155
157
 
156
158
  onReadPackageJson (entries, er, pkg) {
157
- if (er)
159
+ if (er) {
158
160
  this.emit('error', er)
159
- else
161
+ } else {
160
162
  this.getPackageFiles(entries, pkg)
163
+ }
161
164
  }
162
165
 
163
166
  mustHaveFilesFromPackage (pkg) {
164
167
  const files = []
165
- if (pkg.browser)
168
+ if (pkg.browser) {
166
169
  files.push('/' + pkg.browser)
167
- if (pkg.main)
170
+ }
171
+ if (pkg.main) {
168
172
  files.push('/' + pkg.main)
173
+ }
169
174
  if (pkg.bin) {
170
175
  // always an object because normalized already
171
- for (const key in pkg.bin)
176
+ for (const key in pkg.bin) {
172
177
  files.push('/' + pkg.bin[key])
178
+ }
173
179
  }
174
180
  files.push(
175
181
  '/package.json',
176
182
  '/npm-shrinkwrap.json',
177
183
  '!/package-lock.json',
178
- packageMustHaves,
184
+ packageMustHaves
179
185
  )
180
186
  return files
181
187
  }
@@ -199,8 +205,9 @@ const npmWalker = Class => class Walker extends Class {
199
205
  this.packageJsonCache.set(ig, pkg)
200
206
 
201
207
  // no files list, just return the normal readdir() result
202
- if (!Array.isArray(pkg.files))
208
+ if (!Array.isArray(pkg.files)) {
203
209
  return super.onReaddir(entries)
210
+ }
204
211
 
205
212
  pkg.files.push(...this.mustHaveFilesFromPackage(pkg))
206
213
 
@@ -209,13 +216,15 @@ const npmWalker = Class => class Walker extends Class {
209
216
  // the files list as the effective readdir result, that means it
210
217
  // looks like we don't have a node_modules folder at all unless we
211
218
  // include it here.
212
- if ((pkg.bundleDependencies || pkg.bundledDependencies) && entries.includes('node_modules'))
219
+ if ((pkg.bundleDependencies || pkg.bundledDependencies) && entries.includes('node_modules')) {
213
220
  pkg.files.push('node_modules')
221
+ }
214
222
 
215
223
  const patterns = Array.from(new Set(pkg.files)).reduce((set, pattern) => {
216
224
  const excl = pattern.match(/^!+/)
217
- if (excl)
225
+ if (excl) {
218
226
  pattern = pattern.substr(excl[0].length)
227
+ }
219
228
  // strip off any / from the start of the pattern. /foo => foo
220
229
  pattern = pattern.replace(/^\/+/, '')
221
230
  // an odd number of ! means a negated pattern. !!foo ==> foo
@@ -229,8 +238,9 @@ const npmWalker = Class => class Walker extends Class {
229
238
  const negates = new Set()
230
239
  const results = []
231
240
  const then = (pattern, negate, er, fileList, i) => {
232
- if (er)
241
+ if (er) {
233
242
  return this.emit('error', er)
243
+ }
234
244
 
235
245
  results[i] = { negate, fileList }
236
246
  if (--n === 0) {
@@ -273,17 +283,17 @@ const npmWalker = Class => class Walker extends Class {
273
283
  filterEntry (entry, partial) {
274
284
  // get the partial path from the root of the walk
275
285
  const p = this.path.substr(this.root.length + 1)
276
- const pkgre = /^node_modules\/(@[^\/]+\/?[^\/]+|[^\/]+)(\/.*)?$/
277
- const isRoot = !this.parent
278
- const pkg = isRoot && pkgre.test(entry) ?
286
+ const pkgre = /^node_modules\/(@[^/]+\/?[^/]+|[^/]+)(\/.*)?$/
287
+ const { isProject } = this
288
+ const pkg = isProject && pkgre.test(entry) ?
279
289
  entry.replace(pkgre, '$1') : null
280
- const rootNM = isRoot && entry === 'node_modules'
281
- const rootPJ = isRoot && entry === 'package.json'
290
+ const rootNM = isProject && entry === 'node_modules'
291
+ const rootPJ = isProject && entry === 'package.json'
282
292
 
283
293
  return (
284
294
  // if we're in a bundled package, check with the parent.
285
- /^node_modules($|\/)/i.test(p) ? this.parent.filterEntry(
286
- this.basename + '/' + entry, partial)
295
+ /^node_modules($|\/)/i.test(p) && !this.isProject ? this.parent.filterEntry(
296
+ this.basename + '/' + entry, partial)
287
297
 
288
298
  // if package is bundled, all files included
289
299
  // also include @scope dirs for bundled scoped deps
@@ -291,8 +301,8 @@ const npmWalker = Class => class Walker extends Class {
291
301
  // However, this only matters if we're in the root.
292
302
  // node_modules folders elsewhere, like lib/node_modules,
293
303
  // should be included normally unless ignored.
294
- : pkg ? -1 !== this.bundled.indexOf(pkg) ||
295
- -1 !== this.bundledScopes.indexOf(pkg)
304
+ : pkg ? this.bundled.indexOf(pkg) !== -1 ||
305
+ this.bundledScopes.indexOf(pkg) !== -1
296
306
 
297
307
  // only walk top node_modules if we want to bundle something
298
308
  : rootNM ? !!this.bundled.length
@@ -304,32 +314,34 @@ const npmWalker = Class => class Walker extends Class {
304
314
  : packageMustHavesRE.test(entry) ? true
305
315
 
306
316
  // npm-shrinkwrap and package.json always included in the root pkg
307
- : isRoot && (entry === 'npm-shrinkwrap.json' || entry === 'package.json')
317
+ : isProject && (entry === 'npm-shrinkwrap.json' || entry === 'package.json')
308
318
  ? true
309
319
 
310
320
  // package-lock never included
311
- : isRoot && entry === 'package-lock.json' ? false
321
+ : isProject && entry === 'package-lock.json' ? false
312
322
 
313
- // otherwise, follow ignore-walk's logic
314
- : super.filterEntry(entry, partial)
323
+ // otherwise, follow ignore-walk's logic
324
+ : super.filterEntry(entry, partial)
315
325
  )
316
326
  }
317
327
 
318
328
  filterEntries () {
319
- if (this.ignoreRules['.npmignore'])
329
+ if (this.ignoreRules['.npmignore']) {
320
330
  this.ignoreRules['.gitignore'] = null
331
+ }
321
332
  this.filterEntries = super.filterEntries
322
333
  super.filterEntries()
323
334
  }
324
335
 
325
336
  addIgnoreFile (file, then) {
326
337
  const ig = path.resolve(this.path, file)
327
- if (file === 'package.json' && this.parent)
338
+ if (file === 'package.json' && !this.isProject) {
328
339
  then()
329
- else if (this.packageJsonCache.has(ig))
340
+ } else if (this.packageJsonCache.has(ig)) {
330
341
  this.onPackageJson(ig, this.packageJsonCache.get(ig), then)
331
- else
342
+ } else {
332
343
  super.addIgnoreFile(file, then)
344
+ }
333
345
  }
334
346
 
335
347
  onPackageJson (ig, pkg, then) {
@@ -353,19 +365,22 @@ const npmWalker = Class => class Walker extends Class {
353
365
  // override parent stat function to completely skip any filenames
354
366
  // that will break windows entirely.
355
367
  // XXX(isaacs) Next major version should make this an error instead.
356
- stat (entry, file, dir, then) {
357
- if (nameIsBadForWindows(entry))
368
+ stat ({ entry, file, dir }, then) {
369
+ if (nameIsBadForWindows(entry)) {
358
370
  then()
359
- else
360
- super.stat(entry, file, dir, then)
371
+ } else {
372
+ super.stat({ entry, file, dir }, then)
373
+ }
361
374
  }
362
375
 
363
- // override parent onstat function to nix all symlinks
364
- onstat (st, entry, file, dir, then) {
365
- if (st.isSymbolicLink())
376
+ // override parent onstat function to nix all symlinks, other than
377
+ // those coming out of the followed bundled symlink deps
378
+ onstat ({ st, entry, file, dir, isSymbolicLink }, then) {
379
+ if (st.isSymbolicLink()) {
366
380
  then()
367
- else
368
- super.onstat(st, entry, file, dir, then)
381
+ } else {
382
+ super.onstat({ st, entry, file, dir, isSymbolicLink }, then)
383
+ }
369
384
  }
370
385
 
371
386
  onReadIgnoreFile (file, data, then) {
@@ -377,8 +392,9 @@ const npmWalker = Class => class Walker extends Class {
377
392
  // ignore package.json files that are not json
378
393
  then()
379
394
  }
380
- } else
395
+ } else {
381
396
  super.onReadIgnoreFile(file, data, then)
397
+ }
382
398
  }
383
399
 
384
400
  sort (a, b) {
@@ -396,8 +412,8 @@ class Walker extends npmWalker(IgnoreWalker) {
396
412
  this.onReadPackageJson(entries, er, pkg))
397
413
  }
398
414
 
399
- walker (entry, then) {
400
- new Walker(this.walkerOpt(entry)).on('done', then).start()
415
+ walker (entry, opt, then) {
416
+ new Walker(this.walkerOpt(entry, opt)).on('done', then).start()
401
417
  }
402
418
  }
403
419
 
@@ -415,8 +431,8 @@ class WalkerSync extends npmWalker(IgnoreWalkerSync) {
415
431
  }
416
432
  }
417
433
 
418
- walker (entry, then) {
419
- new WalkerSync(this.walkerOpt(entry)).start()
434
+ walker (entry, opt, then) {
435
+ new WalkerSync(this.walkerOpt(entry, opt)).start()
420
436
  then()
421
437
  }
422
438
  }
@@ -454,12 +470,11 @@ const sort = (a, b) => {
454
470
  const basea = path.basename(a).toLowerCase()
455
471
  const baseb = path.basename(b).toLowerCase()
456
472
 
457
- return exta.localeCompare(extb) ||
458
- basea.localeCompare(baseb) ||
459
- a.localeCompare(b)
473
+ return exta.localeCompare(extb, 'en') ||
474
+ basea.localeCompare(baseb, 'en') ||
475
+ a.localeCompare(b, 'en')
460
476
  }
461
477
 
462
-
463
478
  module.exports = walk
464
479
  walk.sync = walkSync
465
480
  walk.Walker = Walker
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "npm-packlist",
3
- "version": "2.1.5",
3
+ "version": "3.0.0",
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"
@@ -8,32 +8,42 @@
8
8
  "main": "index.js",
9
9
  "dependencies": {
10
10
  "glob": "^7.1.6",
11
- "ignore-walk": "^3.0.3",
11
+ "ignore-walk": "^4.0.1",
12
12
  "npm-bundled": "^1.1.1",
13
13
  "npm-normalize-package-bin": "^1.0.1"
14
14
  },
15
15
  "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
16
16
  "license": "ISC",
17
17
  "files": [
18
+ "bin/index.js",
18
19
  "index.js"
19
20
  ],
20
21
  "devDependencies": {
22
+ "@npmcli/lint": "^1.0.2",
21
23
  "mutate-fs": "^2.1.1",
22
- "require-inject": "^1.4.4",
23
- "tap": "^14.10.8"
24
+ "tap": "^15.0.6"
24
25
  },
25
26
  "scripts": {
26
27
  "test": "tap",
28
+ "posttest": "npm run lint --",
27
29
  "snap": "tap",
30
+ "postsnap": "npm run lintfix --",
28
31
  "preversion": "npm test",
29
32
  "postversion": "npm publish",
30
- "prepublishOnly": "git push origin --follow-tags"
33
+ "prepublishOnly": "git push origin --follow-tags",
34
+ "eslint": "eslint",
35
+ "lint": "npm run npmclilint -- \"*.*js\" \"test/**/*.*js\"",
36
+ "lintfix": "npm run lint -- --fix",
37
+ "npmclilint": "npmcli-lint"
31
38
  },
32
39
  "repository": {
33
40
  "type": "git",
34
41
  "url": "git+https://github.com/npm/npm-packlist.git"
35
42
  },
36
43
  "tap": {
44
+ "test-env": [
45
+ "LC_ALL=sk"
46
+ ],
37
47
  "check-coverage": true,
38
48
  "nyc-arg": [
39
49
  "--include=index.js",