Skip to content

Commit 87c778e

Browse files
committed
feat: make the required tree the first parameter
This also uses `tree.path` as the fallback instead of `process.cwd()`. Arborist defaults to `cwd` as well so there is no functional difference. This also refactors away some dead code now that tree is required. BREAKING CHANGE: `tree` is now the first parameter
1 parent 123875a commit 87c778e

49 files changed

Lines changed: 89 additions & 158 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const packageTarball = '/path/to/package.tgz'
1414

1515
const arborist = new Arborist({ path: packageDir })
1616
arborist.loadActual().then((tree) => {
17-
packlist({ path: packageDir, tree })
17+
packlist(tree)
1818
.then(files => tar.create({
1919
prefix: 'package/',
2020
cwd: packageDir,

lib/index.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,26 @@ const readOutOfTreeIgnoreFiles = (root, rel, result = []) => {
8080
}
8181

8282
class PackWalker extends IgnoreWalker {
83-
constructor (opts) {
83+
constructor (tree, opts) {
8484
const options = {
8585
...opts,
8686
includeEmpty: false,
8787
follow: false,
88-
}
89-
90-
// we path.resolve() here because ignore-walk doesn't do it and we want full paths
91-
options.path = resolve(options.path || process.cwd()).replace(/\\/g, '/')
92-
93-
if (!options.ignoreFiles) {
94-
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 || [
9591
defaultRules,
9692
'package.json',
9793
'.npmignore',
9894
'.gitignore',
9995
strictRules,
100-
]
96+
],
10197
}
10298

10399
super(options)
104100
this.isPackage = options.isPackage
105101
this.seen = options.seen || new Set()
106-
this.tree = options.tree
102+
this.tree = tree
107103
this.requiredFiles = options.requiredFiles || []
108104

109105
const additionalDefaults = []
@@ -206,16 +202,11 @@ class PackWalker extends IgnoreWalker {
206202
// copies the root's `ignoreFiles` value, but we don't want to respect package.json for
207203
// subdirectories, so we override it with a list that intentionally omits package.json
208204
walkerOpt (entry, opts) {
209-
let ignoreFiles = [
210-
defaultRules,
211-
'.npmignore',
212-
'.gitignore',
213-
strictRules,
214-
]
205+
let ignoreFiles = null
215206

216207
// however, if we have a tree, and we have workspaces, and the directory we're about
217208
// to step into is a workspace, then we _do_ want to respect its package.json
218-
if (this.tree && this.tree.workspaces) {
209+
if (this.tree.workspaces) {
219210
const workspaceDirs = [...this.tree.workspaces.values()]
220211
.map((dir) => dir.replace(/\\/g, '/'))
221212

@@ -229,6 +220,13 @@ class PackWalker extends IgnoreWalker {
229220
strictRules,
230221
]
231222
}
223+
} else {
224+
ignoreFiles = [
225+
defaultRules,
226+
'.npmignore',
227+
'.gitignore',
228+
strictRules,
229+
]
232230
}
233231

234232
return {
@@ -248,7 +246,7 @@ class PackWalker extends IgnoreWalker {
248246

249247
// overridden method: we want child walkers to be instances of this class, not ignore-walk
250248
walker (entry, opts, callback) {
251-
new PackWalker(this.walkerOpt(entry, opts)).on('done', callback).start()
249+
new PackWalker(this.tree, this.walkerOpt(entry, opts)).on('done', callback).start()
252250
}
253251

254252
// overridden method: we use a custom sort method to help compressibility
@@ -351,7 +349,7 @@ class PackWalker extends IgnoreWalker {
351349
// custom method: after we've finished gathering the files for the root package, we call this
352350
// before emitting the 'done' event in order to gather all of the files for bundled deps
353351
async gatherBundles () {
354-
if (this.tree && this.seen.has(this.tree)) {
352+
if (this.seen.has(this.tree)) {
355353
return
356354
}
357355

@@ -380,14 +378,16 @@ class PackWalker extends IgnoreWalker {
380378

381379
// get a reference to the node we're bundling
382380
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+
383388
// and start building options to be passed to the walker for this package
384389
const walkerOpts = {
385-
// we use node.path for the path because we want the location the node was linked to,
386-
// not where it actually lives on disk
387-
path: node.path,
388-
// but link nodes don't have edgesOut, so we need to pass in the target of the node
389-
// in order to make sure we correctly traverse its dependencies
390-
tree: node.target,
390+
path,
391391
isPackage: true,
392392
ignoreFiles: [],
393393
seen: this.seen, // pass through seen so we can prevent infinite circular loops
@@ -413,7 +413,7 @@ class PackWalker extends IgnoreWalker {
413413
walkerOpts.ignoreFiles.push(strictRules)
414414

415415
// create a walker for this dependency and gather its results
416-
const walker = new PackWalker(walkerOpts)
416+
const walker = new PackWalker(tree, walkerOpts)
417417
const bundled = await new Promise((pResolve, pReject) => {
418418
walker.on('error', pReject)
419419
walker.on('done', pResolve)
@@ -430,10 +430,14 @@ class PackWalker extends IgnoreWalker {
430430
}
431431
}
432432

433-
const walk = (options, callback) => {
434-
options = { ...options, isPackage: true }
433+
const walk = (tree, options, callback) => {
434+
if (typeof options === 'function') {
435+
callback = options
436+
options = {}
437+
}
435438
const p = new Promise((pResolve, pReject) => {
436-
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()
437441
})
438442
return callback ? p.then(res => callback(null, res), callback) : p
439443
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"lib/"
1717
],
1818
"devDependencies": {
19-
"@npmcli/arborist": "^5.0.4 || ^6.0.0 || ^6.0.0-pre.0",
19+
"@npmcli/arborist": "^6.0.0 || ^6.0.0-pre.0",
2020
"@npmcli/eslint-config": "^3.0.1",
2121
"@npmcli/template-oss": "4.4.2",
2222
"mutate-fs": "^2.1.1",

test/bundle-missing-dep.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ t.test('skips bundling deps with missing edges', async (t) => {
2828
const arborist = new Arborist({ path: pkg })
2929
const tree = await arborist.loadActual()
3030

31-
const files = await packlist({ path: pkg, tree })
31+
const files = await packlist(tree)
3232
t.same(files, [
3333
'index.js',
3434
'package.json',

test/bundled-cycle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ t.test('correctly bundles cyclic deps', async (t) => {
4444

4545
const arborist = new Arborist({ path: pkg })
4646
const tree = await arborist.loadActual()
47-
const files = await packlist({ path: pkg, tree })
47+
const files = await packlist(tree)
4848
t.same(files, [
4949
'index.js',
5050
'node_modules/a/index.js',

test/bundled-file-in-workspace.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ t.test('correctly filters files from workspace subdirectory', async (t) => {
3232

3333
const arborist = new Arborist({ path: pkg })
3434
const tree = await arborist.loadActual()
35-
const files = await packlist({ path: pkg, tree })
35+
const files = await packlist(tree)
3636
t.same(files, [
3737
'index.js',
3838
'package.json',
@@ -81,7 +81,7 @@ t.test('does not filter based on package.json if subdirectory is not a workspace
8181

8282
const arborist = new Arborist({ path: pkg })
8383
const tree = await arborist.loadActual()
84-
const files = await packlist({ path: pkg, tree })
84+
const files = await packlist(tree)
8585
t.same(files, [
8686
'index.js',
8787
'package.json',

test/bundled-files.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ t.test('includes bundled dependency using bundleDependencies', async (t) => {
3939

4040
const arborist = new Arborist({ path: pkg })
4141
const tree = await arborist.loadActual()
42-
const files = await packlist({ path: pkg, tree })
42+
const files = await packlist(tree)
4343
t.same(files, [
4444
'elf.js',
4545
'node_modules/history/index.js',
@@ -78,7 +78,7 @@ t.test('includes bundled dependency using bundledDependencies', async (t) => {
7878

7979
const arborist = new Arborist({ path: pkg })
8080
const tree = await arborist.loadActual()
81-
const files = await packlist({ path: pkg, tree })
81+
const files = await packlist(tree)
8282
t.same(files, [
8383
'elf.js',
8484
'node_modules/history/index.js',

test/bundled-scoped-symlink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const pkg = t.testdir({
5454
t.test('includes bundled dependency', async (t) => {
5555
const arborist = new Arborist({ path: pkg })
5656
const tree = await arborist.loadActual()
57-
const files = await packlist({ path: pkg, tree })
57+
const files = await packlist(tree)
5858
t.same(files, [
5959
'elf.js',
6060
'node_modules/@npmwombat/history/index.js',

test/bundled-scoped.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const pkg = t.testdir({
4040
t.test('includes bundled dependency', async (t) => {
4141
const arborist = new Arborist({ path: pkg })
4242
const tree = await arborist.loadActual()
43-
const files = await packlist({ path: pkg, tree })
43+
const files = await packlist(tree)
4444
t.same(files, [
4545
'elf.js',
4646
'node_modules/@npmwombat/history/index.js',

test/bundled-symlink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const pkg = t.testdir({
5252
t.test('includes bundled dependency', async (t) => {
5353
const arborist = new Arborist({ path: pkg })
5454
const tree = await arborist.loadActual()
55-
const files = await packlist({ path: pkg, tree })
55+
const files = await packlist(tree)
5656
t.same(files, [
5757
'elf.js',
5858
'node_modules/history/index.js',

0 commit comments

Comments
 (0)