-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathcmd-pip.mts
More file actions
50 lines (45 loc) · 1.66 KB
/
Copy pathcmd-pip.mts
File metadata and controls
50 lines (45 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Socket pip command — forwards pip operations to Socket Firewall (sfw).
*
* Defined via `defineHandoffCommand`. The pip-specific binary picker (pip ↔
* pip3 with auto-fallback when one is missing) is wired through the factory's
* `binaryPicker` hook.
*
* See util/cli/define-handoff.mts.
*/
import { whichReal } from '@socketsecurity/lib-stable/exe/path/which'
import { defineHandoffCommand } from '../../util/cli/define-handoff.mts'
/**
* Determine the pip binary name to use based on invocation and availability.
*
* Priority: 1. If invoked as `socket pip3`, use 'pip3'. 2. If invoked as
* `socket pip`, use 'pip' if it exists, else fall back to 'pip3'. 3. If pip3
* was requested but is missing, fall back to 'pip'.
*
* If neither binary is available, return the originally-requested name and let
* the spawn fail naturally — the user gets a recognizable PATH error instead of
* an opaque fallback.
*
* @param invokedAs - The alias name used to invoke the command (e.g., 'pip3').
*/
export async function getPipBinName(
invokedAs?: string | undefined,
): Promise<string> {
const requested = invokedAs === 'pip3' ? invokedAs : 'pip'
const fallback = requested === 'pip' ? 'pip3' : 'pip'
if (await whichReal(requested, { nothrow: true })) {
return requested
}
if (await whichReal(fallback, { nothrow: true })) {
return fallback
}
return requested
}
export const cmdPip = defineHandoffCommand({
name: 'pip',
description: 'Run pip with Socket Firewall security',
examples: ['install flask', 'install -r requirements.txt', 'list'],
binaryPicker: ctx => getPipBinName(ctx.invokedAs),
trackTelemetry: false,
supportDryRun: false,
})