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: refactor get package name #3331

Merged
merged 1 commit into from
May 28, 2021
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
8 changes: 4 additions & 4 deletions lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const npmlog = require('npmlog')
const pacote = require('pacote')
const pickManifest = require('npm-pick-manifest')

const readLocalPkg = require('./utils/read-local-package.js')
const readPackageName = require('./utils/read-package-name.js')
const BaseCommand = require('./base-command.js')

class Diff extends BaseCommand {
Expand Down Expand Up @@ -97,7 +97,7 @@ class Diff extends BaseCommand {
let noPackageJson
let pkgName
try {
pkgName = await readLocalPkg(this.npm)
pkgName = await readPackageName(this.npm.prefix)
} catch (e) {
npmlog.verbose('diff', 'could not read project dir package.json')
noPackageJson = true
Expand All @@ -120,7 +120,7 @@ class Diff extends BaseCommand {
let noPackageJson
let pkgName
try {
pkgName = await readLocalPkg(this.npm)
pkgName = await readPackageName(this.npm.prefix)
} catch (e) {
npmlog.verbose('diff', 'could not read project dir package.json')
noPackageJson = true
Expand Down Expand Up @@ -238,7 +238,7 @@ class Diff extends BaseCommand {
if (semverA && semverB) {
let pkgName
try {
pkgName = await readLocalPkg(this.npm)
pkgName = await readPackageName(this.npm.prefix)
} catch (e) {
npmlog.verbose('diff', 'could not read project dir package.json')
}
Expand Down
14 changes: 8 additions & 6 deletions lib/dist-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const regFetch = require('npm-registry-fetch')
const semver = require('semver')

const otplease = require('./utils/otplease.js')
const readLocalPkgName = require('./utils/read-local-package.js')
const readPackageName = require('./utils/read-package-name.js')
const getWorkspaces = require('./workspaces/get-workspaces.js')
const BaseCommand = require('./base-command.js')

Expand Down Expand Up @@ -64,7 +64,7 @@ class DistTag extends BaseCommand {
// should be listing the existing tags
return this.list(cmdName, opts)
} else
throw this.usage
throw this.usageError()
}

execWorkspaces (args, filters, cb) {
Expand Down Expand Up @@ -102,7 +102,7 @@ class DistTag extends BaseCommand {
log.verbose('dist-tag add', defaultTag, 'to', spec.name + '@' + version)

if (!spec.name || !version || !defaultTag)
throw this.usage
throw this.usageError()

const t = defaultTag.trim()

Expand Down Expand Up @@ -135,7 +135,7 @@ class DistTag extends BaseCommand {
log.verbose('dist-tag del', tag, 'from', spec.name)

if (!spec.name)
throw this.usage
throw this.usageError()

const tags = await this.fetchTags(spec, opts)
if (!tags[tag]) {
Expand All @@ -157,9 +157,11 @@ class DistTag extends BaseCommand {

async list (spec, opts) {
if (!spec) {
const pkg = await readLocalPkgName(this.npm)
if (this.npm.config.get('global'))
throw this.usageError()
const pkg = await readPackageName(this.npm.prefix)
if (!pkg)
throw this.usage
throw this.usageError()

return this.list(pkg, opts)
}
Expand Down
19 changes: 14 additions & 5 deletions lib/owner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const npmFetch = require('npm-registry-fetch')
const pacote = require('pacote')

const otplease = require('./utils/otplease.js')
const readLocalPkg = require('./utils/read-local-package.js')
const readLocalPkgName = require('./utils/read-package-name.js')
const BaseCommand = require('./base-command.js')

class Owner extends BaseCommand {
Expand Down Expand Up @@ -47,7 +47,9 @@ class Owner extends BaseCommand {

// reaches registry in order to autocomplete rm
if (argv[2] === 'rm') {
const pkgName = await readLocalPkg(this.npm)
if (this.npm.config.get('global'))
return []
const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName)
return []

Expand Down Expand Up @@ -84,7 +86,10 @@ class Owner extends BaseCommand {

async ls (pkg, opts) {
if (!pkg) {
const pkgName = await readLocalPkg(this.npm)
if (this.npm.config.get('global'))
throw this.usageError()

const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName)
throw this.usageError()

Expand Down Expand Up @@ -113,7 +118,9 @@ class Owner extends BaseCommand {
throw this.usageError()

if (!pkg) {
const pkgName = await readLocalPkg(this.npm)
if (this.npm.config.get('global'))
throw this.usageError()
const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName)
throw this.usageError()

Expand All @@ -131,7 +138,9 @@ class Owner extends BaseCommand {
throw this.usageError()

if (!pkg) {
const pkgName = await readLocalPkg(this.npm)
if (this.npm.config.get('global'))
throw this.usageError()
const pkgName = await readLocalPkgName(this.npm.prefix)
if (!pkgName)
throw this.usageError()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const { resolve } = require('path')
const readJson = require('read-package-json-fast')
async function readLocalPackageName (npm) {
if (npm.config.get('global'))
return

const filepath = resolve(npm.prefix, 'package.json')
async function readLocalPackageName (prefix) {
const filepath = resolve(prefix, 'package.json')
const json = await readJson(filepath)
return json.name
}
Expand Down
57 changes: 47 additions & 10 deletions tap-snapshots/test/lib/dist-tag.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/
'use strict'
exports[`test/lib/dist-tag.js TAP add missing args > should exit usage error message 1`] = `
npm dist-tag
Error:
Usage: npm dist-tag

Modify package distribution tags

Expand All @@ -21,11 +22,14 @@ Options:

alias: dist-tags

Run "npm help dist-tag" for more info
Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP add missing pkg name > should exit usage error message 1`] = `
npm dist-tag
Error:
Usage: npm dist-tag

Modify package distribution tags

Expand All @@ -40,7 +44,9 @@ Options:

alias: dist-tags

Run "npm help dist-tag" for more info
Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP add new tag > should return success msg 1`] = `
Expand All @@ -53,7 +59,8 @@ dist-tag add 1.0.0 to @scoped/[email protected]
`

exports[`test/lib/dist-tag.js TAP borked cmd usage > should show usage error 1`] = `
npm dist-tag
Error:
Usage: npm dist-tag

Modify package distribution tags

Expand All @@ -68,7 +75,31 @@ Options:

alias: dist-tags

Run "npm help dist-tag" for more info
Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP ls global > should throw basic usage 1`] = `
Error:
Usage: npm dist-tag

Modify package distribution tags

Usage:
npm dist-tag add <pkg>@<version> [<tag>]
npm dist-tag rm <pkg> <tag>
npm dist-tag ls [<pkg>]

Options:
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
[-ws|--workspaces]

alias: dist-tags

Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP ls in current package > should list available tags for current package 1`] = `
Expand All @@ -78,7 +109,8 @@ latest: 1.0.0
`

exports[`test/lib/dist-tag.js TAP ls on missing name in current package > should throw usage error message 1`] = `
npm dist-tag
Error:
Usage: npm dist-tag

Modify package distribution tags

Expand All @@ -93,7 +125,9 @@ Options:

alias: dist-tags

Run "npm help dist-tag" for more info
Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP ls on missing package > should log no dist-tag found msg 1`] = `
Expand Down Expand Up @@ -133,7 +167,8 @@ exports[`test/lib/dist-tag.js TAP remove existing tag > should return success ms
`

exports[`test/lib/dist-tag.js TAP remove missing pkg name > should exit usage error message 1`] = `
npm dist-tag
Error:
Usage: npm dist-tag

Modify package distribution tags

Expand All @@ -148,7 +183,9 @@ Options:

alias: dist-tags

Run "npm help dist-tag" for more info
Run "npm help dist-tag" for more info {
"code": "EUSAGE",
}
`

exports[`test/lib/dist-tag.js TAP remove non-existing tag > should log error msg 1`] = `
Expand Down
Loading