@@ -11,6 +11,7 @@ import { parse as yamlParse } from 'yaml'
11
11
12
12
import { getManifestData } from '@socketsecurity/registry'
13
13
import {
14
+ hasKeys ,
14
15
hasOwn ,
15
16
isObject ,
16
17
toSortedObject
@@ -187,17 +188,47 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
187
188
if ( oldValue ) {
188
189
// The field already exists so we simply update the field value.
189
190
if ( field === PNPM_FIELD_NAME ) {
190
- editablePkgJson [ 'update' ] ( {
191
- [ field ] : {
192
- ...( isObject ( oldValue ) ? oldValue : { } ) ,
193
- overrides : value
194
- }
191
+ if ( hasKeys ( value ) ) {
192
+ editablePkgJson . update ( {
193
+ [ field ] : {
194
+ ...( isObject ( oldValue ) ? oldValue : { } ) ,
195
+ overrides : value
196
+ }
197
+ } )
198
+ } else {
199
+ // Properties with undefined values are omitted when saved as JSON.
200
+ editablePkgJson . update (
201
+ < typeof pkgJson > ( hasKeys ( pkgJson [ field ] )
202
+ ? {
203
+ [ field ] : {
204
+ ...( isObject ( oldValue ) ? oldValue : { } ) ,
205
+ overrides : undefined
206
+ }
207
+ }
208
+ : { [ field ] : undefined } )
209
+ )
210
+ }
211
+ } else if (
212
+ field === OVERRIDES_FIELD_NAME ||
213
+ field === RESOLUTIONS_FIELD_NAME
214
+ ) {
215
+ // Properties with undefined values are omitted when saved as JSON.
216
+ editablePkgJson . update ( < typeof pkgJson > {
217
+ [ field ] : hasKeys ( value ) ? value : undefined
195
218
} )
196
219
} else {
197
220
editablePkgJson . update ( { [ field ] : value } )
198
221
}
199
222
return
200
223
}
224
+ if (
225
+ ( field === OVERRIDES_FIELD_NAME ||
226
+ field === PNPM_FIELD_NAME ||
227
+ field === RESOLUTIONS_FIELD_NAME ) &&
228
+ ! hasKeys ( value )
229
+ ) {
230
+ return
231
+ }
201
232
// Since the field doesn't exist we want to insert it into the package.json
202
233
// in a place that makes sense, e.g. close to the "dependencies" field. If
203
234
// we can't find a place to insert the field we'll add it to the bottom.
0 commit comments