Skip to content

Commit 397b0f9

Browse files
committed
Use more @socketsecurity/registry
1 parent b4c70fd commit 397b0f9

File tree

5 files changed

+28
-35
lines changed

5 files changed

+28
-35
lines changed

.dep-stats.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"@apideck/better-ajv-errors": "^0.3.6",
44
"@cyclonedx/cdxgen": "^10.11.0",
55
"@inquirer/prompts": "^7.0.1",
6-
"@npmcli/package-json": "6.0.1",
76
"@npmcli/promise-spawn": "^8.0.2",
87
"@socketregistry/hyrious__bun.lockb": "1.0.4",
98
"@socketsecurity/config": "^2.1.3",
10-
"@socketsecurity/registry": "^1.0.16",
9+
"@socketsecurity/registry": "^1.0.22",
1110
"@socketsecurity/sdk": "^1.3.0",
1211
"blessed": "^0.1.81",
1312
"blessed-contrib": "^4.11.0",
@@ -62,11 +61,10 @@
6261
"@apideck/better-ajv-errors": "^0.3.6",
6362
"@cyclonedx/cdxgen": "^10.11.0",
6463
"@inquirer/prompts": "^7.0.1",
65-
"@npmcli/package-json": "6.0.1",
6664
"@npmcli/promise-spawn": "^8.0.2",
6765
"@socketregistry/hyrious__bun.lockb": "1.0.4",
6866
"@socketsecurity/config": "^2.1.3",
69-
"@socketsecurity/registry": "^1.0.16",
67+
"@socketsecurity/registry": "^1.0.22",
7068
"@socketsecurity/sdk": "^1.3.0",
7169
"blessed": "^0.1.81",
7270
"blessed-contrib": "^4.11.0",

package-lock.json

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@
4242
"@apideck/better-ajv-errors": "^0.3.6",
4343
"@cyclonedx/cdxgen": "^10.11.0",
4444
"@inquirer/prompts": "^7.0.1",
45-
"@npmcli/package-json": "6.0.1",
4645
"@npmcli/promise-spawn": "^8.0.2",
4746
"@socketregistry/hyrious__bun.lockb": "1.0.4",
4847
"@socketsecurity/config": "^2.1.3",
49-
"@socketsecurity/registry": "^1.0.16",
48+
"@socketsecurity/registry": "^1.0.22",
5049
"@socketsecurity/sdk": "^1.3.0",
5150
"blessed": "^0.1.81",
5251
"blessed-contrib": "^4.11.0",
@@ -91,7 +90,6 @@
9190
"@types/mock-fs": "^4.13.4",
9291
"@types/node": "^22.9.0",
9392
"@types/npmcli__arborist": "^5.6.11",
94-
"@types/npmcli__package-json": "^4.0.4",
9593
"@types/npmcli__promise-spawn": "^6.0.3",
9694
"@types/proc-log": "^3.0.4",
9795
"@types/semver": "^7.5.8",

src/commands/optimize.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fs from 'fs/promises'
22
import path from 'node:path'
33

4-
import EditablePackageJson from '@npmcli/package-json'
54
import spawn from '@npmcli/promise-spawn'
65
import meow from 'meow'
76
import 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'
2019
import { pEach } from '@socketsecurity/registry/lib/promises'
2120
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
2221
import { 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'
3533
import type { ManifestEntry } from '@socketsecurity/registry'
34+
import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
3635
import type { Ora } from 'ora'
3736

37+
type PackageJson = Awaited<ReturnType<typeof readPackageJson>>
38+
3839
const COMMAND_TITLE = 'Socket Optimize'
3940
const OVERRIDES_FIELD_NAME = 'overrides'
4041
const PNPM_FIELD_NAME = 'pnpm'
@@ -47,42 +48,42 @@ const manifestNpmOverrides = getManifestData('npm')!
4748
type NpmOverrides = { [key: string]: string | StringKeyValueObject }
4849
type PnpmOrYarnOverrides = { [key: string]: string }
4950
type Overrides = NpmOverrides | PnpmOrYarnOverrides
50-
type GetOverrides = (pkgJson: NPMCliPackageJson) => GetOverridesResult
51+
type GetOverrides = (pkgJson: PackageJson) => GetOverridesResult
5152
type GetOverridesResult = {
5253
type: Agent
5354
overrides: Overrides
5455
}
5556

5657
const 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) {
469470
async 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
}

src/utils/package-manager-detector.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'node:path'
22

3-
import EditablePackageJson from '@npmcli/package-json'
43
import spawn from '@npmcli/promise-spawn'
54
import browserslist from 'browserslist'
65
import semver from 'semver'
@@ -9,11 +8,12 @@ import which from 'which'
98
import { parse as parseBunLockb } from '@socketregistry/hyrious__bun.lockb'
109
import constants from '@socketsecurity/registry/lib/constants'
1110
import { isObjectObject } from '@socketsecurity/registry/lib/objects'
11+
import { readPackageJson } from '@socketsecurity/registry/lib/packages'
1212
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
1313

1414
import { existsSync, findUp, readFileBinary, readFileUtf8 } from './fs'
1515

16-
import type { Content as NPMCliPackageJson } from '@npmcli/package-json'
16+
import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
1717
import type { SemVer } from 'semver'
1818

1919
export const AGENTS = [
@@ -152,10 +152,9 @@ export async function detect({
152152
? path.dirname(pkgJsonPath)
153153
: undefined
154154
const editablePkgJson = pkgPath
155-
? await EditablePackageJson.load(pkgPath)
155+
? await readPackageJson(pkgPath, { editable: true })
156156
: undefined
157-
const pkgJson: Readonly<NPMCliPackageJson> | undefined =
158-
editablePkgJson?.content
157+
const pkgJson = editablePkgJson?.content
159158
// Read Corepack `packageManager` field in package.json:
160159
// https://nodejs.org/api/packages.html#packagemanager
161160
const pkgManager = isNonEmptyString(pkgJson?.packageManager)

0 commit comments

Comments
 (0)