Skip to content

Commit

Permalink
fix: remove lib/utils/read-package-name.js
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wraithgar authored and lukekarrys committed Apr 19, 2022
1 parent b06e89f commit 8da28b4
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 722 deletions.
11 changes: 7 additions & 4 deletions lib/commands/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Arborist = require('@npmcli/arborist')
const pacote = require('pacote')
const pickManifest = require('npm-pick-manifest')
const log = require('../utils/log-shim')
const readPackageName = require('../utils/read-package-name.js')
const readPackage = require('read-package-json-fast')
const BaseCommand = require('../base-command.js')

class Diff extends BaseCommand {
Expand Down Expand Up @@ -81,7 +81,8 @@ class Diff extends BaseCommand {
async packageName (path) {
let name
try {
name = await readPackageName(this.prefix)
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
name = pkg.name
} catch (e) {
log.verbose('diff', 'could not read project dir package.json')
}
Expand Down Expand Up @@ -114,7 +115,8 @@ class Diff extends BaseCommand {
let noPackageJson
let pkgName
try {
pkgName = await readPackageName(this.prefix)
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
pkgName = pkg.name
} catch (e) {
log.verbose('diff', 'could not read project dir package.json')
noPackageJson = true
Expand Down Expand Up @@ -225,7 +227,8 @@ class Diff extends BaseCommand {
if (semverA && semverB) {
let pkgName
try {
pkgName = await readPackageName(this.prefix)
const pkg = await readPackage(resolve(this.prefix, 'package.json'))
pkgName = pkg.name
} catch (e) {
log.verbose('diff', 'could not read project dir package.json')
}
Expand Down
9 changes: 5 additions & 4 deletions lib/commands/dist-tag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const npa = require('npm-package-arg')
const path = require('path')
const regFetch = require('npm-registry-fetch')
const semver = require('semver')
const log = require('../utils/log-shim')
const otplease = require('../utils/otplease.js')
const readPackageName = require('../utils/read-package-name.js')
const readPackage = require('read-package-json-fast')
const BaseCommand = require('../base-command.js')

class DistTag extends BaseCommand {
Expand Down Expand Up @@ -150,12 +151,12 @@ class DistTag extends BaseCommand {
if (this.npm.config.get('global')) {
throw this.usageError()
}
const pkg = await readPackageName(this.npm.prefix)
if (!pkg) {
const { name } = await readPackage(path.resolve(this.npm.prefix, 'package.json'))
if (!name) {
throw this.usageError()
}

return this.list(pkg, opts)
return this.list(name, opts)
}
spec = npa(spec)

Expand Down
64 changes: 32 additions & 32 deletions lib/commands/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ const npmFetch = require('npm-registry-fetch')
const pacote = require('pacote')
const log = require('../utils/log-shim')
const otplease = require('../utils/otplease.js')
const readLocalPkgName = require('../utils/read-package-name.js')
const readPackageJsonFast = require('read-package-json-fast')
const BaseCommand = require('../base-command.js')
const { resolve } = require('path')

const readJson = async (pkg) => {
try {
const json = await readPackageJsonFast(pkg)
return json
} catch {
return {}
}
}

class Owner extends BaseCommand {
static description = 'Manage package owners'
Expand Down Expand Up @@ -41,12 +51,12 @@ class Owner extends BaseCommand {
if (this.npm.config.get('global')) {
return []
}
const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName) {
const { name } = await readJson(resolve(this.npm.prefix, 'package.json'))
if (!name) {
return []
}

const spec = npa(pkgName)
const spec = npa(name)
const data = await pacote.packument(spec, {
...this.npm.flatOptions,
fullMetadata: true,
Expand Down Expand Up @@ -96,12 +106,12 @@ class Owner extends BaseCommand {
if (this.npm.config.get('global')) {
throw this.usageError()
}
const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName) {
const { name } = await readJson(resolve(this.npm.prefix, 'package.json'))
if (!name) {
throw this.usageError()
}

return pkgName
return name
}
return pkg
}
Expand All @@ -125,15 +135,6 @@ class Owner extends BaseCommand {
throw err
}

if (!u || !u.name || u.error) {
throw Object.assign(
new Error(
"Couldn't get user data for " + user + ': ' + JSON.stringify(u)
),
{ code: 'EOWNERUSER' }
)
}

// normalize user data
u = { name: u.name, email: u.email }

Expand Down Expand Up @@ -177,32 +178,31 @@ class Owner extends BaseCommand {
}

const dataPath = `/${spec.escapedName}/-rev/${encodeURIComponent(data._rev)}`
const res = await otplease(this.npm.flatOptions, opts => {
return npmFetch.json(dataPath, {
...opts,
method: 'PUT',
body: {
_id: data._id,
_rev: data._rev,
maintainers,
},
spec,
try {
const res = await otplease(this.npm.flatOptions, opts => {
return npmFetch.json(dataPath, {
...opts,
method: 'PUT',
body: {
_id: data._id,
_rev: data._rev,
maintainers,
},
spec,
})
})
})

if (!res.error) {
if (addOrRm === 'add') {
this.npm.output(`+ ${user} (${spec.name})`)
} else {
this.npm.output(`- ${user} (${spec.name})`)
}
} else {
return res
} catch (err) {
throw Object.assign(
new Error('Failed to update package: ' + JSON.stringify(res)),
new Error('Failed to update package: ' + JSON.stringify(err.message)),
{ code: 'EOWNERMUTATE' }
)
}
return res
}
}

Expand Down
20 changes: 0 additions & 20 deletions tap-snapshots/test/lib/commands/owner.js.test.cjs

This file was deleted.

15 changes: 14 additions & 1 deletion test/fixtures/mock-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* for tests against any registry data.
*/
const pacote = require('pacote')
const npa = require('npm-package-arg')
class MockRegistry {
#tap
#nock
Expand Down Expand Up @@ -84,6 +85,16 @@ class MockRegistry {
).reply(200)
}

couchuser ({ username, body, responseCode = 200 }) {
if (body) {
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
.reply(responseCode, body)
} else {
this.nock = this.nock.get(`/-/user/org.couchdb.user:${encodeURIComponent(username)}`)
.reply(responseCode, { _id: `org.couchdb.user:${username}`, email: '', name: username })
}
}

couchlogin ({ username, password, email, otp, token = 'npm_default-test-token' }) {
this.nock = this.nock
.post('/-/v1/login').reply(401, { error: 'You must be logged in to publish packages.' })
Expand Down Expand Up @@ -156,7 +167,8 @@ class MockRegistry {

async package ({ manifest, times = 1, query, tarballs }) {
let nock = this.nock
nock = nock.get(`/${manifest.name}`).times(times)
const spec = npa(manifest.name)
nock = nock.get(`/${spec.escapedName}`).times(times)
if (query) {
nock = nock.query(query)
}
Expand Down Expand Up @@ -203,6 +215,7 @@ class MockRegistry {
dist: {
tarball: `${this.#registry}/${name}/-/${name}-${packument.version}.tgz`,
},
maintainers: [],
...packument,
}
manifest.time[packument.version] = new Date()
Expand Down
2 changes: 0 additions & 2 deletions test/lib/commands/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ t.test('single arg', t => {

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

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

0 comments on commit 8da28b4

Please sign in to comment.