Skip to content

Commit 44655ac

Browse files
Use @socketsecurity/socket-patch for patch command (#987)
* Use @socketsecurity/socket-patch for patch command - Replace inline patch implementation with @socketsecurity/[email protected] - Use runPatch() from socket-patch/run for programmatic invocation - Remove deleted handle-patch.mts, manifest-schema.mts, output-patch-result.mts - Add SOCKET_PATCH_PROXY_URL environment variable support - Forward socket-cli environment to socket-patch options * update lockfile --------- Co-authored-by: John-David Dalton <[email protected]>
1 parent ac9fc49 commit 44655ac

File tree

8 files changed

+48
-1018
lines changed

8 files changed

+48
-1018
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
"@socketsecurity/config": "3.0.1",
124124
"@socketsecurity/registry": "1.1.17",
125125
"@socketsecurity/sdk": "1.4.95",
126+
"@socketsecurity/socket-patch": "1.0.0",
126127
"@types/blessed": "0.1.25",
127128
"@types/cmd-shim": "5.0.2",
128129
"@types/js-yaml": "4.0.9",

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/patch/cmd-patch.mts

Lines changed: 31 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
import { existsSync } from 'node:fs'
2-
import path from 'node:path'
1+
import constants from '../../constants.mts'
2+
import { runPatch } from '@socketsecurity/socket-patch/run'
33

4-
import { arrayUnique } from '@socketsecurity/registry/lib/arrays'
5-
6-
import { handlePatch } from './handle-patch.mts'
7-
import constants, { DOT_SOCKET_DIR, MANIFEST_JSON } from '../../constants.mts'
8-
import { commonFlags, outputFlags } from '../../flags.mts'
9-
import { checkCommandInput } from '../../utils/check-input.mts'
10-
import { cmdFlagValueToArray } from '../../utils/cmd.mts'
11-
import { InputError } from '../../utils/errors.mts'
12-
import { getOutputKind } from '../../utils/get-output-kind.mts'
13-
import { meowOrExit } from '../../utils/meow-with-subcommands.mts'
14-
import {
15-
getFlagApiRequirementsOutput,
16-
getFlagListOutput,
17-
} from '../../utils/output-formatting.mts'
18-
import { getPurlObject } from '../../utils/purl.mts'
19-
20-
import type {
21-
CliCommandConfig,
22-
CliCommandContext,
23-
} from '../../utils/meow-with-subcommands.mts'
24-
import type { PurlObject } from '../../utils/purl.mts'
25-
import type { PackageURL } from '@socketregistry/packageurl-js'
4+
import type { CliCommandContext } from '../../utils/meow-with-subcommands.mts'
265

276
export const CMD_NAME = 'patch'
287

29-
const description = 'Apply CVE patches to dependencies'
8+
const description = 'Manage CVE patches for dependencies'
309

31-
const hidden = true
10+
const hidden = false
3211

3312
export const cmdPatch = {
3413
description,
@@ -38,100 +17,40 @@ export const cmdPatch = {
3817

3918
async function run(
4019
argv: string[] | readonly string[],
41-
importMeta: ImportMeta,
42-
{ parentName }: CliCommandContext,
20+
_importMeta: ImportMeta,
21+
_context: CliCommandContext,
4322
): Promise<void> {
44-
const config: CliCommandConfig = {
45-
commandName: CMD_NAME,
46-
description,
47-
hidden,
48-
flags: {
49-
...commonFlags,
50-
...outputFlags,
51-
purl: {
52-
type: 'string',
53-
default: [],
54-
description:
55-
'Specify purls to patch, as either a comma separated value or as multiple flags',
56-
isMultiple: true,
57-
shortFlag: 'p',
58-
},
59-
},
60-
help: (command, config) => `
61-
Usage
62-
$ ${command} [options] [CWD=.]
23+
const { ENV } = constants
6324

64-
API Token Requirements
65-
${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)}
25+
// Map socket-cli environment to socket-patch options.
26+
// Only include properties with defined values (exactOptionalPropertyTypes).
27+
const options: Parameters<typeof runPatch>[1] = {}
6628

67-
Options
68-
${getFlagListOutput(config.flags)}
69-
70-
Examples
71-
$ ${command}
72-
$ ${command} --package lodash
73-
$ ${command} ./path/to/project --package lodash,react
74-
`,
29+
// Strip /v0/ suffix from API URL if present.
30+
const apiUrl = ENV.SOCKET_CLI_API_BASE_URL?.replace(/\/v0\/?$/, '')
31+
if (apiUrl) {
32+
options.apiUrl = apiUrl
7533
}
76-
77-
const cli = meowOrExit(
78-
{
79-
argv,
80-
config,
81-
parentName,
82-
importMeta,
83-
},
84-
{ allowUnknownFlags: false },
85-
)
86-
87-
const { dryRun, json, markdown } = cli.flags as {
88-
dryRun: boolean
89-
json: boolean
90-
markdown: boolean
34+
if (ENV.SOCKET_CLI_API_TOKEN) {
35+
options.apiToken = ENV.SOCKET_CLI_API_TOKEN
9136
}
92-
93-
const outputKind = getOutputKind(json, markdown)
94-
95-
const wasValidInput = checkCommandInput(outputKind, {
96-
nook: true,
97-
test: !json || !markdown,
98-
message: 'The json and markdown flags cannot be both set, pick one',
99-
fail: 'omit one',
100-
})
101-
if (!wasValidInput) {
102-
return
37+
if (ENV.SOCKET_CLI_ORG_SLUG) {
38+
options.orgSlug = ENV.SOCKET_CLI_ORG_SLUG
10339
}
104-
105-
let [cwd = '.'] = cli.input
106-
// Note: path.resolve vs .join:
107-
// If given path is absolute then cwd should not affect it.
108-
cwd = path.resolve(process.cwd(), cwd)
109-
110-
const dotSocketDirPath = path.join(cwd, DOT_SOCKET_DIR)
111-
if (!existsSync(dotSocketDirPath)) {
112-
throw new InputError(
113-
`No ${DOT_SOCKET_DIR} directory found in current directory`,
114-
)
40+
if (ENV.SOCKET_PATCH_PROXY_URL) {
41+
options.patchProxyUrl = ENV.SOCKET_PATCH_PROXY_URL
11542
}
116-
117-
const manifestPath = path.join(dotSocketDirPath, MANIFEST_JSON)
118-
if (!existsSync(manifestPath)) {
119-
throw new InputError(
120-
`No ${MANIFEST_JSON} found in ${DOT_SOCKET_DIR} directory`,
121-
)
43+
if (ENV.SOCKET_CLI_API_PROXY) {
44+
options.httpProxy = ENV.SOCKET_CLI_API_PROXY
45+
}
46+
if (ENV.SOCKET_CLI_DEBUG) {
47+
options.debug = ENV.SOCKET_CLI_DEBUG
12248
}
12349

124-
const { spinner } = constants
125-
126-
const purlObjs = arrayUnique(cmdFlagValueToArray(cli.flags['purl']))
127-
.map(p => getPurlObject(p, { throws: false }))
128-
.filter(Boolean) as Array<PurlObject<PackageURL>>
50+
// Forward all arguments to socket-patch.
51+
const exitCode = await runPatch([...argv], options)
12952

130-
await handlePatch({
131-
cwd,
132-
dryRun,
133-
outputKind,
134-
purlObjs,
135-
spinner,
136-
})
53+
if (exitCode !== 0) {
54+
process.exitCode = exitCode
55+
}
13756
}

0 commit comments

Comments
 (0)