-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcmd-patch-apply.mts
More file actions
138 lines (118 loc) · 3.66 KB
/
cmd-patch-apply.mts
File metadata and controls
138 lines (118 loc) · 3.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { existsSync } from 'node:fs'
import path from 'node:path'
import { arrayUnique } from '@socketsecurity/lib/arrays'
import { DOT_SOCKET_DIR } from '@socketsecurity/lib/paths/dirnames'
import { MANIFEST_JSON } from '@socketsecurity/lib/paths/filenames'
import { getDefaultSpinner } from '@socketsecurity/lib/spinner'
import { handlePatchApply } from './handle-patch-apply.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { InputError } from '../../utils/error/errors.mjs'
import {
getFlagApiRequirementsOutput,
getFlagListOutput,
} from '../../utils/output/formatting.mts'
import { getOutputKind } from '../../utils/output/mode.mjs'
import { cmdFlagValueToArray } from '../../utils/process/cmd.mts'
import { getPurlObject } from '../../utils/purl/parse.mjs'
import { checkCommandInput } from '../../utils/validation/check-input.mts'
import type {
CliCommandConfig,
CliCommandContext,
CliSubcommand,
} from '../../utils/cli/with-subcommands.mjs'
import type { PurlObject } from '../../utils/purl/parse.mjs'
import type { PackageURL } from '@socketregistry/packageurl-js'
export const CMD_NAME = 'apply'
const description = 'Apply CVE patches to dependencies'
export const cmdPatchApply: CliSubcommand = {
description,
hidden: false,
run,
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: CliCommandContext,
): Promise<void> {
const config: CliCommandConfig = {
commandName: CMD_NAME,
description,
hidden: false,
flags: {
...commonFlags,
...outputFlags,
purl: {
type: 'string',
default: [],
description:
'Specify purls to patch, as either a comma separated value or as multiple flags',
isMultiple: true,
shortFlag: 'p',
},
},
help: (command, config) => `
Usage
$ ${command} [options] [CWD=.]
API Token Requirements
${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)}
Options
${getFlagListOutput(config.flags)}
Examples
$ ${command}
$ ${command} --purl pkg:npm/lodash@4.17.21
$ ${command} ./path/to/project --purl pkg:npm/lodash@4.17.21,pkg:npm/react@18.0.0
`,
}
const cli = meowOrExit(
{
argv,
config,
parentName,
importMeta,
},
{ allowUnknownFlags: false },
)
const { dryRun, json, markdown } = cli.flags as unknown as {
dryRun: boolean
json: boolean
markdown: boolean
}
const outputKind = getOutputKind(json, markdown)
const wasValidInput = checkCommandInput(outputKind, {
nook: true,
test: !json || !markdown,
message: 'The json and markdown flags cannot be both set, pick one',
fail: 'omit one',
})
if (!wasValidInput) {
return
}
let [cwd = '.'] = cli.input
// Note: path.resolve vs .join:
// If given path is absolute then cwd should not affect it.
cwd = path.resolve(process.cwd(), cwd)
const dotSocketDirPath = path.join(cwd, DOT_SOCKET_DIR)
if (!existsSync(dotSocketDirPath)) {
throw new InputError(
`No ${DOT_SOCKET_DIR} directory found in current directory`,
)
}
const manifestPath = path.join(dotSocketDirPath, MANIFEST_JSON)
if (!existsSync(manifestPath)) {
throw new InputError(
`No ${MANIFEST_JSON} found in ${DOT_SOCKET_DIR} directory`,
)
}
const spinner = getDefaultSpinner()
const purlObjs = arrayUnique(cmdFlagValueToArray(cli.flags['purl']))
.map(p => getPurlObject(p, { throws: false }))
.filter(Boolean) as Array<PurlObject<PackageURL>>
await handlePatchApply({
cwd,
dryRun,
outputKind,
purlObjs,
spinner,
})
}