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