|
| 1 | +import path from 'node:path' |
| 2 | + |
| 3 | +import { logger } from '@socketsecurity/registry/lib/logger' |
| 4 | + |
| 5 | +import { handlePatch } from './handle-patch.mts' |
| 6 | +import constants from '../../constants.mts' |
| 7 | +import { commonFlags, outputFlags } from '../../flags.mts' |
| 8 | +import { checkCommandInput } from '../../utils/check-input.mts' |
| 9 | +import { getOutputKind } from '../../utils/get-output-kind.mts' |
| 10 | +import { meowOrExit } from '../../utils/meow-with-subcommands.mts' |
| 11 | +import { |
| 12 | + getFlagApiRequirementsOutput, |
| 13 | + getFlagListOutput, |
| 14 | +} from '../../utils/output-formatting.mts' |
| 15 | + |
| 16 | +import type { CliCommandConfig } from '../../utils/meow-with-subcommands.mts' |
| 17 | + |
| 18 | +const { DRY_RUN_NOT_SAVING } = constants |
| 19 | + |
| 20 | +export const CMD_NAME = 'patch' |
| 21 | + |
| 22 | +const description = 'Apply security patches to dependencies' |
| 23 | + |
| 24 | +const hidden = false |
| 25 | + |
| 26 | +export const cmdPatch = { |
| 27 | + description, |
| 28 | + hidden, |
| 29 | + run, |
| 30 | +} |
| 31 | + |
| 32 | +async function run( |
| 33 | + argv: string[] | readonly string[], |
| 34 | + importMeta: ImportMeta, |
| 35 | + { parentName }: { parentName: string }, |
| 36 | +): Promise<void> { |
| 37 | + const config: CliCommandConfig = { |
| 38 | + commandName: CMD_NAME, |
| 39 | + description, |
| 40 | + hidden, |
| 41 | + flags: { |
| 42 | + ...commonFlags, |
| 43 | + ...outputFlags, |
| 44 | + package: { |
| 45 | + type: 'string', |
| 46 | + default: [], |
| 47 | + description: |
| 48 | + 'Specify packages to patch, as either a comma separated value or as multiple flags', |
| 49 | + isMultiple: true, |
| 50 | + shortFlag: 'p', |
| 51 | + }, |
| 52 | + }, |
| 53 | + help: (command, config) => ` |
| 54 | + Usage |
| 55 | + $ ${command} [options] [CWD=.] |
| 56 | +
|
| 57 | + API Token Requirements |
| 58 | + ${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)} |
| 59 | +
|
| 60 | + Options |
| 61 | + ${getFlagListOutput(config.flags)} |
| 62 | +
|
| 63 | + Examples |
| 64 | + $ ${command} |
| 65 | + $ ${command} --package lodash |
| 66 | + $ ${command} ./proj/tree --package lodash,react |
| 67 | + `, |
| 68 | + } |
| 69 | + |
| 70 | + const cli = meowOrExit({ |
| 71 | + allowUnknownFlags: false, |
| 72 | + argv, |
| 73 | + config, |
| 74 | + importMeta, |
| 75 | + parentName, |
| 76 | + }) |
| 77 | + |
| 78 | + const dryRun = !!cli.flags['dryRun'] |
| 79 | + const outputKind = getOutputKind(cli.flags['json'], cli.flags['markdown']) |
| 80 | + |
| 81 | + const wasValidInput = checkCommandInput(outputKind, { |
| 82 | + nook: true, |
| 83 | + test: !cli.flags['json'] || !cli.flags['markdown'], |
| 84 | + message: 'The json and markdown flags cannot be both set, pick one', |
| 85 | + fail: 'omit one', |
| 86 | + }) |
| 87 | + if (!wasValidInput) { |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + if (dryRun) { |
| 92 | + logger.log(DRY_RUN_NOT_SAVING) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + let [cwd = '.'] = cli.input |
| 97 | + // Note: path.resolve vs .join: |
| 98 | + // If given path is absolute then cwd should not affect it. |
| 99 | + cwd = path.resolve(process.cwd(), cwd) |
| 100 | + |
| 101 | + // Lazily access constants.spinner. |
| 102 | + const { spinner } = constants |
| 103 | + |
| 104 | + const packages = Array.isArray(cli.flags['package']) |
| 105 | + ? cli.flags['package'].flatMap(p => String(p).split(',')) |
| 106 | + : String(cli.flags['package'] || '') |
| 107 | + .split(',') |
| 108 | + .filter(Boolean) |
| 109 | + |
| 110 | + await handlePatch({ |
| 111 | + cwd, |
| 112 | + outputKind, |
| 113 | + packages, |
| 114 | + spinner, |
| 115 | + }) |
| 116 | +} |
0 commit comments