Skip to content

Commit 8da28b4

Browse files
wraithgarlukekarrys
authored andcommitted
fix: remove lib/utils/read-package-name.js
This code wasn't doing anything special, just dereferencing `name` from a packument. There is no need for this to exist. Most of the tests were able to handle having this go away, except for `npm owner` which had to have its tests rewritten to be real, which of course surfaced bugs along the way of behavior that was incorrectly being tested. `npm owner` needs some love to clean up its UX, it throws or returns inconsistently. I did fix it so that if there is no package.json in cwd it errored as expected instead of throwing `ENOENT` which is what it did before.
1 parent b06e89f commit 8da28b4

File tree

8 files changed

+426
-722
lines changed

8 files changed

+426
-722
lines changed

lib/commands/diff.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const Arborist = require('@npmcli/arborist')
66
const pacote = require('pacote')
77
const pickManifest = require('npm-pick-manifest')
88
const log = require('../utils/log-shim')
9-
const readPackageName = require('../utils/read-package-name.js')
9+
const readPackage = require('read-package-json-fast')
1010
const BaseCommand = require('../base-command.js')
1111

1212
class Diff extends BaseCommand {
@@ -81,7 +81,8 @@ class Diff extends BaseCommand {
8181
async packageName (path) {
8282
let name
8383
try {
84-
name = await readPackageName(this.prefix)
84+
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
85+
name = pkg.name
8586
} catch (e) {
8687
log.verbose('diff', 'could not read project dir package.json')
8788
}
@@ -114,7 +115,8 @@ class Diff extends BaseCommand {
114115
let noPackageJson
115116
let pkgName
116117
try {
117-
pkgName = await readPackageName(this.prefix)
118+
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
119+
pkgName = pkg.name
118120
} catch (e) {
119121
log.verbose('diff', 'could not read project dir package.json')
120122
noPackageJson = true
@@ -225,7 +227,8 @@ class Diff extends BaseCommand {
225227
if (semverA && semverB) {
226228
let pkgName
227229
try {
228-
pkgName = await readPackageName(this.prefix)
230+
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
231+
pkgName = pkg.name
229232
} catch (e) {
230233
log.verbose('diff', 'could not read project dir package.json')
231234
}

lib/commands/dist-tag.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const npa = require('npm-package-arg')
2+
const path = require('path')
23
const regFetch = require('npm-registry-fetch')
34
const semver = require('semver')
45
const log = require('../utils/log-shim')
56
const otplease = require('../utils/otplease.js')
6-
const readPackageName = require('../utils/read-package-name.js')
7+
const readPackage = require('read-package-json-fast')
78
const BaseCommand = require('../base-command.js')
89

910
class DistTag extends BaseCommand {
@@ -150,12 +151,12 @@ class DistTag extends BaseCommand {
150151
if (this.npm.config.get('global')) {
151152
throw this.usageError()
152153
}
153-
const pkg = await readPackageName(this.npm.prefix)
154-
if (!pkg) {
154+
const { name } = await readPackage(path.resolve(this.npm.prefix, 'package.json'))
155+
if (!name) {
155156
throw this.usageError()
156157
}
157158

158-
return this.list(pkg, opts)
159+
return this.list(name, opts)
159160
}
160161
spec = npa(spec)
161162

lib/commands/owner.js

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ const npmFetch = require('npm-registry-fetch')
33
const pacote = require('pacote')
44
const log = require('../utils/log-shim')
55
const otplease = require('../utils/otplease.js')
6-
const readLocalPkgName = require('../utils/read-package-name.js')
6+
const readPackageJsonFast = require('read-package-json-fast')
77
const BaseCommand = require('../base-command.js')
8+
const { resolve } = require('path')
9+
10+
const readJson = async (pkg) => {
11+
try {
12+
const json = await readPackageJsonFast(pkg)
13+
return json
14+
} catch {
15+
return {}
16+
}
17+
}
818

919
class Owner extends BaseCommand {
1020
static description = 'Manage package owners'
@@ -41,12 +51,12 @@ class Owner extends BaseCommand {
4151
if (this.npm.config.get('global')) {
4252
return []
4353
}
44-
const pkgName = await readLocalPkgName(this.npm.prefix)
45-
if (!pkgName) {
54+
const { name } = await readJson(resolve(this.npm.prefix, 'package.json'))
55+
if (!name) {
4656
return []
4757
}
4858

49-
const spec = npa(pkgName)
59+
const spec = npa(name)
5060
const data = await pacote.packument(spec, {
5161
...this.npm.flatOptions,
5262
fullMetadata: true,
@@ -96,12 +106,12 @@ class Owner extends BaseCommand {
96106
if (this.npm.config.get('global')) {
97107
throw this.usageError()
98108
}
99-
const pkgName = await readLocalPkgName(this.npm.prefix)
100-
if (!pkgName) {
109+
const { name } = await readJson(resolve(this.npm.prefix, 'package.json'))
110+
if (!name) {
101111
throw this.usageError()
102112
}
103113

104-
return pkgName
114+
return name
105115
}
106116
return pkg
107117
}
@@ -125,15 +135,6 @@ class Owner extends BaseCommand {
125135
throw err
126136
}
127137

128-
if (!u || !u.name || u.error) {
129-
throw Object.assign(
130-
new Error(
131-
"Couldn't get user data for " + user + ': ' + JSON.stringify(u)
132-
),
133-
{ code: 'EOWNERUSER' }
134-
)
135-
}
136-
137138
// normalize user data
138139
u = { name: u.name, email: u.email }
139140

@@ -177,32 +178,31 @@ class Owner extends BaseCommand {
177178
}
178179

179180
const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}`
180-
const res = await otplease(this.npm.flatOptions, opts => {
181-
return npmFetch.json(dataPath, {
182-
...opts,
183-
method: 'PUT',
184-
body: {
185-
_id: data._id,
186-
_rev: data._rev,
187-
maintainers,
188-
},
189-
spec,
181+
try {
182+
const res = await otplease(this.npm.flatOptions, opts => {
183+
return npmFetch.json(dataPath, {
184+
...opts,
185+
method: 'PUT',
186+
body: {
187+
_id: data._id,
188+
_rev: data._rev,
189+
maintainers,
190+
},
191+
spec,
192+
})
190193
})
191-
})
192-
193-
if (!res.error) {
194194
if (addOrRm === 'add') {
195195
this.npm.output(`+ ${user} (${spec.name})`)
196196
} else {
197197
this.npm.output(`- ${user} (${spec.name})`)
198198
}
199-
} else {
199+
return res
200+
} catch (err) {
200201
throw Object.assign(
201-
new Error('Failed to update package: ' + JSON.stringify(res)),
202+
new Error('Failed to update package: ' + JSON.stringify(err.message)),
202203
{ code: 'EOWNERMUTATE' }
203204
)
204205
}
205-
return res
206206
}
207207
}
208208

tap-snapshots/test/lib/commands/owner.js.test.cjs

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/fixtures/mock-registry.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* for tests against any registry data.
66
*/
77
const pacote = require('pacote')
8+
const npa = require('npm-package-arg')
89
class MockRegistry {
910
#tap
1011
#nock
@@ -84,6 +85,16 @@ class MockRegistry {
8485
).reply(200)
8586
}
8687

88+
couchuser ({ username, body, responseCode = 200 }) {
89+
if (body) {
90+
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
91+
.reply(responseCode, body)
92+
} else {
93+
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
94+
.reply(responseCode, { _id: `org.couchdb.user:${username}`, email: '', name: username })
95+
}
96+
}
97+
8798
couchlogin ({ username, password, email, otp, token = 'npm_default-test-token' }) {
8899
this.nock = this.nock
89100
.post('/-/v1/login').reply(401, { error: 'You must be logged in to publish packages.' })
@@ -156,7 +167,8 @@ class MockRegistry {
156167

157168
async package ({ manifest, times = 1, query, tarballs }) {
158169
let nock = this.nock
159-
nock = nock.get(`/${manifest.name}`).times(times)
170+
const spec = npa(manifest.name)
171+
nock = nock.get(`/${spec.escapedName}`).times(times)
160172
if (query) {
161173
nock = nock.query(query)
162174
}
@@ -203,6 +215,7 @@ class MockRegistry {
203215
dist: {
204216
tarball: `${this.#registry}/${name}/-/${name}-${packument.version}.tgz`,
205217
},
218+
maintainers: [],
206219
...packument,
207220
}
208221
manifest.time[packument.version] = new Date()

test/lib/commands/diff.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ t.test('single arg', t => {
417417

418418
const Diff = t.mock('../../../lib/commands/diff.js', {
419419
...mocks,
420-
'../../../lib/utils/read-package-name.js': async () => 'my-project',
421420
pacote: {
422421
packument: spec => {
423422
t.equal(spec.name, 'lorem', 'should have expected spec name')
@@ -455,7 +454,6 @@ t.test('single arg', t => {
455454

456455
const Diff = t.mock('../../../lib/commands/diff.js', {
457456
...mocks,
458-
'../../../lib/utils/read-package-name.js': async () => 'my-project',
459457
'@npmcli/arborist': class {
460458
constructor () {
461459
throw new Error('ERR')

0 commit comments

Comments
 (0)