1
1
import fs from 'fs/promises'
2
2
import path from 'node:path'
3
3
4
- import EditablePackageJson from '@npmcli/package-json'
5
4
import spawn from '@npmcli/promise-spawn'
6
5
import meow from 'meow'
7
6
import npa from 'npm-package-arg'
@@ -16,7 +15,7 @@ import {
16
15
isObject ,
17
16
toSortedObject
18
17
} from '@socketsecurity/registry/lib/objects'
19
- import { fetchPackageManifest } from '@socketsecurity/registry/lib/packages'
18
+ import { fetchPackageManifest , readPackageJson } from '@socketsecurity/registry/lib/packages'
20
19
import { pEach } from '@socketsecurity/registry/lib/promises'
21
20
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
22
21
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
@@ -31,10 +30,12 @@ import type {
31
30
Agent ,
32
31
StringKeyValueObject
33
32
} from '../utils/package-manager-detector'
34
- import type { Content as NPMCliPackageJson } from '@npmcli/package-json'
35
33
import type { ManifestEntry } from '@socketsecurity/registry'
34
+ import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
36
35
import type { Ora } from 'ora'
37
36
37
+ type PackageJson = Awaited < ReturnType < typeof readPackageJson > >
38
+
38
39
const COMMAND_TITLE = 'Socket Optimize'
39
40
const OVERRIDES_FIELD_NAME = 'overrides'
40
41
const PNPM_FIELD_NAME = 'pnpm'
@@ -47,42 +48,42 @@ const manifestNpmOverrides = getManifestData('npm')!
47
48
type NpmOverrides = { [ key : string ] : string | StringKeyValueObject }
48
49
type PnpmOrYarnOverrides = { [ key : string ] : string }
49
50
type Overrides = NpmOverrides | PnpmOrYarnOverrides
50
- type GetOverrides = ( pkgJson : NPMCliPackageJson ) => GetOverridesResult
51
+ type GetOverrides = ( pkgJson : PackageJson ) => GetOverridesResult
51
52
type GetOverridesResult = {
52
53
type : Agent
53
54
overrides : Overrides
54
55
}
55
56
56
57
const getOverridesDataByAgent : Record < Agent , GetOverrides > = {
57
- bun ( pkgJson : NPMCliPackageJson ) {
58
+ bun ( pkgJson : PackageJson ) {
58
59
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
59
60
return { type : 'yarn/berry' , overrides }
60
61
} ,
61
62
// npm overrides documentation:
62
63
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
63
- npm ( pkgJson : NPMCliPackageJson ) {
64
+ npm ( pkgJson : PackageJson ) {
64
65
const overrides = ( pkgJson as any ) ?. overrides ?? { }
65
66
return { type : 'npm' , overrides }
66
67
} ,
67
68
// pnpm overrides documentation:
68
69
// https://pnpm.io/package_json#pnpmoverrides
69
- pnpm ( pkgJson : NPMCliPackageJson ) {
70
+ pnpm ( pkgJson : PackageJson ) {
70
71
const overrides = ( pkgJson as any ) ?. pnpm ?. overrides ?? { }
71
72
return { type : 'pnpm' , overrides }
72
73
} ,
73
- vlt ( pkgJson : NPMCliPackageJson ) {
74
+ vlt ( pkgJson : PackageJson ) {
74
75
const overrides = ( pkgJson as any ) ?. overrides ?? { }
75
76
return { type : 'vlt' , overrides }
76
77
} ,
77
78
// Yarn resolutions documentation:
78
79
// https://yarnpkg.com/configuration/manifest#resolutions
79
- 'yarn/berry' ( pkgJson : NPMCliPackageJson ) {
80
+ 'yarn/berry' ( pkgJson : PackageJson ) {
80
81
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
81
82
return { type : 'yarn/berry' , overrides }
82
83
} ,
83
84
// Yarn resolutions documentation:
84
85
// https://classic.yarnpkg.com/en/docs/selective-version-resolutions
85
- 'yarn/classic' ( pkgJson : NPMCliPackageJson ) {
86
+ 'yarn/classic' ( pkgJson : PackageJson ) {
86
87
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
87
88
return { type : 'yarn/classic' , overrides }
88
89
}
@@ -182,7 +183,7 @@ const updateManifestByAgent: Record<Agent, AgentModifyManifestFn> = (() => {
182
183
if ( oldValue ) {
183
184
// The field already exists so we simply update the field value.
184
185
if ( field === PNPM_FIELD_NAME ) {
185
- editablePkgJson . update ( {
186
+ editablePkgJson [ ' update' ] ( {
186
187
[ field ] : {
187
188
...( isObject ( oldValue ) ? oldValue : { } ) ,
188
189
overrides : value
@@ -437,7 +438,7 @@ function createActionMessage(
437
438
return `${ verb } ${ overrideCount } Socket.dev optimized overrides${ workspaceCount ? ` in ${ workspaceCount } workspace${ workspaceCount > 1 ? 's' : '' } ` : '' } `
438
439
}
439
440
440
- function getDependencyEntries ( pkgJson : NPMCliPackageJson ) {
441
+ function getDependencyEntries ( pkgJson : PackageJson ) {
441
442
const {
442
443
dependencies,
443
444
devDependencies,
@@ -469,7 +470,7 @@ function getDependencyEntries(pkgJson: NPMCliPackageJson) {
469
470
async function getWorkspaceGlobs (
470
471
agent : Agent ,
471
472
pkgPath : string ,
472
- pkgJson : NPMCliPackageJson
473
+ pkgJson : PackageJson
473
474
) : Promise < string [ ] | undefined > {
474
475
let workspacePatterns
475
476
if ( agent === 'pnpm' ) {
@@ -570,10 +571,10 @@ async function addOverrides(
570
571
state = createAddOverridesState ( )
571
572
) : Promise < AddOverridesState > {
572
573
if ( editablePkgJson === undefined ) {
573
- editablePkgJson = await EditablePackageJson . load ( pkgPath )
574
+ editablePkgJson = await readPackageJson ( pkgPath , { editable : true } )
574
575
}
575
576
const { spinner } = state
576
- const pkgJson : Readonly < NPMCliPackageJson > = editablePkgJson . content
577
+ const { content : pkgJson } = editablePkgJson
577
578
const isRoot = pkgPath === rootPath
578
579
const isLockScanned = isRoot && ! prod
579
580
const workspaceName = path . relative ( rootPath , pkgPath )
@@ -723,7 +724,7 @@ async function addOverrides(
723
724
} )
724
725
}
725
726
if ( state . added . size > 0 || state . updated . size > 0 ) {
726
- editablePkgJson . update ( < NPMCliPackageJson > Object . fromEntries ( depEntries ) )
727
+ editablePkgJson . update ( < PackageJson > Object . fromEntries ( depEntries ) )
727
728
for ( const { overrides, type } of overridesDataObjects ) {
728
729
updateManifestByAgent [ type ] ( editablePkgJson , toSortedObject ( overrides ) )
729
730
}
0 commit comments