Skip to content

Commit

Permalink
fix: refactor get package name
Browse files Browse the repository at this point in the history
renames read-local-package to read-package-name

The global check needed to be moved outside this function, because it
was handled differently (and will be even moreso when we implement diff
workspaces) in each function.  This allowed us to now pass in the prefix
itself instead of the npm object, so we can reuse this function to look
up package names when we refactor npm diff for workspaces.
  • Loading branch information
wraithgar committed May 28, 2021
1 parent 7b56bfd commit 86fb015
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 103 deletions.
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/another@7.7.7
`

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

0 comments on commit 86fb015

Please sign in to comment.