|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +import chalk from 'chalk' |
| 4 | +import meow from 'meow' |
| 5 | +import ora from 'ora' |
| 6 | + |
| 7 | +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' |
| 8 | +import { ChalkOrMarkdown } from '../../utils/chalk-markdown.js' |
| 9 | +import { InputError } from '../../utils/errors.js' |
| 10 | +import { getSeveritySummary } from '../../utils/format-issues.js' |
| 11 | +import { printFlagList } from '../../utils/formatting.js' |
| 12 | +import { setupSdk } from '../../utils/sdk.js' |
| 13 | + |
| 14 | +/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */ |
| 15 | +export const view = { |
| 16 | + description: 'View a project report', |
| 17 | + async run (argv, importMeta, { parentName }) { |
| 18 | + const name = parentName + ' view' |
| 19 | + |
| 20 | + const input = setupCommand(name, view.description, argv, importMeta) |
| 21 | + const result = input && await fetchReportData(input.reportId) |
| 22 | + |
| 23 | + if (result) { |
| 24 | + formatReportDataOutput(result.data, { name, ...input }) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Internal functions |
| 30 | + |
| 31 | +/** |
| 32 | + * @param {string} name |
| 33 | + * @param {string} description |
| 34 | + * @param {readonly string[]} argv |
| 35 | + * @param {ImportMeta} importMeta |
| 36 | + * @returns {void|{ outputJson: boolean, outputMarkdown: boolean, reportId: string }} |
| 37 | + */ |
| 38 | +function setupCommand (name, description, argv, importMeta) { |
| 39 | + // FIXME: Add examples |
| 40 | + const cli = meow(` |
| 41 | + Usage |
| 42 | + $ ${name} <report-identifier> |
| 43 | +
|
| 44 | + Options |
| 45 | + ${printFlagList({ |
| 46 | + '--json': 'Output result as json', |
| 47 | + '--markdown': 'Output result as markdown', |
| 48 | + }, 6)} |
| 49 | + `, { |
| 50 | + argv, |
| 51 | + description, |
| 52 | + importMeta, |
| 53 | + flags: { |
| 54 | + debug: { |
| 55 | + type: 'boolean', |
| 56 | + alias: 'd', |
| 57 | + default: false, |
| 58 | + }, |
| 59 | + json: { |
| 60 | + type: 'boolean', |
| 61 | + alias: 'j', |
| 62 | + default: false, |
| 63 | + }, |
| 64 | + markdown: { |
| 65 | + type: 'boolean', |
| 66 | + alias: 'm', |
| 67 | + default: false, |
| 68 | + }, |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + // Extract the input |
| 73 | + |
| 74 | + const { |
| 75 | + json: outputJson, |
| 76 | + markdown: outputMarkdown, |
| 77 | + } = cli.flags |
| 78 | + |
| 79 | + const [reportId, ...extraInput] = cli.input |
| 80 | + |
| 81 | + if (!reportId) { |
| 82 | + cli.showHelp() |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Validate the input |
| 87 | + |
| 88 | + if (extraInput.length) { |
| 89 | + throw new InputError(`Can only handle a single report ID at a time, but got ${cli.input.length} report ID:s: ${cli.input.join(', ')}`) |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + outputJson, |
| 94 | + outputMarkdown, |
| 95 | + reportId, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * @param {string} reportId |
| 101 | + * @returns {Promise<void|import('@socketsecurity/sdk').SocketSdkReturnType<'getReport'>>} |
| 102 | + */ |
| 103 | +async function fetchReportData (reportId) { |
| 104 | + // Do the API call |
| 105 | + |
| 106 | + const socketSdk = await setupSdk() |
| 107 | + const spinner = ora(`Fetching report with ID ${reportId}`).start() |
| 108 | + const result = await handleApiCall(socketSdk.getReport(reportId), spinner, 'fetching report') |
| 109 | + |
| 110 | + if (result.success === false) { |
| 111 | + return handleUnsuccessfulApiResponse(result, spinner) |
| 112 | + } |
| 113 | + |
| 114 | + // Conclude the status of the API call |
| 115 | + |
| 116 | + const issueSummary = getSeveritySummary(result.data.issues) |
| 117 | + spinner.succeed(`Report contains ${issueSummary || 'no'} issues`) |
| 118 | + |
| 119 | + return result |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @param {import('@socketsecurity/sdk').SocketSdkReturnType<'getReport'>["data"]} data |
| 124 | + * @param {{ name: string, outputJson: boolean, outputMarkdown: boolean, reportId: string }} context |
| 125 | + * @returns {void} |
| 126 | + */ |
| 127 | +function formatReportDataOutput (data, { name, outputJson, outputMarkdown, reportId }) { |
| 128 | + // If JSON, output and return... |
| 129 | + |
| 130 | + if (outputJson) { |
| 131 | + console.log(JSON.stringify(data, undefined, 2)) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + // ...else do the CLI / Markdown output dance |
| 136 | + |
| 137 | + const format = new ChalkOrMarkdown(!!outputMarkdown) |
| 138 | + const url = `https://socket.dev/npm/reports/${encodeURIComponent(reportId)}` |
| 139 | + |
| 140 | + console.log('\nDetailed info on socket.dev: ' + format.hyperlink(reportId, url, { fallbackToUrl: true })) |
| 141 | + if (!outputMarkdown) { |
| 142 | + console.log(chalk.dim('\nOr rerun', chalk.italic(name), 'using the', chalk.italic('--json'), 'flag to get full JSON output')) |
| 143 | + } |
| 144 | +} |
0 commit comments