npm-packlist 6.0.0 → 7.0.0-pre.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.
package/README.md CHANGED
@@ -6,20 +6,24 @@ These can be handed to [tar](http://npm.im/tar) like so to make an npm
6
6
  package tarball:
7
7
 
8
8
  ```js
9
+ const Arborist = require('@npmcli/arborist')
9
10
  const packlist = require('npm-packlist')
10
11
  const tar = require('tar')
11
12
  const packageDir = '/path/to/package'
12
13
  const packageTarball = '/path/to/package.tgz'
13
14
 
14
- packlist({ path: packageDir })
15
- .then(files => tar.create({
16
- prefix: 'package/',
17
- cwd: packageDir,
18
- file: packageTarball,
19
- gzip: true
20
- }, files))
21
- .then(_ => {
22
- // tarball has been created, continue with your day
15
+ const arborist = new Arborist({ path: packageDir })
16
+ arborist.loadActual().then((tree) => {
17
+ packlist(tree)
18
+ .then(files => tar.create({
19
+ prefix: 'package/',
20
+ cwd: packageDir,
21
+ file: packageTarball,
22
+ gzip: true
23
+ }, files))
24
+ .then(_ => {
25
+ // tarball has been created, continue with your day
26
+ })
23
27
  })
24
28
  ```
25
29
 
@@ -97,7 +101,6 @@ Any specific file matched by an exact filename in the package.json `files` list
97
101
 
98
102
  ## API
99
103
 
100
- Same API as [ignore-walk](http://npm.im/ignore-walk), just hard-coded
101
- file list and rule sets.
104
+ Same API as [ignore-walk](http://npm.im/ignore-walk), except providing a `tree` is required and there are hard-coded file list and rule sets.
102
105
 
103
- The `Walker` class will load an [arborist](https://github.com/npm/cli/tree/latest/workspaces/arborist) tree, and if any bundled dependencies are found will include them as well as their own dependencies in the resulting file set.
106
+ The `Walker` class requires an [arborist](https://github.com/npm/cli/tree/latest/workspaces/arborist) tree, and if any bundled dependencies are found will include them as well as their own dependencies in the resulting file set.
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  'use strict'
2
2
 
3
- const Arborist = require('@npmcli/arborist')
4
3
  const { Walker: IgnoreWalker } = require('ignore-walk')
5
4
  const { lstatSync: lstat, readFileSync: readFile } = require('fs')
6
5
  const { basename, dirname, extname, join, relative, resolve, sep } = require('path')
@@ -81,30 +80,26 @@ const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
81
80
  }
82
81
 
83
82
  class PackWalker extends IgnoreWalker {
84
- constructor (opts) {
83
+ constructor (tree, opts) {
85
84
  const options = {
86
85
  ...opts,
87
86
  includeEmpty: false,
88
87
  follow: false,
89
- }
90
-
91
- // we path.resolve() here because ignore-walk doesn't do it and we want full paths
92
- options.path = resolve(options.path || process.cwd()).replace(/\\/g, '/')
93
-
94
- if (!options.ignoreFiles) {
95
- options.ignoreFiles = [
88
+ // we path.resolve() here because ignore-walk doesn't do it and we want full paths
89
+ path: resolve(opts?.path || tree.path).replace(/\\/g, '/'),
90
+ ignoreFiles: opts?.ignoreFiles || [
96
91
  defaultRules,
97
92
  'package.json',
98
93
  '.npmignore',
99
94
  '.gitignore',
100
95
  strictRules,
101
- ]
96
+ ],
102
97
  }
103
98
 
104
99
  super(options)
105
100
  this.isPackage = options.isPackage
106
101
  this.seen = options.seen || new Set()
107
- this.tree = options.tree // no default, we'll load the tree later if we need to
102
+ this.tree = tree
108
103
  this.requiredFiles = options.requiredFiles || []
109
104
 
110
105
  const additionalDefaults = []
@@ -202,37 +197,16 @@ class PackWalker extends IgnoreWalker {
202
197
  return super.stat(opts, callback)
203
198
  }
204
199
 
205
- // overridden method: we need to load the arborist tree before we actually start running
206
- start () {
207
- if (this.isPackage && !this.tree) {
208
- const arborist = new Arborist({ path: this.path })
209
- // loading the tree is async while the start function depends on being sync
210
- // eslint-disable-next-line promise/catch-or-return, promise/always-return
211
- arborist.loadActual().then((tree) => {
212
- this.tree = tree
213
- super.start()
214
- })
215
- return this
216
- }
217
-
218
- return super.start()
219
- }
220
-
221
200
  // overridden method: this is called to create options for a child walker when we step
222
201
  // in to a normal child directory (this will never be a bundle). the default method here
223
202
  // copies the root's `ignoreFiles` value, but we don't want to respect package.json for
224
203
  // subdirectories, so we override it with a list that intentionally omits package.json
225
204
  walkerOpt (entry, opts) {
226
- let ignoreFiles = [
227
- defaultRules,
228
- '.npmignore',
229
- '.gitignore',
230
- strictRules,
231
- ]
205
+ let ignoreFiles = null
232
206
 
233
207
  // however, if we have a tree, and we have workspaces, and the directory we're about
234
208
  // to step into is a workspace, then we _do_ want to respect its package.json
235
- if (this.tree && this.tree.workspaces) {
209
+ if (this.tree.workspaces) {
236
210
  const workspaceDirs = [...this.tree.workspaces.values()]
237
211
  .map((dir) => dir.replace(/\\/g, '/'))
238
212
 
@@ -246,6 +220,13 @@ class PackWalker extends IgnoreWalker {
246
220
  strictRules,
247
221
  ]
248
222
  }
223
+ } else {
224
+ ignoreFiles = [
225
+ defaultRules,
226
+ '.npmignore',
227
+ '.gitignore',
228
+ strictRules,
229
+ ]
249
230
  }
250
231
 
251
232
  return {
@@ -265,7 +246,7 @@ class PackWalker extends IgnoreWalker {
265
246
 
266
247
  // overridden method: we want child walkers to be instances of this class, not ignore-walk
267
248
  walker (entry, opts, callback) {
268
- new PackWalker(this.walkerOpt(entry, opts)).on('done', callback).start()
249
+ new PackWalker(this.tree, this.walkerOpt(entry, opts)).on('done', callback).start()
269
250
  }
270
251
 
271
252
  // overridden method: we use a custom sort method to help compressibility
@@ -368,7 +349,7 @@ class PackWalker extends IgnoreWalker {
368
349
  // custom method: after we've finished gathering the files for the root package, we call this
369
350
  // before emitting the 'done' event in order to gather all of the files for bundled deps
370
351
  async gatherBundles () {
371
- if (this.tree && this.seen.has(this.tree)) {
352
+ if (this.seen.has(this.tree)) {
372
353
  return
373
354
  }
374
355
 
@@ -397,14 +378,16 @@ class PackWalker extends IgnoreWalker {
397
378
 
398
379
  // get a reference to the node we're bundling
399
380
  const node = this.tree.edgesOut.get(dep).to
381
+ // we use node.path for the path because we want the location the node was linked to,
382
+ // not where it actually lives on disk
383
+ const path = node.path
384
+ // but link nodes don't have edgesOut, so we need to pass in the target of the node
385
+ // in order to make sure we correctly traverse its dependencies
386
+ const tree = node.target
387
+
400
388
  // and start building options to be passed to the walker for this package
401
389
  const walkerOpts = {
402
- // we use node.path for the path because we want the location the node was linked to,
403
- // not where it actually lives on disk
404
- path: node.path,
405
- // but link nodes don't have edgesOut, so we need to pass in the target of the node
406
- // in order to make sure we correctly traverse its dependencies
407
- tree: node.target,
390
+ path,
408
391
  isPackage: true,
409
392
  ignoreFiles: [],
410
393
  seen: this.seen, // pass through seen so we can prevent infinite circular loops
@@ -430,7 +413,7 @@ class PackWalker extends IgnoreWalker {
430
413
  walkerOpts.ignoreFiles.push(strictRules)
431
414
 
432
415
  // create a walker for this dependency and gather its results
433
- const walker = new PackWalker(walkerOpts)
416
+ const walker = new PackWalker(tree, walkerOpts)
434
417
  const bundled = await new Promise((pResolve, pReject) => {
435
418
  walker.on('error', pReject)
436
419
  walker.on('done', pResolve)
@@ -447,10 +430,14 @@ class PackWalker extends IgnoreWalker {
447
430
  }
448
431
  }
449
432
 
450
- const walk = (options, callback) => {
451
- options = { ...options, isPackage: true }
433
+ const walk = (tree, options, callback) => {
434
+ if (typeof options === 'function') {
435
+ callback = options
436
+ options = {}
437
+ }
452
438
  const p = new Promise((pResolve, pReject) => {
453
- new PackWalker(options).on('done', pResolve).on('error', pReject).start()
439
+ new PackWalker(tree, { ...options, isPackage: true })
440
+ .on('done', pResolve).on('error', pReject).start()
454
441
  })
455
442
  return callback ? p.then(res => callback(null, res), callback) : p
456
443
  }
package/package.json CHANGED
@@ -1,13 +1,12 @@
1
1
  {
2
2
  "name": "npm-packlist",
3
- "version": "6.0.0",
3
+ "version": "7.0.0-pre.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"
7
7
  },
8
8
  "main": "lib/index.js",
9
9
  "dependencies": {
10
- "@npmcli/arborist": "^5.0.4",
11
10
  "ignore-walk": "^5.0.1"
12
11
  },
13
12
  "author": "GitHub Inc.",
@@ -17,8 +16,9 @@
17
16
  "lib/"
18
17
  ],
19
18
  "devDependencies": {
19
+ "@npmcli/arborist": "^6.0.0 || ^6.0.0-pre.0",
20
20
  "@npmcli/eslint-config": "^3.0.1",
21
- "@npmcli/template-oss": "4.3.2",
21
+ "@npmcli/template-oss": "4.4.2",
22
22
  "mutate-fs": "^2.1.1",
23
23
  "tap": "^16.0.1"
24
24
  },
@@ -45,16 +45,16 @@
45
45
  "nyc-arg": [
46
46
  "--exclude",
47
47
  "tap-snapshots/**"
48
+ ],
49
+ "files": [
50
+ "test/*.js"
48
51
  ]
49
52
  },
50
- "bin": {
51
- "npm-packlist": "bin/index.js"
52
- },
53
53
  "engines": {
54
54
  "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
55
55
  },
56
56
  "templateOSS": {
57
57
  "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
58
- "version": "4.3.2"
58
+ "version": "4.4.2"
59
59
  }
60
60
  }
package/bin/index.js DELETED
@@ -1,39 +0,0 @@
1
- #!/usr/bin/env node
2
- 'use strict'
3
-
4
- const packlist = require('../')
5
-
6
- const dirs = []
7
- let doSort = false
8
- process.argv.slice(2).forEach(arg => {
9
- if (arg === '-h' || arg === '--help') {
10
- console.log('usage: npm-packlist [-s --sort] [directory, directory, ...]')
11
- process.exit(0)
12
- } else if (arg === '-s' || arg === '--sort') {
13
- doSort = true
14
- } else {
15
- dirs.push(arg)
16
- }
17
- })
18
-
19
- const sort = list => doSort ? list.sort((a, b) => a.localeCompare(b, 'en')) : list
20
-
21
- const main = async () => {
22
- if (!dirs.length) {
23
- const results = await packlist({ path: process.cwd() })
24
- console.log(sort(results).join('\n'))
25
- } else {
26
- for (const dir of dirs) {
27
- console.group(`> ${dir}`)
28
- const results = await packlist({ path: dir })
29
- console.log(sort(results).join('\n'))
30
- console.groupEnd()
31
- }
32
- }
33
- }
34
-
35
- // coverage disabled for catch handler because we don't need to test that
36
- main().catch(/* istanbul ignore next */(err) => {
37
- process.exitCode = 1
38
- console.error(err.stack)
39
- })