Skip to content

Commit 910c8cd

Browse files
committed
Run socket npm install after optimize when agent is npm
1 parent 73d13e0 commit 910c8cd

File tree

3 files changed

+92
-52
lines changed

3 files changed

+92
-52
lines changed

src/commands/optimize.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import meow from 'meow'
1+
import path from 'node:path'
2+
3+
import spawn from '@npmcli/promise-spawn'
24
import { getManifestData } from '@socketsecurity/registry'
5+
import meow from 'meow'
36

47
import { printFlagList } from '../utils/formatting'
58
import { writeFileUtf8 } from '../utils/fs'
@@ -17,6 +20,8 @@ import type {
1720
StringKeyValueObject
1821
} from '../utils/package-manager-detector'
1922

23+
const distPath = __dirname
24+
2025
const OVERRIDES_FIELD_NAME = 'overrides'
2126

2227
const RESOLUTIONS_FIELD_NAME = 'resolutions'
@@ -54,17 +59,22 @@ type CreateManifest = (
5459

5560
const createManifestByAgent: Record<Agent, CreateManifest> = {
5661
npm: (ref: PackageJSONObject, overrides: Overrides) =>
57-
<PackageJSONObject>{ ...ref, overrides },
62+
(<unknown>{ __proto__: null, ...ref, overrides }) as PackageJSONObject,
5863
pnpm: (ref: PackageJSONObject, overrides: Overrides) =>
59-
<PackageJSONObject>{
64+
(<unknown>{
65+
__proto__: null,
6066
...ref,
6167
pnpm: <PackageJSONObject>{
6268
...(<StringKeyValueObject>(ref['pnpm'] ?? {})),
6369
overrides
6470
}
65-
},
71+
}) as PackageJSONObject,
6672
yarn: (ref: PackageJSONObject, overrides: Overrides) =>
67-
<PackageJSONObject>{ ...ref, resolutions: overrides }
73+
(<unknown>{
74+
__proto__: null,
75+
...ref,
76+
resolutions: overrides
77+
}) as PackageJSONObject
6878
} as const
6979

7080
type LockIncludes = (lockSrc: string, name: string) => boolean
@@ -253,7 +263,10 @@ async function addOverrides(
253263
for (const name of allPackages) {
254264
if (!hasOwn(overrides, name) && lockIncludes(lockSrc, name)) {
255265
if (clonedOverrides === undefined) {
256-
clonedOverrides = { ...overrides }
266+
clonedOverrides = (<unknown>{
267+
__proto__: null,
268+
...overrides
269+
}) as Overrides
257270
}
258271
addedCount += 1
259272
packageNames.add(name)
@@ -289,8 +302,6 @@ export const optimize: CliSubcommand = {
289302
importMeta
290303
)
291304
if (commandContext) {
292-
//const spinnerText = 'Searching dependencies...'
293-
//const spinner = ora(spinnerText).start()
294305
const { agent, lockSrc, pkgJson, pkgPath, pkgJsonStr, supported } =
295306
await detect({
296307
cwd: process.cwd(),
@@ -342,6 +353,7 @@ export const optimize: CliSubcommand = {
342353
for (const config of configs) {
343354
await addOverrides(
344355
<AddOverridesConfig>{
356+
__proto__: null,
345357
lockSrc,
346358
pkgPath,
347359
pkgJson,
@@ -351,6 +363,17 @@ export const optimize: CliSubcommand = {
351363
aoState
352364
)
353365
}
366+
if (agent === 'npm') {
367+
const wrapperPath = path.join(distPath, 'npm-cli.js')
368+
await spawn(process.execPath, [wrapperPath, ...argv], {
369+
stdio: 'inherit',
370+
env: (<unknown>{
371+
__proto__: null,
372+
...process.env,
373+
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: '1'
374+
}) as NodeJS.ProcessEnv
375+
})
376+
}
354377
}
355378
const { size: count } = aoState.packageNames
356379
if (count) {

src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
function envAsBoolean(value: any): boolean {
2+
return (
3+
typeof value === 'string' &&
4+
(value === '1' || value.toLowerCase() === 'true')
5+
)
6+
}
7+
18
export const API_V0_URL = 'https://api.socket.dev/v0'
9+
10+
export const ENV = Object.freeze({
11+
// Flag set by the optimize command to bypass the packagesHaveRiskyIssues check.
12+
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: envAsBoolean(
13+
process.env['UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE']
14+
)
15+
})

src/shadow/arborist.ts

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type {
2929
} from '@npmcli/arborist'
3030
import type { Writable } from 'node:stream'
3131
import type { Options as OraOptions } from 'ora'
32-
import { API_V0_URL } from '../constants'
32+
import { API_V0_URL, ENV } from '../constants'
3333

3434
type ArboristClass = typeof BaseArborist & {
3535
new (...args: any): typeof BaseArborist
@@ -894,53 +894,56 @@ export class SafeArborist extends Arborist {
894894
) {
895895
return await this[kRiskyReify](...args)
896896
}
897-
const proceed = await ttyServer.captureTTY(
898-
async (colorLevel, input, output) => {
899-
chalk.level = colorLevel
900-
if (input && output) {
901-
const risky = await packagesHaveRiskyIssues(
902-
this,
903-
this['registry'],
904-
diff,
905-
output
906-
)
907-
if (!risky) {
908-
return true
909-
}
910-
const rlin = new PassThrough()
911-
input.pipe(rlin)
912-
const rlout = new PassThrough()
913-
rlout.pipe(output, { end: false })
914-
const rli = rl.createInterface(rlin, rlout)
915-
try {
916-
while (true) {
917-
const answer: string = await new Promise(resolve => {
918-
rli.question(
919-
'Accept risks of installing these packages (y/N)?\n',
920-
{ signal: abortSignal },
921-
resolve
922-
)
923-
})
924-
if (/^\s*y(?:es)?\s*$/i.test(answer)) {
925-
return true
926-
}
927-
if (/^(?:\s*no?\s*|)$/i.test(answer)) {
928-
return false
897+
let proceed = ENV.UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE
898+
if (!proceed) {
899+
proceed = await ttyServer.captureTTY(
900+
async (colorLevel, input, output) => {
901+
chalk.level = colorLevel
902+
if (input && output) {
903+
const risky = await packagesHaveRiskyIssues(
904+
this,
905+
this['registry'],
906+
diff,
907+
output
908+
)
909+
if (!risky) {
910+
return true
911+
}
912+
const rlin = new PassThrough()
913+
input.pipe(rlin)
914+
const rlout = new PassThrough()
915+
rlout.pipe(output, { end: false })
916+
const rli = rl.createInterface(rlin, rlout)
917+
try {
918+
while (true) {
919+
const answer: string = await new Promise(resolve => {
920+
rli.question(
921+
'Accept risks of installing these packages (y/N)?\n',
922+
{ signal: abortSignal },
923+
resolve
924+
)
925+
})
926+
if (/^\s*y(?:es)?\s*$/i.test(answer)) {
927+
return true
928+
}
929+
if (/^(?:\s*no?\s*|)$/i.test(answer)) {
930+
return false
931+
}
929932
}
933+
} finally {
934+
rli.close()
930935
}
931-
} finally {
932-
rli.close()
936+
} else if (
937+
await packagesHaveRiskyIssues(this, this['registry'], diff, output)
938+
) {
939+
throw new Error(
940+
'Socket npm Unable to prompt to accept risk, need TTY to do so'
941+
)
933942
}
934-
} else if (
935-
await packagesHaveRiskyIssues(this, this['registry'], diff, output)
936-
) {
937-
throw new Error(
938-
'Socket npm Unable to prompt to accept risk, need TTY to do so'
939-
)
943+
return true
940944
}
941-
return true
942-
}
943-
)
945+
)
946+
}
944947
if (proceed) {
945948
return await this[kRiskyReify](...args)
946949
} else {

0 commit comments

Comments
 (0)