Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lazy loading of arborist and pacote #6225

Merged
merged 6 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/commands/audit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Arborist = require('@npmcli/arborist')
const auditReport = require('npm-audit-report')
const fetch = require('npm-registry-fetch')
const localeCompare = require('@isaacs/string-locale-compare')('en')
Expand Down Expand Up @@ -413,6 +412,7 @@ class Audit extends ArboristWorkspaceCmd {

async auditAdvisories (args) {
const reporter = this.npm.config.get('json') ? 'json' : 'detail'
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
audit: true,
Expand Down Expand Up @@ -445,6 +445,7 @@ class Audit extends ArboristWorkspaceCmd {
}

log.verbose('loading installed dependencies')
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: this.npm.prefix,
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/cache.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const cacache = require('cacache')
const Arborist = require('@npmcli/arborist')
const pacote = require('pacote')
const fs = require('fs/promises')
const { join } = require('path')
Expand Down Expand Up @@ -162,7 +161,7 @@ class Cache extends BaseCommand {
return pacote.tarball.stream(spec, stream => {
stream.resume()
return stream.promise()
}, { ...this.npm.flatOptions, Arborist })
}, { ...this.npm.flatOptions })
}))
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/ci.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Arborist = require('@npmcli/arborist')
const reifyFinish = require('../utils/reify-finish.js')
const runScript = require('@npmcli/run-script')
const fs = require('fs/promises')
Expand All @@ -22,6 +21,7 @@ class CI extends ArboristWorkspaceCmd {
}

const where = this.npm.prefix
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
packageLock: true, // npm ci should never skip lock files
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/dedupe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// dedupe duplicated packages, or find them in the tree
const Arborist = require('@npmcli/arborist')
const reifyFinish = require('../utils/reify-finish.js')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
Expand Down Expand Up @@ -31,6 +30,7 @@ class Dedupe extends ArboristWorkspaceCmd {

const dryRun = this.npm.config.get('dry-run')
const where = this.npm.prefix
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: where,
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const { resolve } = require('path')
const semver = require('semver')
const libnpmdiff = require('libnpmdiff')
const npa = require('npm-package-arg')
const Arborist = require('@npmcli/arborist')
const pacote = require('pacote')
const pickManifest = require('npm-pick-manifest')
const log = require('../utils/log-shim')
Expand Down Expand Up @@ -146,6 +145,7 @@ class Diff extends BaseCommand {
if (spec.registry) {
let actualTree
let node
const Arborist = require('@npmcli/arborist')
try {
const opts = {
...this.npm.flatOptions,
Expand Down Expand Up @@ -257,6 +257,7 @@ class Diff extends BaseCommand {

async findVersionsByPackageName (specs) {
let actualTree
const Arborist = require('@npmcli/arborist')
try {
const opts = {
...this.npm.flatOptions,
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/explain.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { explainNode } = require('../utils/explain-dep.js')
const completion = require('../utils/completion/installed-deep.js')
const Arborist = require('@npmcli/arborist')
const npa = require('npm-package-arg')
const semver = require('semver')
const { relative, resolve } = require('path')
Expand All @@ -21,6 +19,7 @@ class Explain extends ArboristWorkspaceCmd {
// TODO
/* istanbul ignore next */
async completion (opts) {
const completion = require('../utils/completion/installed-deep.js')
return completion(this.npm, opts)
}

Expand All @@ -29,6 +28,7 @@ class Explain extends ArboristWorkspaceCmd {
throw this.usageError()
}

const Arborist = require('@npmcli/arborist')
const arb = new Arborist({ path: this.npm.prefix, ...this.npm.flatOptions })
const tree = await arb.loadActual()

Expand Down
5 changes: 3 additions & 2 deletions lib/commands/fund.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const archy = require('archy')
const Arborist = require('@npmcli/arborist')
const pacote = require('pacote')
const semver = require('semver')
const npa = require('npm-package-arg')
const { depth } = require('treeverse')
const { readTree: getFundingInfo, normalizeFunding, isValidFunding } = require('libnpmfund')

const completion = require('../utils/completion/installed-deep.js')
const openUrl = require('../utils/open-url.js')
const ArboristWorkspaceCmd = require('../arborist-cmd.js')

Expand Down Expand Up @@ -39,6 +37,7 @@ class Fund extends ArboristWorkspaceCmd {
// TODO
/* istanbul ignore next */
async completion (opts) {
const completion = require('../utils/completion/installed-deep.js')
return completion(this.npm, opts)
}

Expand All @@ -64,6 +63,7 @@ class Fund extends ArboristWorkspaceCmd {
}

const where = this.npm.prefix
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({ ...this.npm.flatOptions, path: where })
const tree = await arb.loadActual()

Expand All @@ -80,6 +80,7 @@ class Fund extends ArboristWorkspaceCmd {
// TODO: add !workspacesEnabled option handling to libnpmfund
const fundingInfo = getFundingInfo(tree, {
...this.flatOptions,
Arborist,
workspaces: this.workspaceNames,
})

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const readdir = util.promisify(fs.readdir)
const reifyFinish = require('../utils/reify-finish.js')
const log = require('../utils/log-shim.js')
const { resolve, join } = require('path')
const Arborist = require('@npmcli/arborist')
const runScript = require('@npmcli/run-script')
const pacote = require('pacote')
const checks = require('npm-install-checks')
Expand Down Expand Up @@ -135,6 +134,7 @@ class Install extends ArboristWorkspaceCmd {
throw this.usageError()
}

const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
auditLevel: null,
Expand Down
5 changes: 4 additions & 1 deletion lib/commands/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const util = require('util')
const readdir = util.promisify(fs.readdir)
const { resolve } = require('path')

const Arborist = require('@npmcli/arborist')
const npa = require('npm-package-arg')
const rpj = require('read-package-json-fast')
const semver = require('semver')
Expand Down Expand Up @@ -67,8 +66,10 @@ class Link extends ArboristWorkspaceCmd {
// load current packages from the global space,
// and then add symlinks installs locally
const globalTop = resolve(this.npm.globalDir, '..')
const Arborist = require('@npmcli/arborist')
const globalOpts = {
...this.npm.flatOptions,
Arborist,
path: globalTop,
global: true,
prune: false,
Expand Down Expand Up @@ -138,8 +139,10 @@ class Link extends ArboristWorkspaceCmd {
const paths = wsp && wsp.length ? wsp : [this.npm.prefix]
const add = paths.map(path => `file:${path.replace(/#/g, '%23')}`)
const globalTop = resolve(this.npm.globalDir, '..')
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({
...this.npm.flatOptions,
Arborist,
path: globalTop,
global: true,
})
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ const relativePrefix = `.${sep}`
const { EOL } = require('os')

const archy = require('archy')
const Arborist = require('@npmcli/arborist')
const { breadth } = require('treeverse')
const npa = require('npm-package-arg')

const completion = require('../utils/completion/installed-deep.js')

const _depth = Symbol('depth')
const _dedupe = Symbol('dedupe')
const _filteredBy = Symbol('filteredBy')
Expand Down Expand Up @@ -44,6 +41,7 @@ class LS extends ArboristWorkspaceCmd {
// TODO
/* istanbul ignore next */
async completion (opts) {
const completion = require('../utils/completion/installed-deep.js')
return completion(this.npm, opts)
}

Expand All @@ -63,6 +61,8 @@ class LS extends ArboristWorkspaceCmd {

const path = global ? resolve(this.npm.globalDir, '..') : this.npm.prefix

const Arborist = require('@npmcli/arborist')

const arb = new Arborist({
global,
...this.npm.flatOptions,
Expand Down
3 changes: 1 addition & 2 deletions lib/commands/outdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ const npa = require('npm-package-arg')
const pickManifest = require('npm-pick-manifest')
const localeCompare = require('@isaacs/string-locale-compare')('en')

const Arborist = require('@npmcli/arborist')

const ansiTrim = require('../utils/ansi-trim.js')
const ArboristWorkspaceCmd = require('../arborist-cmd.js')

Expand All @@ -30,6 +28,7 @@ class Outdated extends ArboristWorkspaceCmd {
? global
: this.npm.prefix

const Arborist = require('@npmcli/arborist')
const arb = new Arborist({
...this.npm.flatOptions,
path: where,
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/prune.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// prune extraneous packages
const Arborist = require('@npmcli/arborist')
const reifyFinish = require('../utils/reify-finish.js')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
Expand All @@ -19,6 +18,7 @@ class Prune extends ArboristWorkspaceCmd {

async exec () {
const where = this.npm.prefix
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: where,
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const BaseCommand = require('../base-command.js')

class QuerySelectorItem {
Expand Down Expand Up @@ -58,6 +57,7 @@ class Query extends BaseCommand {
async exec (args) {
// one dir up from wherever node_modules lives
const where = resolve(this.npm.dir, '..')
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: where,
Expand All @@ -73,6 +73,7 @@ class Query extends BaseCommand {

async execWorkspaces (args) {
await this.setWorkspaces()
const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: this.npm.prefix,
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/rebuild.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const npa = require('npm-package-arg')
const semver = require('semver')
const completion = require('../utils/completion/installed-deep.js')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
class Rebuild extends ArboristWorkspaceCmd {
Expand All @@ -21,12 +19,14 @@ class Rebuild extends ArboristWorkspaceCmd {
// TODO
/* istanbul ignore next */
async completion (opts) {
const completion = require('../utils/completion/installed-deep.js')
return completion(this.npm, opts)
}

async exec (args) {
const globalTop = resolve(this.npm.globalDir, '..')
const where = this.npm.global ? globalTop : this.npm.prefix
const Arborist = require('@npmcli/arborist')
const arb = new Arborist({
...this.npm.flatOptions,
path: where,
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/shrinkwrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { resolve, basename } = require('path')
const { unlink } = require('fs').promises
const Arborist = require('@npmcli/arborist')
const log = require('../utils/log-shim')
const BaseCommand = require('../base-command.js')
class Shrinkwrap extends BaseCommand {
Expand All @@ -21,6 +20,7 @@ class Shrinkwrap extends BaseCommand {
throw er
}

const Arborist = require('@npmcli/arborist')
const path = this.npm.prefix
const sw = resolve(path, 'npm-shrinkwrap.json')
const arb = new Arborist({ ...this.npm.flatOptions, path })
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/uninstall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const rpj = require('read-package-json-fast')

const reifyFinish = require('../utils/reify-finish.js')
Expand Down Expand Up @@ -42,6 +41,7 @@ class Uninstall extends ArboristWorkspaceCmd {
? resolve(this.npm.globalDir, '..')
: this.npm.localPrefix

const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path,
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/update.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const path = require('path')

const Arborist = require('@npmcli/arborist')
const log = require('../utils/log-shim.js')

const reifyFinish = require('../utils/reify-finish.js')
const completion = require('../utils/completion/installed-deep.js')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
class Update extends ArboristWorkspaceCmd {
Expand Down Expand Up @@ -34,6 +32,7 @@ class Update extends ArboristWorkspaceCmd {
// TODO
/* istanbul ignore next */
async completion (opts) {
const completion = require('../utils/completion/installed-deep.js')
return completion(this.npm, opts)
}

Expand All @@ -53,6 +52,7 @@ class Update extends ArboristWorkspaceCmd {
'https://github.com/npm/rfcs/blob/latest/implemented/0019-remove-update-depth-option.md')
}

const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
path: where,
Expand Down
5 changes: 0 additions & 5 deletions lib/npm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Arborist = require('@npmcli/arborist')
const EventEmitter = require('events')
const { resolve, dirname, join } = require('path')
const Config = require('@npmcli/config')
Expand Down Expand Up @@ -310,10 +309,6 @@ class Npm extends EventEmitter {

get flatOptions () {
const { flat } = this.config
// the Arborist constructor is used almost everywhere we call pacote, it's
// easiest to attach it to flatOptions so it goes everywhere without having
// to touch every call
flat.Arborist = Arborist
flat.nodeVersion = process.version
flat.npmVersion = pkg.version
if (this.command) {
Expand Down
2 changes: 0 additions & 2 deletions lib/package-url-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const pacote = require('pacote')
const hostedGitInfo = require('hosted-git-info')
const Arborist = require('@npmcli/arborist')

const openUrl = require('./utils/open-url.js')
const log = require('./utils/log-shim')
Expand Down Expand Up @@ -33,7 +32,6 @@ class PackageUrlCommand extends BaseCommand {
...this.npm.flatOptions,
where: this.npm.localPrefix,
fullMetadata: true,
Arborist,
}
const mani = await pacote.manifest(arg, opts)
const url = this.getUrl(arg, mani)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/completion/installed-deep.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { resolve } = require('path')
const Arborist = require('@npmcli/arborist')
const localeCompare = require('@isaacs/string-locale-compare')('en')

const installedDeep = async (npm) => {
const Arborist = require('@npmcli/arborist')
const {
depth,
global,
Expand Down
Loading