Skip to content

Commit e96eab7

Browse files
committed
Auto update lock files after adding overrides
1 parent 0e83793 commit e96eab7

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/commands/optimize.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,11 @@ export const optimize: CliSubcommand = {
200200
if (commandContext) {
201201
const {
202202
agent,
203+
agentExecPath,
203204
isPrivate,
204205
isWorkspace,
205206
lockSrc,
207+
lockPath,
206208
pkgJsonPath,
207209
pkgJsonStr,
208210
pkgJson,
@@ -273,10 +275,19 @@ export const optimize: CliSubcommand = {
273275
const { size: count } = aoState.packageNames
274276
if (count) {
275277
console.log(`Added ${count} Socket.dev optimized overrides 🚀`)
276-
if (agent === 'npm') {
277-
const spinner = ora('Updating package-lock.json...').start()
278-
const wrapperPath = path.join(distPath, 'npm-cli.js')
279-
try {
278+
} else {
279+
console.log('Congratulations! Already Socket.dev optimized 🎉')
280+
}
281+
282+
const lockName = lockPath ? path.basename(lockPath) : 'lock file'
283+
const isNpm = agent === 'npm'
284+
if (isNpm || count) {
285+
// Always update package-lock.json until the npm overrides PR lands:
286+
// https://github.com/npm/cli/pull/7025
287+
const spinner = ora(`Updating ${lockName}...`).start()
288+
try {
289+
if (isNpm) {
290+
const wrapperPath = path.join(distPath, 'npm-cli.js')
280291
await spawn(process.execPath, [wrapperPath, 'install'], {
281292
stdio: 'pipe',
282293
env: (<unknown>{
@@ -285,15 +296,14 @@ export const optimize: CliSubcommand = {
285296
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: '1'
286297
}) as NodeJS.ProcessEnv
287298
})
288-
} catch {
289-
console.log(
290-
'✘ socket npm install: Failed to update package-lock.json'
291-
)
299+
} else {
300+
await spawn(agentExecPath, ['install'], { stdio: 'pipe' })
292301
}
293302
spinner.stop()
303+
} catch {
304+
spinner.stop()
305+
console.log(`✘ socket ${agent} install: Failed to update ${lockName}`)
294306
}
295-
} else {
296-
console.log('Congratulations! Already Socket.dev optimized 🎉')
297307
}
298308
}
299309
}

src/utils/package-manager-detector.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { parse as parseBunLockb } from '@socketregistry/hyrious__bun.lockb'
44
import spawn from '@npmcli/promise-spawn'
55
import browserslist from 'browserslist'
66
import semver from 'semver'
7+
import which from 'which'
78

89
import { existsSync, findUp, readFileBinary, readFileUtf8 } from './fs'
910
import { parseJSONObject } from './json'
@@ -52,6 +53,7 @@ export type DetectOptions = {
5253

5354
export type DetectResult = Readonly<{
5455
agent: AgentPlusBun
56+
agentExecPath: string
5557
agentVersion: string | undefined
5658
isPrivate: boolean
5759
isWorkspace: boolean
@@ -67,19 +69,27 @@ export type DetectResult = Readonly<{
6769
}
6870
}>
6971

70-
type ReadLockFile = (lockPath: string) => Promise<string | undefined>
72+
type ReadLockFile = (
73+
lockPath: string,
74+
agentExecPath?: string
75+
) => Promise<string | undefined>
7176

7277
const readLockFileByAgent: Record<AgentPlusBun, ReadLockFile> = (() => {
7378
const wrapReader =
74-
(reader: (lockPath: string) => Promise<string | undefined>): ReadLockFile =>
75-
async (lockPath: string) => {
79+
(
80+
reader: (
81+
lockPath: string,
82+
agentExecPath?: string
83+
) => Promise<string | undefined>
84+
): ReadLockFile =>
85+
async (lockPath: string, agentExecPath?: string) => {
7686
try {
77-
return await reader(lockPath)
87+
return await reader(lockPath, agentExecPath)
7888
} catch {}
7989
return undefined
8090
}
8191
return {
82-
bun: wrapReader(async (lockPath: string) => {
92+
bun: wrapReader(async (lockPath: string, agentExecPath?: string) => {
8393
let lockBuffer: Buffer | undefined
8494
try {
8595
lockBuffer = <Buffer>await readFileBinary(lockPath)
@@ -91,7 +101,7 @@ const readLockFileByAgent: Record<AgentPlusBun, ReadLockFile> = (() => {
91101
} catch {}
92102
// To print a Yarn lockfile to your console without writing it to disk use `bun bun.lockb`.
93103
// https://bun.sh/guides/install/yarnlock
94-
return (await spawn('bun', [lockPath])).stdout
104+
return (await spawn(agentExecPath ?? 'bun', [lockPath])).stdout
95105
}),
96106
npm: wrapReader(async (lockPath: string) => await readFileUtf8(lockPath)),
97107
pnpm: wrapReader(async (lockPath: string) => await readFileUtf8(lockPath)),
@@ -151,6 +161,7 @@ export async function detect({
151161
agent = 'npm'
152162
onUnknown?.(pkgManager)
153163
}
164+
const agentExecPath = (await which(agent, { nothrow: true })) ?? agent
154165

155166
let lockSrc: string | undefined
156167
const targets = {
@@ -167,7 +178,6 @@ export async function detect({
167178
!!pkgJson['workspaces'] ||
168179
(agent === 'pnpm' &&
169180
existsSync(path.join(pkgPath, 'pnpm-workspace.yaml')))
170-
171181
let browser: boolean | undefined
172182
let node: boolean | undefined
173183
const browserField = getOwn(pkgJson, 'browser')
@@ -201,12 +211,12 @@ export async function detect({
201211
}
202212
lockSrc =
203213
typeof lockPath === 'string'
204-
? await readLockFileByAgent[agent](lockPath)
214+
? await readLockFileByAgent[agent](lockPath, agentExecPath)
205215
: undefined
206216
}
207-
208217
return <DetectResult>{
209218
agent,
219+
agentExecPath,
210220
agentVersion,
211221
isPrivate,
212222
isWorkspace,

0 commit comments

Comments
 (0)