|
| 1 | +// @ts-ignore |
| 2 | +import chalkTable from 'chalk-table' |
| 3 | +import colors from 'yoctocolors-cjs' |
| 4 | + |
| 5 | +import { logger } from '@socketsecurity/registry/lib/logger' |
| 6 | + |
| 7 | +import type { SocketSdkReturnType } from '@socketsecurity/sdk' |
| 8 | + |
| 9 | +export async function outputDependencies( |
| 10 | + data: SocketSdkReturnType<'searchDependencies'>['data'], |
| 11 | + { |
| 12 | + limit, |
| 13 | + offset, |
| 14 | + outputKind |
| 15 | + }: { |
| 16 | + limit: number |
| 17 | + offset: number |
| 18 | + outputKind: 'json' | 'markdown' | 'text' |
| 19 | + } |
| 20 | +): Promise<void> { |
| 21 | + if (outputKind === 'json') { |
| 22 | + let json |
| 23 | + try { |
| 24 | + json = JSON.stringify(data, null, 2) |
| 25 | + } catch (e) { |
| 26 | + process.exitCode = 1 |
| 27 | + logger.fail( |
| 28 | + 'There was a problem converting the data to JSON, please try without the `--json` flag' |
| 29 | + ) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + logger.log(json) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + logger.log( |
| 38 | + 'Request details: Offset:', |
| 39 | + offset, |
| 40 | + ', limit:', |
| 41 | + limit, |
| 42 | + ', is there more data after this?', |
| 43 | + data.end ? 'no' : 'yes' |
| 44 | + ) |
| 45 | + |
| 46 | + const options = { |
| 47 | + columns: [ |
| 48 | + { field: 'namespace', name: colors.cyan('Namespace') }, |
| 49 | + { field: 'name', name: colors.cyan('Name') }, |
| 50 | + { field: 'version', name: colors.cyan('Version') }, |
| 51 | + { field: 'repository', name: colors.cyan('Repository') }, |
| 52 | + { field: 'branch', name: colors.cyan('Branch') }, |
| 53 | + { field: 'type', name: colors.cyan('Type') }, |
| 54 | + { field: 'direct', name: colors.cyan('Direct') } |
| 55 | + ] |
| 56 | + } |
| 57 | + |
| 58 | + logger.log(chalkTable(options, data.rows)) |
| 59 | +} |
0 commit comments