-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathcmd-patch.mts
More file actions
95 lines (78 loc) · 2.69 KB
/
Copy pathcmd-patch.mts
File metadata and controls
95 lines (78 loc) · 2.69 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { commonFlags } from '../../flags.mjs'
import { meowOrExit } from '../../util/cli/with-subcommands.mjs'
import { spawnSocketPatch } from '../../core/patch/spawn.mts'
import { outputDryRunExecute } from '../../util/dry-run/output.mjs'
import { filterFlags } from '../../util/process/cmd.mjs'
import type {
CliCommandContext,
CliSubcommand,
} from '../../util/cli/with-subcommands.mjs'
export const CMD_NAME = 'patch'
const description = 'Manage CVE patches for dependencies'
const hidden = false
export const cmdPatch: CliSubcommand = {
description,
hidden,
run,
}
export async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
context: CliCommandContext,
): Promise<void> {
const { parentName } = { __proto__: null, ...context } as CliCommandContext
// Strip Socket CLI global flags (--config, --dry-run, banner/header knobs)
// before forwarding — socket-patch is a strict clap CLI that exits 2 on any
// unknown flag. --help survives so `patch <sub> --help` reaches socket-patch.
const forwardArgs = filterFlags(argv, commonFlags, ['--help', '-h'])
const dryRun = argv.includes('--dry-run')
// Check if there are any non-flag arguments (subcommands). Detect on the
// filtered argv so a flag VALUE (e.g. the JSON payload of --config) does not
// count as a subcommand.
const hasSubcommand = forwardArgs.some(arg => !arg.startsWith('-'))
// Only show Socket CLI help if no subcommand is provided.
// If a subcommand is present (like 'list', 'info'), forward to socket-patch.
if (!hasSubcommand) {
const config = {
commandName: CMD_NAME,
description,
hidden,
flags: {},
help: (command: string) => `
Usage
$ ${command} ...
Note: All arguments are forwarded to socket-patch.
Examples
$ ${command} list
$ ${command} get <package>
$ ${command} apply
`,
}
// Parse arguments to handle --help for patch-level help (exits 0).
const cli = meowOrExit({
argv,
config,
importMeta,
parentName,
})
// No subcommand and no --help: missing input, show help and exit 2
// matching the with-subcommands convention.
cli.showHelp(2)
}
if (dryRun) {
outputDryRunExecute('socket-patch', forwardArgs, 'socket-patch')
return
}
process.exitCode = 1
// Forward the remaining arguments to socket-patch via DLX.
const { spawnPromise } = await spawnSocketPatch(forwardArgs, {
stdio: 'inherit',
})
// Wait for the spawn to complete and set exit code.
const result = await spawnPromise
if (result.code != null && result.code !== 0) {
process.exitCode = result.code
} else if (result.code === 0) {
process.exitCode = 0
}
}