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
276export 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
3312export const cmdPatch = {
3413 description,
@@ -38,100 +17,40 @@ export const cmdPatch = {
3817
3918async 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 ( / \/ v 0 \/ ? $ / , '' )
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