Skip to content

Commit

Permalink
feat: require arborist constructor to be passed in for preparing git …
Browse files Browse the repository at this point in the history
…dirs (#204)

* feat: require arborist constructor to be passed in for preparing git dirs

This reverts the peer dependency change from d3517fd.

BREAKING CHANGE: a `@npmcli/arborist` constructor must be passed in if
no tree is provided and pacote is going to operate on git dependencies.
  • Loading branch information
lukekarrys committed Sep 28, 2022
1 parent 7e7a1e2 commit d6ef5dc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/dir.js
Expand Up @@ -3,7 +3,6 @@ const FileFetcher = require('./file.js')
const Minipass = require('minipass')
const tarCreateOptions = require('./util/tar-create-options.js')
const packlist = require('npm-packlist')
const Arborist = require('@npmcli/arborist')
const tar = require('tar')
const _prepareDir = Symbol('_prepareDir')
const { resolve } = require('path')
Expand All @@ -17,6 +16,9 @@ class DirFetcher extends Fetcher {
super(spec, opts)
// just the fully resolved filename
this.resolved = this.spec.fetchSpec

this.tree = opts.tree || null
this.Arborist = opts.Arborist || null
}

// exposes tarCreateOptions as public API
Expand Down Expand Up @@ -60,6 +62,10 @@ class DirFetcher extends Fetcher {
}

[_tarballFromResolved] () {
if (!this.tree && !this.Arborist) {
throw new Error('DirFetcher requires either a tree or an Arborist constructor to pack')
}

const stream = new Minipass()
stream.resolved = this.resolved
stream.integrity = this.integrity
Expand All @@ -71,7 +77,7 @@ class DirFetcher extends Fetcher {
this[_prepareDir]()
.then(async () => {
if (!this.tree) {
const arb = new Arborist({ path: this.resolved })
const arb = new this.Arborist({ path: this.resolved })
this.tree = await arb.loadActual()
}
return packlist(this.tree, { path: this.resolved, prefix, workspaces })
Expand Down
1 change: 0 additions & 1 deletion lib/fetcher.js
Expand Up @@ -72,7 +72,6 @@ class FetcherBase {

this.cache = opts.cache || cacheDir()
this.resolved = opts.resolved || null
this.tree = opts.tree || null

// default to caching/verifying with sha512, that's what we usually have
// need to change this default, or start overriding it, when sha512
Expand Down
6 changes: 6 additions & 0 deletions lib/git.js
Expand Up @@ -61,6 +61,8 @@ class GitFetcher extends Fetcher {
} else {
this.resolvedSha = ''
}

this.Arborist = opts.Arborist || null
}

// just exposed to make it easier to test all the combinations
Expand Down Expand Up @@ -206,8 +208,12 @@ class GitFetcher extends Fetcher {
// check it out and then shell out to the DirFetcher tarball packer
this[_clone](dir => this[_prepareDir](dir)
.then(() => new Promise((res, rej) => {
if (!this.Arborist) {
throw new Error('GitFetcher requires an Arborist constructor to pack a tarball')
}
const df = new DirFetcher(`file:${dir}`, {
...this.opts,
Arborist: this.Arborist,
resolved: null,
integrity: null,
})
Expand Down
3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -66,9 +66,6 @@
"ssri": "^9.0.0",
"tar": "^6.1.11"
},
"peerDependencies": {
"@npmcli/arborist": "^6.0.0 || ^6.0.0-pre.0"
},
"engines": {
"node": "^14.17.0 || ^16.13.0 || >=18.0.0"
},
Expand Down
5 changes: 5 additions & 0 deletions test/dir.js
Expand Up @@ -160,3 +160,8 @@ t.test('exposes tarCreateOptions method', async t => {
'should return standard options'
)
})

t.test('fails without a tree or constructor', async t => {
const f = new DirFetcher(abbrevspec, {})
t.rejects(() => f.extract(me + '/prepare'))
})
1 change: 1 addition & 0 deletions test/fixtures/npm-mock.js
Expand Up @@ -22,6 +22,7 @@ for (const [name, spec] of Object.entries(pkg.dependencies)) {
pacote.extract(spec, process.cwd() + '/' + name, {
npmBin: __filename,
...JSON.parse(pacoteOpts),
Arborist: require('@npmcli/arborist'),
})
}

Expand Down
8 changes: 7 additions & 1 deletion test/git.js
Expand Up @@ -78,7 +78,7 @@ const cycleB = resolve(me, 'cycle-b')

const abbrevSpec = `file:${abbrev}`

const opts = { cache }
const opts = { cache, Arborist: require('@npmcli/arborist') }

const spawnGit = require('@npmcli/git').spawn
const { spawn } = require('child_process')
Expand Down Expand Up @@ -755,3 +755,9 @@ t.test('simple repo with only a prepack script', async t => {
'should run prepack lifecycle script'
)
})

t.test('fails without arborist constructor', async t => {
const ws = new GitFetcher(prepackRemote, { cache })
const extract = resolve(me, 'extract-prepack')
t.rejects(() => ws.extract(extract))
})

0 comments on commit d6ef5dc

Please sign in to comment.