11import fs from 'fs/promises'
22import path from 'node:path'
33
4- import EditablePackageJson from '@npmcli/package-json'
54import spawn from '@npmcli/promise-spawn'
65import meow from 'meow'
76import npa from 'npm-package-arg'
@@ -16,7 +15,7 @@ import {
1615 isObject ,
1716 toSortedObject
1817} from '@socketsecurity/registry/lib/objects'
19- import { fetchPackageManifest } from '@socketsecurity/registry/lib/packages'
18+ import { fetchPackageManifest , readPackageJson } from '@socketsecurity/registry/lib/packages'
2019import { pEach } from '@socketsecurity/registry/lib/promises'
2120import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
2221import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
@@ -31,10 +30,12 @@ import type {
3130 Agent ,
3231 StringKeyValueObject
3332} from '../utils/package-manager-detector'
34- import type { Content as NPMCliPackageJson } from '@npmcli/package-json'
3533import type { ManifestEntry } from '@socketsecurity/registry'
34+ import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
3635import type { Ora } from 'ora'
3736
37+ type PackageJson = Awaited < ReturnType < typeof readPackageJson > >
38+
3839const COMMAND_TITLE = 'Socket Optimize'
3940const OVERRIDES_FIELD_NAME = 'overrides'
4041const PNPM_FIELD_NAME = 'pnpm'
@@ -47,42 +48,42 @@ const manifestNpmOverrides = getManifestData('npm')!
4748type NpmOverrides = { [ key : string ] : string | StringKeyValueObject }
4849type PnpmOrYarnOverrides = { [ key : string ] : string }
4950type Overrides = NpmOverrides | PnpmOrYarnOverrides
50- type GetOverrides = ( pkgJson : NPMCliPackageJson ) => GetOverridesResult
51+ type GetOverrides = ( pkgJson : PackageJson ) => GetOverridesResult
5152type GetOverridesResult = {
5253 type : Agent
5354 overrides : Overrides
5455}
5556
5657const getOverridesDataByAgent : Record < Agent , GetOverrides > = {
57- bun ( pkgJson : NPMCliPackageJson ) {
58+ bun ( pkgJson : PackageJson ) {
5859 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
5960 return { type : 'yarn/berry' , overrides }
6061 } ,
6162 // npm overrides documentation:
6263 // https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
63- npm ( pkgJson : NPMCliPackageJson ) {
64+ npm ( pkgJson : PackageJson ) {
6465 const overrides = ( pkgJson as any ) ?. overrides ?? { }
6566 return { type : 'npm' , overrides }
6667 } ,
6768 // pnpm overrides documentation:
6869 // https://pnpm.io/package_json#pnpmoverrides
69- pnpm ( pkgJson : NPMCliPackageJson ) {
70+ pnpm ( pkgJson : PackageJson ) {
7071 const overrides = ( pkgJson as any ) ?. pnpm ?. overrides ?? { }
7172 return { type : 'pnpm' , overrides }
7273 } ,
73- vlt ( pkgJson : NPMCliPackageJson ) {
74+ vlt ( pkgJson : PackageJson ) {
7475 const overrides = ( pkgJson as any ) ?. overrides ?? { }
7576 return { type : 'vlt' , overrides }
7677 } ,
7778 // Yarn resolutions documentation:
7879 // https://yarnpkg.com/configuration/manifest#resolutions
79- 'yarn/berry' ( pkgJson : NPMCliPackageJson ) {
80+ 'yarn/berry' ( pkgJson : PackageJson ) {
8081 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
8182 return { type : 'yarn/berry' , overrides }
8283 } ,
8384 // Yarn resolutions documentation:
8485 // https://classic.yarnpkg.com/en/docs/selective-version-resolutions
85- 'yarn/classic' ( pkgJson : NPMCliPackageJson ) {
86+ 'yarn/classic' ( pkgJson : PackageJson ) {
8687 const overrides = ( pkgJson as any ) ?. resolutions ?? { }
8788 return { type : 'yarn/classic' , overrides }
8889 }
@@ -182,7 +183,7 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
182183 if ( oldValue ) {
183184 // The field already exists so we simply update the field value.
184185 if ( field === PNPM_FIELD_NAME ) {
185- editablePkgJson . update ( {
186+ editablePkgJson [ ' update' ] ( {
186187 [ field ] : {
187188 ...( isObject ( oldValue ) ? oldValue : { } ) ,
188189 overrides : value
@@ -437,7 +438,7 @@ function createActionMessage(
437438 return `${ verb } ${ overrideCount } Socket.dev optimized overrides${ workspaceCount ? ` in ${ workspaceCount } workspace${ workspaceCount > 1 ? 's' : '' } ` : '' } `
438439}
439440
440- function getDependencyEntries ( pkgJson : NPMCliPackageJson ) {
441+ function getDependencyEntries ( pkgJson : PackageJson ) {
441442 const {
442443 dependencies,
443444 devDependencies,
@@ -469,7 +470,7 @@ function getDependencyEntries(pkgJson: NPMCliPackageJson) {
469470async function getWorkspaceGlobs (
470471 agent : Agent ,
471472 pkgPath : string ,
472- pkgJson : NPMCliPackageJson
473+ pkgJson : PackageJson
473474) : Promise < string [ ] | undefined > {
474475 let workspacePatterns
475476 if ( agent === 'pnpm' ) {
@@ -570,10 +571,10 @@ async function addOverrides(
570571 state = createAddOverridesState ( )
571572) : Promise < AddOverridesState > {
572573 if ( editablePkgJson === undefined ) {
573- editablePkgJson = await EditablePackageJson . load ( pkgPath )
574+ editablePkgJson = await readPackageJson ( pkgPath , { editable : true } )
574575 }
575576 const { spinner } = state
576- const pkgJson : Readonly < NPMCliPackageJson > = editablePkgJson . content
577+ const { content : pkgJson } = editablePkgJson
577578 const isRoot = pkgPath === rootPath
578579 const isLockScanned = isRoot && ! prod
579580 const workspaceName = path . relative ( rootPath , pkgPath )
@@ -723,7 +724,7 @@ async function addOverrides(
723724 } )
724725 }
725726 if ( state . added . size > 0 || state . updated . size > 0 ) {
726- editablePkgJson . update ( < NPMCliPackageJson > Object . fromEntries ( depEntries ) )
727+ editablePkgJson . update ( < PackageJson > Object . fromEntries ( depEntries ) )
727728 for ( const { overrides, type } of overridesDataObjects ) {
728729 updateManifestByAgent [ type ] ( editablePkgJson , toSortedObject ( overrides ) )
729730 }
0 commit comments