Skip to content

Commit 9497230

Browse files
committed
Only add fields if values aren't empty
1 parent 163c9aa commit 9497230

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/commands/optimize.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { parse as yamlParse } from 'yaml'
1111

1212
import { getManifestData } from '@socketsecurity/registry'
1313
import {
14+
hasKeys,
1415
hasOwn,
1516
isObject,
1617
toSortedObject
@@ -187,17 +188,47 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
187188
if (oldValue) {
188189
// The field already exists so we simply update the field value.
189190
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
195218
})
196219
} else {
197220
editablePkgJson.update({ [field]: value })
198221
}
199222
return
200223
}
224+
if (
225+
(field === OVERRIDES_FIELD_NAME ||
226+
field === PNPM_FIELD_NAME ||
227+
field === RESOLUTIONS_FIELD_NAME) &&
228+
!hasKeys(value)
229+
) {
230+
return
231+
}
201232
// Since the field doesn't exist we want to insert it into the package.json
202233
// in a place that makes sense, e.g. close to the "dependencies" field. If
203234
// we can't find a place to insert the field we'll add it to the bottom.

0 commit comments

Comments
 (0)