@@ -8,13 +8,11 @@ import { getManifestData } from '@socketsecurity/registry'
8
8
import meow from 'meow'
9
9
import npa from 'npm-package-arg'
10
10
import ora from 'ora'
11
- import pacote from 'pacote'
12
11
import semver from 'semver'
13
12
import { glob as tinyGlob } from 'tinyglobby'
14
13
import { parse as yamlParse } from 'yaml'
15
14
16
- //import { packumentCache, pacoteCachePath } from '../constants'
17
- import { packumentCache } from '../constants'
15
+ import { fetchPackageManifest } from '@socketsecurity/registry/lib/packages'
18
16
import { commonFlags } from '../flags'
19
17
import { printFlagList } from '../utils/formatting'
20
18
import { existsSync } from '../utils/fs'
@@ -24,10 +22,9 @@ import { pEach } from '@socketsecurity/registry/lib/promises'
24
22
import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
25
23
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
26
24
27
- import type { Content as PackageJsonContent } from '@npmcli/package-json'
25
+ import type { Content as NPMCliPackageJson } from '@npmcli/package-json'
28
26
import type { ManifestEntry } from '@socketsecurity/registry'
29
27
import type { Ora } from 'ora'
30
- import type { PacoteOptions } from 'pacote'
31
28
import type { CliSubcommand } from '../utils/meow-with-subcommands'
32
29
import type {
33
30
Agent ,
@@ -45,42 +42,42 @@ const manifestNpmOverrides = getManifestData('npm')!
45
42
type NpmOverrides = { [ key : string ] : string | StringKeyValueObject }
46
43
type PnpmOrYarnOverrides = { [ key : string ] : string }
47
44
type Overrides = NpmOverrides | PnpmOrYarnOverrides
48
- type GetOverrides = ( pkgJson : PackageJsonContent ) => GetOverridesResult
45
+ type GetOverrides = ( pkgJson : NPMCliPackageJson ) => GetOverridesResult
49
46
type GetOverridesResult = {
50
47
type : Agent
51
48
overrides : Overrides
52
49
}
53
50
54
51
const getOverridesDataByAgent : Record < Agent , GetOverrides > = {
55
- bun ( pkgJson : PackageJsonContent ) {
52
+ bun ( pkgJson : NPMCliPackageJson ) {
56
53
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
57
54
return { type : 'yarn/berry' , overrides }
58
55
} ,
59
56
// npm overrides documentation:
60
57
// https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides
61
- npm ( pkgJson : PackageJsonContent ) {
58
+ npm ( pkgJson : NPMCliPackageJson ) {
62
59
const overrides = ( pkgJson as any ) ?. overrides ?? { }
63
60
return { type : 'npm' , overrides }
64
61
} ,
65
62
// pnpm overrides documentation:
66
63
// https://pnpm.io/package_json#pnpmoverrides
67
- pnpm ( pkgJson : PackageJsonContent ) {
64
+ pnpm ( pkgJson : NPMCliPackageJson ) {
68
65
const overrides = ( pkgJson as any ) ?. pnpm ?. overrides ?? { }
69
66
return { type : 'pnpm' , overrides }
70
67
} ,
71
- vlt ( pkgJson : PackageJsonContent ) {
68
+ vlt ( pkgJson : NPMCliPackageJson ) {
72
69
const overrides = ( pkgJson as any ) ?. overrides ?? { }
73
70
return { type : 'vlt' , overrides }
74
71
} ,
75
72
// Yarn resolutions documentation:
76
73
// https://yarnpkg.com/configuration/manifest#resolutions
77
- 'yarn/berry' ( pkgJson : PackageJsonContent ) {
74
+ 'yarn/berry' ( pkgJson : NPMCliPackageJson ) {
78
75
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
79
76
return { type : 'yarn/berry' , overrides }
80
77
} ,
81
78
// Yarn resolutions documentation:
82
79
// https://classic.yarnpkg.com/en/docs/selective-version-resolutions
83
- 'yarn/classic' ( pkgJson : PackageJsonContent ) {
80
+ 'yarn/classic' ( pkgJson : NPMCliPackageJson ) {
84
81
const overrides = ( pkgJson as any ) ?. resolutions ?? { }
85
82
return { type : 'yarn/classic' , overrides }
86
83
}
@@ -335,7 +332,7 @@ function createActionMessage(
335
332
return `${ verb } ${ overrideCount } Socket.dev optimized overrides${ workspaceCount ? ` in ${ workspaceCount } workspace${ workspaceCount > 1 ? 's' : '' } ` : '' } `
336
333
}
337
334
338
- function getDependencyEntries ( pkgJson : PackageJsonContent ) {
335
+ function getDependencyEntries ( pkgJson : NPMCliPackageJson ) {
339
336
const {
340
337
dependencies,
341
338
devDependencies,
@@ -367,7 +364,7 @@ function getDependencyEntries(pkgJson: PackageJsonContent) {
367
364
async function getWorkspaceGlobs (
368
365
agent : Agent ,
369
366
pkgPath : string ,
370
- pkgJson : PackageJsonContent
367
+ pkgJson : NPMCliPackageJson
371
368
) : Promise < string [ ] | undefined > {
372
369
let workspacePatterns
373
370
if ( agent === 'pnpm' ) {
@@ -471,7 +468,7 @@ async function addOverrides(
471
468
editablePkgJson = await EditablePackageJson . load ( pkgPath )
472
469
}
473
470
const { spinner } = state
474
- const pkgJson : Readonly < PackageJsonContent > = editablePkgJson . content
471
+ const pkgJson : Readonly < NPMCliPackageJson > = editablePkgJson . content
475
472
const isRoot = pkgPath === rootPath
476
473
const isLockScanned = isRoot && ! prod
477
474
const workspaceName = path . relative ( rootPath , pkgPath )
@@ -621,7 +618,7 @@ async function addOverrides(
621
618
} )
622
619
}
623
620
if ( state . added . size > 0 || state . updated . size > 0 ) {
624
- editablePkgJson . update ( < PackageJsonContent > Object . fromEntries ( depEntries ) )
621
+ editablePkgJson . update ( < NPMCliPackageJson > Object . fromEntries ( depEntries ) )
625
622
for ( const { overrides, type } of overridesDataObjects ) {
626
623
updateManifestByAgent [ type ] ( editablePkgJson , toSortedObject ( overrides ) )
627
624
}
@@ -630,61 +627,6 @@ async function addOverrides(
630
627
return state
631
628
}
632
629
633
- // type ExtractOptions = pacote.Options & {
634
- // tmpPrefix?: string
635
- // [key: string]: any
636
- // }
637
-
638
- // async function extractPackage(pkgNameOrId: string, options: ExtractOptions | undefined, callback: (tmpDirPath: string) => any) {
639
- // if (arguments.length === 2 && typeof options === 'function') {
640
- // callback = options
641
- // options = undefined
642
- // }
643
- // const { tmpPrefix, ...extractOptions } = { __proto__: null, ...options }
644
- // // cacache.tmp.withTmp DOES return a promise.
645
- // await cacache.tmp.withTmp(
646
- // pacoteCachePath,
647
- // { tmpPrefix },
648
- // // eslint-disable-next-line @typescript-eslint/no-misused-promises
649
- // async tmpDirPath => {
650
- // await pacote.extract(pkgNameOrId, tmpDirPath, {
651
- // __proto__: null,
652
- // packumentCache,
653
- // preferOffline: true,
654
- // ...<Omit<typeof extractOptions, '__proto__'>>extractOptions
655
- // })
656
- // await callback(tmpDirPath)
657
- // }
658
- // )
659
- // }
660
-
661
- type FetchPackageManifestOptions = {
662
- signal ?: AbortSignal
663
- }
664
-
665
- async function fetchPackageManifest (
666
- pkgNameOrId : string ,
667
- options ?: FetchPackageManifestOptions
668
- ) {
669
- const pacoteOptions = < PacoteOptions & { signal ?: AbortSignal } > {
670
- ...options ,
671
- packumentCache,
672
- preferOffline : true
673
- }
674
- const { signal } = pacoteOptions
675
- if ( signal ?. aborted ) {
676
- return null
677
- }
678
- let result
679
- try {
680
- result = await pacote . manifest ( pkgNameOrId , pacoteOptions )
681
- } catch { }
682
- if ( signal ?. aborted ) {
683
- return null
684
- }
685
- return result
686
- }
687
-
688
630
export const optimize : CliSubcommand = {
689
631
description : 'Optimize dependencies with @socketregistry overrides' ,
690
632
async run ( argv , importMeta , { parentName } ) {
0 commit comments