Skip to content

Commit e420d75

Browse files
committed
fix(refactor): use output.buffer for pkg
1 parent b1f8f3d commit e420d75

File tree

1 file changed

+19
-44
lines changed

1 file changed

+19
-44
lines changed

lib/commands/pkg.js

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,7 @@ class Pkg extends BaseCommand {
2525
static workspaces = true
2626
static ignoreImplicitWorkspace = false
2727

28-
async exec (args, { prefix } = {}) {
29-
if (!prefix) {
30-
this.prefix = this.npm.localPrefix
31-
} else {
32-
this.prefix = prefix
33-
}
34-
28+
async exec (args, { prefix = this.npm.localPrefix, ...opts } = {}) {
3529
if (this.npm.global) {
3630
throw Object.assign(
3731
new Error(`There's no package.json file to manage on global mode`),
@@ -42,38 +36,32 @@ class Pkg extends BaseCommand {
4236
const [cmd, ..._args] = args
4337
switch (cmd) {
4438
case 'get':
45-
return this.get(_args)
39+
return this.get(_args, { prefix, ...opts })
4640
case 'set':
47-
return this.set(_args)
41+
return this.set(_args, { prefix, ...opts }).then(p => p.save())
4842
case 'delete':
49-
return this.delete(_args)
43+
return this.delete(_args, { prefix, ...opts }).then(p => p.save())
5044
case 'fix':
51-
return this.fix(_args)
45+
return PackageJson.fix(prefix).then(p => p.save())
5246
default:
5347
throw this.usageError()
5448
}
5549
}
5650

5751
async execWorkspaces (args) {
5852
await this.setWorkspaces()
59-
const result = {}
60-
for (const [workspaceName, workspacePath] of this.workspaces.entries()) {
61-
this.prefix = workspacePath
62-
result[workspaceName] = await this.exec(args, { prefix: workspacePath })
53+
for (const [workspace, prefix] of this.workspaces.entries()) {
54+
await this.exec(args, { prefix, workspace })
6355
}
64-
// when running in workspaces names, make sure to key by workspace
65-
// name the results of each value retrieved in each ws
66-
output.standard(JSON.stringify(result, null, 2))
6756
}
6857

69-
async get (args) {
70-
const pkgJson = await PackageJson.load(this.prefix)
58+
async get (args, { prefix, workspace }) {
59+
const pkgJson = await PackageJson.load(prefix)
7160

72-
const { content } = pkgJson
73-
let result = !args.length && content
61+
let result = pkgJson.content
7462

75-
if (!result) {
76-
const q = new Queryable(content)
63+
if (args.length) {
64+
const q = new Queryable(result)
7765
result = q.query(args)
7866

7967
// in case there's only a single result from the query
@@ -83,16 +71,10 @@ class Pkg extends BaseCommand {
8371
}
8472
}
8573

86-
// only outputs if not running with workspaces config
87-
// execWorkspaces will handle the output otherwise
88-
if (!this.workspaces) {
89-
output.standard(JSON.stringify(result, null, 2))
90-
}
91-
92-
return result
74+
output.buffer(workspace ? { [workspace]: result } : result)
9375
}
9476

95-
async set (args) {
77+
async set (args, { prefix }) {
9678
const setError = () =>
9779
this.usageError('npm pkg set expects a key=value pair of args.')
9880

@@ -102,7 +84,7 @@ class Pkg extends BaseCommand {
10284

10385
const force = this.npm.config.get('force')
10486
const json = this.npm.config.get('json')
105-
const pkgJson = await PackageJson.load(this.prefix)
87+
const pkgJson = await PackageJson.load(prefix)
10688
const q = new Queryable(pkgJson.content)
10789
for (const arg of args) {
10890
const [key, ...rest] = arg.split('=')
@@ -114,19 +96,18 @@ class Pkg extends BaseCommand {
11496
q.set(key, json ? JSON.parse(value) : value, { force })
11597
}
11698

117-
pkgJson.update(q.toJSON())
118-
await pkgJson.save()
99+
return pkgJson.update(q.toJSON())
119100
}
120101

121-
async delete (args) {
102+
async delete (args, { prefix }) {
122103
const setError = () =>
123104
this.usageError('npm pkg delete expects key args.')
124105

125106
if (!args.length) {
126107
throw setError()
127108
}
128109

129-
const pkgJson = await PackageJson.load(this.prefix)
110+
const pkgJson = await PackageJson.load(prefix)
130111
const q = new Queryable(pkgJson.content)
131112
for (const key of args) {
132113
if (!key) {
@@ -136,13 +117,7 @@ class Pkg extends BaseCommand {
136117
q.delete(key)
137118
}
138119

139-
pkgJson.update(q.toJSON())
140-
await pkgJson.save()
141-
}
142-
143-
async fix () {
144-
const pkgJson = await PackageJson.fix(this.prefix)
145-
await pkgJson.save()
120+
return pkgJson.update(q.toJSON())
146121
}
147122
}
148123

0 commit comments

Comments
 (0)