@@ -25,13 +25,7 @@ class Pkg extends BaseCommand {
25
25
static workspaces = true
26
26
static ignoreImplicitWorkspace = false
27
27
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 } = { } ) {
35
29
if ( this . npm . global ) {
36
30
throw Object . assign (
37
31
new Error ( `There's no package.json file to manage on global mode` ) ,
@@ -42,38 +36,32 @@ class Pkg extends BaseCommand {
42
36
const [ cmd , ..._args ] = args
43
37
switch ( cmd ) {
44
38
case 'get' :
45
- return this . get ( _args )
39
+ return this . get ( _args , { prefix , ... opts } )
46
40
case 'set' :
47
- return this . set ( _args )
41
+ return this . set ( _args , { prefix , ... opts } ) . then ( p => p . save ( ) )
48
42
case 'delete' :
49
- return this . delete ( _args )
43
+ return this . delete ( _args , { prefix , ... opts } ) . then ( p => p . save ( ) )
50
44
case 'fix' :
51
- return this . fix ( _args )
45
+ return PackageJson . fix ( prefix ) . then ( p => p . save ( ) )
52
46
default :
53
47
throw this . usageError ( )
54
48
}
55
49
}
56
50
57
51
async execWorkspaces ( args ) {
58
52
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 } )
63
55
}
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 ) )
67
56
}
68
57
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 )
71
60
72
- const { content } = pkgJson
73
- let result = ! args . length && content
61
+ let result = pkgJson . content
74
62
75
- if ( ! result ) {
76
- const q = new Queryable ( content )
63
+ if ( args . length ) {
64
+ const q = new Queryable ( result )
77
65
result = q . query ( args )
78
66
79
67
// in case there's only a single result from the query
@@ -83,16 +71,10 @@ class Pkg extends BaseCommand {
83
71
}
84
72
}
85
73
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 )
93
75
}
94
76
95
- async set ( args ) {
77
+ async set ( args , { prefix } ) {
96
78
const setError = ( ) =>
97
79
this . usageError ( 'npm pkg set expects a key=value pair of args.' )
98
80
@@ -102,7 +84,7 @@ class Pkg extends BaseCommand {
102
84
103
85
const force = this . npm . config . get ( 'force' )
104
86
const json = this . npm . config . get ( 'json' )
105
- const pkgJson = await PackageJson . load ( this . prefix )
87
+ const pkgJson = await PackageJson . load ( prefix )
106
88
const q = new Queryable ( pkgJson . content )
107
89
for ( const arg of args ) {
108
90
const [ key , ...rest ] = arg . split ( '=' )
@@ -114,19 +96,18 @@ class Pkg extends BaseCommand {
114
96
q . set ( key , json ? JSON . parse ( value ) : value , { force } )
115
97
}
116
98
117
- pkgJson . update ( q . toJSON ( ) )
118
- await pkgJson . save ( )
99
+ return pkgJson . update ( q . toJSON ( ) )
119
100
}
120
101
121
- async delete ( args ) {
102
+ async delete ( args , { prefix } ) {
122
103
const setError = ( ) =>
123
104
this . usageError ( 'npm pkg delete expects key args.' )
124
105
125
106
if ( ! args . length ) {
126
107
throw setError ( )
127
108
}
128
109
129
- const pkgJson = await PackageJson . load ( this . prefix )
110
+ const pkgJson = await PackageJson . load ( prefix )
130
111
const q = new Queryable ( pkgJson . content )
131
112
for ( const key of args ) {
132
113
if ( ! key ) {
@@ -136,13 +117,7 @@ class Pkg extends BaseCommand {
136
117
q . delete ( key )
137
118
}
138
119
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 ( ) )
146
121
}
147
122
}
148
123
0 commit comments