Skip to content

Commit 67e9a0d

Browse files
committed
Cleanup console messages and lock file detection
1 parent be5d8d0 commit 67e9a0d

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/commands/optimize.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type {
2222

2323
const distPath = __dirname
2424

25+
const COMMAND_TITLE = 'Socket Optimize'
2526
const OVERRIDES_FIELD_NAME = 'overrides'
2627
const RESOLUTIONS_FIELD_NAME = 'resolutions'
2728

@@ -230,6 +231,7 @@ export const optimize: CliSubcommand = {
230231
importMeta
231232
)
232233
if (commandContext) {
234+
const cwd = process.cwd()
233235
const {
234236
agent,
235237
agentExecPath,
@@ -242,21 +244,30 @@ export const optimize: CliSubcommand = {
242244
pkgJson,
243245
supported
244246
} = await detect({
245-
cwd: process.cwd(),
247+
cwd,
246248
onUnknown(pkgManager: string | undefined) {
247249
console.log(
248-
`⚠️ Unknown package manager${pkgManager ? ` ${pkgManager}` : ''}: Defaulting to npm`
250+
`⚠️ ${COMMAND_TITLE}: Unknown package manager${pkgManager ? ` ${pkgManager}` : ''}, defaulting to npm`
249251
)
250252
}
251253
})
252254
if (!supported) {
253-
console.log('✘ The engines.node range is not supported.')
255+
console.log(`✘ ${COMMAND_TITLE}: Package engines.node range is not supported`)
256+
return
257+
}
258+
const lockName = lockPath ? path.basename(lockPath) : 'lock file'
259+
if (lockSrc === undefined) {
260+
console.log(`✘ ${COMMAND_TITLE}: No ${lockName} found`)
254261
return
255262
}
256263
if (pkgJson === undefined) {
257-
console.log('✘ No package.json found.')
264+
console.log(`✘ ${COMMAND_TITLE}: No package.json found`)
258265
return
259266
}
267+
if (lockPath && path.relative(cwd, lockPath).startsWith('.')) {
268+
console.log(`⚠️ ${COMMAND_TITLE}: Package ${lockName} found at ${lockPath}`)
269+
}
270+
260271
const aoState: AddOverridesState = {
261272
output: pkgJsonStr!,
262273
packageNames: new Set()
@@ -288,7 +299,6 @@ export const optimize: CliSubcommand = {
288299
console.log('Congratulations! Already Socket.dev optimized 🎉')
289300
}
290301

291-
const lockName = lockPath ? path.basename(lockPath) : 'lock file'
292302
const isNpm = agent === 'npm'
293303
if (isNpm || count) {
294304
// Always update package-lock.json until the npm overrides PR lands:
@@ -311,12 +321,12 @@ export const optimize: CliSubcommand = {
311321
spinner.stop()
312322
if (isNpm) {
313323
console.log(
314-
`💡 Re-run Socket Optimize whenever ${lockName} changes.\n This can be skipped once npm ships https://github.com/npm/cli/pull/7025.`
324+
`💡 Re-run ${COMMAND_TITLE} whenever ${lockName} changes.\n This can be skipped once npm ships https://github.com/npm/cli/pull/7025.`
315325
)
316326
}
317327
} catch {
318328
spinner.stop()
319-
console.log(`✘ socket ${agent} install: Failed to update ${lockName}`)
329+
console.log(`✘ ${COMMAND_TITLE}: ${agent} install failed to update ${lockName}`)
320330
}
321331
}
322332
}

src/utils/package-manager-detector.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,27 +108,23 @@ const readLockFileByAgent: Record<AgentPlusBun, ReadLockFile> = (() => {
108108
})()
109109

110110
export async function detect({
111-
cwd,
111+
cwd = process.cwd(),
112112
onUnknown
113113
}: DetectOptions = {}): Promise<DetectResult> {
114-
const lockPath = await findUp(Object.keys(LOCKS), { cwd })
114+
let lockPath = await findUp(Object.keys(LOCKS), { cwd })
115115
const isHiddenLockFile = lockPath?.endsWith('.package-lock.json') ?? false
116-
117116
const pkgJsonPath = lockPath
118117
? path.resolve(lockPath, `${isHiddenLockFile ? '../' : ''}../package.json`)
119118
: await findUp('package.json', { cwd })
120-
121119
// Read Corepack `packageManager` field in package.json:
122120
// https://nodejs.org/api/packages.html#packagemanager
123121
const pkgJsonStr = existsSync(pkgJsonPath)
124122
? await readFileUtf8(pkgJsonPath)
125123
: undefined
126-
127124
const pkgJson =
128125
typeof pkgJsonStr === 'string'
129126
? (parseJSONObject(pkgJsonStr) ?? undefined)
130127
: undefined
131-
132128
const pkgManager = <string | undefined>(
133129
(isNonEmptyString(getOwn(pkgJson, 'packageManager'))
134130
? pkgJson?.['packageManager']
@@ -151,6 +147,7 @@ export async function detect({
151147
if (
152148
agent === undefined &&
153149
!isHiddenLockFile &&
150+
typeof pkgJsonPath === 'string' &&
154151
typeof lockPath === 'string'
155152
) {
156153
agent = <AgentPlusBun>LOCKS[path.basename(lockPath)]
@@ -211,6 +208,8 @@ export async function detect({
211208
typeof lockPath === 'string'
212209
? await readLockFileByAgent[agent](lockPath, agentExecPath)
213210
: undefined
211+
} else {
212+
lockPath = undefined
214213
}
215214
return <DetectResult>{
216215
agent,

0 commit comments

Comments
 (0)