([a, b])
| 91 | } |
| 92 | |
| 93 | async retrieveSpecs ([a, b]) { |
| 94 | if (a && b) { |
| 95 | const specs = await this.convertVersionsToSpecs([a, b]) |
| 96 | return this.findVersionsByPackageName(specs) |
| 97 | } |
| 98 | |
| 99 | // no arguments, defaults to comparing cwd to its latest published registry version |
| 100 | if (!a) { |
| 101 | const pkgName = await this.packageName() |
| 102 | return [ |
| 103 | `${pkgName}@${this.npm.config.get('tag')}`, |
| 104 | `file:${this.prefix}`, |
| 105 | ] |
| 106 | } |
| 107 | |
| 108 | // single argument, used to compare wanted versions of an installed dependency or to compare the cwd to a published version |
| 109 | let noPackageJson |
| 110 | let pkgName |
| 111 | try { |
| 112 | const { content: pkg } = await pkgJson.normalize(this.prefix) |
| 113 | pkgName = pkg.name |
| 114 | } catch { |
| 115 | log.verbose('diff', 'could not read project dir package.json') |
| 116 | noPackageJson = true |
| 117 | } |
| 118 | |
| 119 | const missingPackageJson = |
| 120 | this.usageError('Needs multiple arguments to compare or run from a project dir.') |
| 121 | |
| 122 | // using a valid semver range, that means it should just diff the cwd against a published version to the registry using the same project name and the provided semver range |
| 123 | if (semver.validRange(a)) { |
| 124 | if (!pkgName) { |
| 125 | throw missingPackageJson |
| 126 | } |
| 127 | return [ |
| 128 | `${pkgName}@${a}`, |
| 129 | `file:${this.prefix}`, |
| 130 | ] |
| 131 | } |
| 132 | |
| 133 | // when using a single package name as arg and it's part of the current install tree, then retrieve the current installed version and compare it against the same value `npm outdated` would suggest you to update to |
| 134 | const spec = npa(a) |
| 135 | if (spec.registry) { |
| 136 | let actualTree |
| 137 | let node |
| 138 | const Arborist = require('@npmcli/arborist') |
| 139 | try { |
| 140 | const opts = { |
| 141 | ...this.npm.flatOptions, |
| 142 | path: this.top, |
| 143 | } |
| 144 | const arb = new Arborist(opts) |
| 145 | actualTree = await arb.loadActual(opts) |
| 146 | node = actualTree && |
| 147 | actualTree.inventory.query('name', spec.name) |
| 148 | .values().next().value |
| 149 | } catch { |
| 150 | log.verbose('diff', 'failed to load actual install tree') |
no test coverage detected