Skip to content

Commit 09f9d2e

Browse files
wraithgarruyadorno
authored andcommitted
fix: refactor get package name
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. PR-URL: #3331 Credit: @wraithgar Close: #3331 Reviewed-by: @ruyadorno
1 parent 7b6db90 commit 09f9d2e

9 files changed

Lines changed: 203 additions & 103 deletions

File tree

lib/diff.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const npmlog = require('npmlog')
88
const pacote = require('pacote')
99
const pickManifest = require('npm-pick-manifest')
1010

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

1414
class Diff extends BaseCommand {
@@ -97,7 +97,7 @@ class Diff extends BaseCommand {
9797
let noPackageJson
9898
let pkgName
9999
try {
100-
pkgName = await readLocalPkg(this.npm)
100+
pkgName = await readPackageName(this.npm.prefix)
101101
} catch (e) {
102102
npmlog.verbose('diff', 'could not read project dir package.json')
103103
noPackageJson = true
@@ -120,7 +120,7 @@ class Diff extends BaseCommand {
120120
let noPackageJson
121121
let pkgName
122122
try {
123-
pkgName = await readLocalPkg(this.npm)
123+
pkgName = await readPackageName(this.npm.prefix)
124124
} catch (e) {
125125
npmlog.verbose('diff', 'could not read project dir package.json')
126126
noPackageJson = true
@@ -238,7 +238,7 @@ class Diff extends BaseCommand {
238238
if (semverA && semverB) {
239239
let pkgName
240240
try {
241-
pkgName = await readLocalPkg(this.npm)
241+
pkgName = await readPackageName(this.npm.prefix)
242242
} catch (e) {
243243
npmlog.verbose('diff', 'could not read project dir package.json')
244244
}

lib/dist-tag.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const regFetch = require('npm-registry-fetch')
44
const semver = require('semver')
55

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

@@ -64,7 +64,7 @@ class DistTag extends BaseCommand {
6464
// should be listing the existing tags
6565
return this.list(cmdName, opts)
6666
} else
67-
throw this.usage
67+
throw this.usageError()
6868
}
6969

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

104104
if (!spec.name || !version || !defaultTag)
105-
throw this.usage
105+
throw this.usageError()
106106

107107
const t = defaultTag.trim()
108108

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

137137
if (!spec.name)
138-
throw this.usage
138+
throw this.usageError()
139139

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

158158
async list (spec, opts) {
159159
if (!spec) {
160-
const pkg = await readLocalPkgName(this.npm)
160+
if (this.npm.config.get('global'))
161+
throw this.usageError()
162+
const pkg = await readPackageName(this.npm.prefix)
161163
if (!pkg)
162-
throw this.usage
164+
throw this.usageError()
163165

164166
return this.list(pkg, opts)
165167
}

lib/owner.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const npmFetch = require('npm-registry-fetch')
44
const pacote = require('pacote')
55

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

1010
class Owner extends BaseCommand {
@@ -47,7 +47,9 @@ class Owner extends BaseCommand {
4747

4848
// reaches registry in order to autocomplete rm
4949
if (argv[2] === 'rm') {
50-
const pkgName = await readLocalPkg(this.npm)
50+
if (this.npm.config.get('global'))
51+
return []
52+
const pkgName = await readLocalPkgName(this.npm.prefix)
5153
if (!pkgName)
5254
return []
5355

@@ -84,7 +86,10 @@ class Owner extends BaseCommand {
8486

8587
async ls (pkg, opts) {
8688
if (!pkg) {
87-
const pkgName = await readLocalPkg(this.npm)
89+
if (this.npm.config.get('global'))
90+
throw this.usageError()
91+
92+
const pkgName = await readLocalPkgName(this.npm.prefix)
8893
if (!pkgName)
8994
throw this.usageError()
9095

@@ -113,7 +118,9 @@ class Owner extends BaseCommand {
113118
throw this.usageError()
114119

115120
if (!pkg) {
116-
const pkgName = await readLocalPkg(this.npm)
121+
if (this.npm.config.get('global'))
122+
throw this.usageError()
123+
const pkgName = await readLocalPkgName(this.npm.prefix)
117124
if (!pkgName)
118125
throw this.usageError()
119126

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

133140
if (!pkg) {
134-
const pkgName = await readLocalPkg(this.npm)
141+
if (this.npm.config.get('global'))
142+
throw this.usageError()
143+
const pkgName = await readLocalPkgName(this.npm.prefix)
135144
if (!pkgName)
136145
throw this.usageError()
137146

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const { resolve } = require('path')
22
const readJson = require('read-package-json-fast')
3-
async function readLocalPackageName (npm) {
4-
if (npm.config.get('global'))
5-
return
6-
7-
const filepath = resolve(npm.prefix, 'package.json')
3+
async function readLocalPackageName (prefix) {
4+
const filepath = resolve(prefix, 'package.json')
85
const json = await readJson(filepath)
96
return json.name
107
}

tap-snapshots/test/lib/dist-tag.js.test.cjs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77
'use strict'
88
exports[`test/lib/dist-tag.js TAP add missing args > should exit usage error message 1`] = `
9-
npm dist-tag
9+
Error:
10+
Usage: npm dist-tag
1011
1112
Modify package distribution tags
1213
@@ -21,11 +22,14 @@ Options:
2122
2223
alias: dist-tags
2324
24-
Run "npm help dist-tag" for more info
25+
Run "npm help dist-tag" for more info {
26+
"code": "EUSAGE",
27+
}
2528
`
2629

2730
exports[`test/lib/dist-tag.js TAP add missing pkg name > should exit usage error message 1`] = `
28-
npm dist-tag
31+
Error:
32+
Usage: npm dist-tag
2933
3034
Modify package distribution tags
3135
@@ -40,7 +44,9 @@ Options:
4044
4145
alias: dist-tags
4246
43-
Run "npm help dist-tag" for more info
47+
Run "npm help dist-tag" for more info {
48+
"code": "EUSAGE",
49+
}
4450
`
4551

4652
exports[`test/lib/dist-tag.js TAP add new tag > should return success msg 1`] = `
@@ -53,7 +59,8 @@ dist-tag add 1.0.0 to @scoped/another@7.7.7
5359
`
5460

5561
exports[`test/lib/dist-tag.js TAP borked cmd usage > should show usage error 1`] = `
56-
npm dist-tag
62+
Error:
63+
Usage: npm dist-tag
5764
5865
Modify package distribution tags
5966
@@ -68,7 +75,31 @@ Options:
6875
6976
alias: dist-tags
7077
71-
Run "npm help dist-tag" for more info
78+
Run "npm help dist-tag" for more info {
79+
"code": "EUSAGE",
80+
}
81+
`
82+
83+
exports[`test/lib/dist-tag.js TAP ls global > should throw basic usage 1`] = `
84+
Error:
85+
Usage: npm dist-tag
86+
87+
Modify package distribution tags
88+
89+
Usage:
90+
npm dist-tag add <pkg>@<version> [<tag>]
91+
npm dist-tag rm <pkg> <tag>
92+
npm dist-tag ls [<pkg>]
93+
94+
Options:
95+
[-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
96+
[-ws|--workspaces]
97+
98+
alias: dist-tags
99+
100+
Run "npm help dist-tag" for more info {
101+
"code": "EUSAGE",
102+
}
72103
`
73104

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

80111
exports[`test/lib/dist-tag.js TAP ls on missing name in current package > should throw usage error message 1`] = `
81-
npm dist-tag
112+
Error:
113+
Usage: npm dist-tag
82114
83115
Modify package distribution tags
84116
@@ -93,7 +125,9 @@ Options:
93125
94126
alias: dist-tags
95127
96-
Run "npm help dist-tag" for more info
128+
Run "npm help dist-tag" for more info {
129+
"code": "EUSAGE",
130+
}
97131
`
98132

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

135169
exports[`test/lib/dist-tag.js TAP remove missing pkg name > should exit usage error message 1`] = `
136-
npm dist-tag
170+
Error:
171+
Usage: npm dist-tag
137172
138173
Modify package distribution tags
139174
@@ -148,7 +183,9 @@ Options:
148183
149184
alias: dist-tags
150185
151-
Run "npm help dist-tag" for more info
186+
Run "npm help dist-tag" for more info {
187+
"code": "EUSAGE",
188+
}
152189
`
153190

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

0 commit comments

Comments
 (0)