|
| 1 | +/* eslint-disable no-console */ |
| 2 | + |
| 3 | +// import chalk from 'chalk' |
| 4 | +import meow from 'meow' |
| 5 | +import ora from 'ora' |
| 6 | + |
| 7 | +import { outputFlags } from '../../flags/index.js' |
| 8 | +import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js' |
| 9 | +import { prepareFlags } from '../../utils/flags.js' |
| 10 | +import { printFlagList } from '../../utils/formatting.js' |
| 11 | +import { getDefaultKey, setupSdk } from '../../utils/sdk.js' |
| 12 | + |
| 13 | +/** @type {import('../../utils/meow-with-subcommands.js').CliSubcommand} */ |
| 14 | +export const dependencies = { |
| 15 | + description: 'Search for any dependency that is being used in your organization', |
| 16 | + async run (argv, importMeta, { parentName }) { |
| 17 | + const name = parentName + ' dependencies' |
| 18 | + |
| 19 | + const input = setupCommand(name, dependencies.description, argv, importMeta) |
| 20 | + if (input) { |
| 21 | + const spinnerText = 'Searching dependencies...' |
| 22 | + const spinner = ora(spinnerText).start() |
| 23 | + await searchDeps(input.limit, input.offset, spinner) |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const dependenciesFlags = prepareFlags({ |
| 29 | + limit: { |
| 30 | + type: 'number', |
| 31 | + shortFlag: 'l', |
| 32 | + default: 50, |
| 33 | + description: 'Repository name', |
| 34 | + }, |
| 35 | + offset: { |
| 36 | + type: 'number', |
| 37 | + shortFlag: 'o', |
| 38 | + default: 0, |
| 39 | + description: 'Branch name', |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | +// Internal functions |
| 44 | + |
| 45 | +/** |
| 46 | + * @typedef Command |
| 47 | + * @property {boolean} outputJson |
| 48 | + * @property {boolean} outputMarkdown |
| 49 | + * @property {number} limit |
| 50 | + * @property {number} offset |
| 51 | + */ |
| 52 | + |
| 53 | +/** |
| 54 | + * @param {string} name |
| 55 | + * @param {string} description |
| 56 | + * @param {readonly string[]} argv |
| 57 | + * @param {ImportMeta} importMeta |
| 58 | + * @returns {void|Command} |
| 59 | + */ |
| 60 | +function setupCommand (name, description, argv, importMeta) { |
| 61 | + const flags = { |
| 62 | + ...outputFlags, |
| 63 | + ...dependenciesFlags |
| 64 | + } |
| 65 | + |
| 66 | + const cli = meow(` |
| 67 | + Usage |
| 68 | + $ ${name} |
| 69 | +
|
| 70 | + Options |
| 71 | + ${printFlagList(flags, 6)} |
| 72 | +
|
| 73 | + Examples |
| 74 | + $ ${name} FakeOrg |
| 75 | + `, { |
| 76 | + argv, |
| 77 | + description, |
| 78 | + importMeta, |
| 79 | + flags |
| 80 | + }) |
| 81 | + |
| 82 | + const { |
| 83 | + json: outputJson, |
| 84 | + markdown: outputMarkdown, |
| 85 | + limit, |
| 86 | + offset |
| 87 | + } = cli.flags |
| 88 | + |
| 89 | +// if (cli.input.length < 2) { |
| 90 | +// console.error(`${chalk.bgRed('Input error')}: Please specify an organization slug and a scan ID.\n`) |
| 91 | +// cli.showHelp() |
| 92 | +// return |
| 93 | +// } |
| 94 | + |
| 95 | + return { |
| 96 | + outputJson, |
| 97 | + outputMarkdown, |
| 98 | + limit, |
| 99 | + offset |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @typedef DependenciesData |
| 105 | + * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'searchDependencies'>["data"]} data |
| 106 | + */ |
| 107 | + |
| 108 | +/** |
| 109 | + * @param {number} limit |
| 110 | + * @param {number} offset |
| 111 | + * @param {import('ora').Ora} spinner |
| 112 | + * @returns {Promise<void|DependenciesData>} |
| 113 | + */ |
| 114 | +async function searchDeps (limit, offset, spinner) { |
| 115 | + const socketSdk = await setupSdk(getDefaultKey()) |
| 116 | + // @ts-ignore |
| 117 | + const result = await handleApiCall(socketSdk.searchDependencies({ limit, offset }), 'Searching dependencies') |
| 118 | + |
| 119 | + if (!result.success) { |
| 120 | + return handleUnsuccessfulApiResponse('searchDependencies', result, spinner) |
| 121 | + } |
| 122 | + |
| 123 | + spinner.stop() |
| 124 | + |
| 125 | + console.log('Organization dependencies: \n') |
| 126 | + console.log(result.data) |
| 127 | + |
| 128 | + return { |
| 129 | + data: result.data |
| 130 | + } |
| 131 | +} |
0 commit comments