@@ -5,98 +5,29 @@ import colors from 'yoctocolors-cjs'
55
66import { logger } from '@socketsecurity/registry/lib/logger'
77
8- import constants from '../../constants'
9- import { handleAPIError , handleApiCall , queryAPI } from '../../utils/api'
10- import { AuthError } from '../../utils/errors'
11- import { getDefaultToken } from '../../utils/sdk'
12-
138import type { SocketSdkReturnType } from '@socketsecurity/sdk'
149
15- export async function getDiffScan ( {
16- after,
17- before,
18- depth,
19- file,
20- orgSlug,
21- outputJson
22- } : {
23- after : string
24- before : string
25- depth : number
26- file : string
27- orgSlug : string
28- outputJson : boolean
29- } ) : Promise < void > {
30- const apiToken = getDefaultToken ( )
31- if ( ! apiToken ) {
32- throw new AuthError (
33- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
34- )
35- }
36-
37- await getDiffScanWithToken ( {
38- after,
39- before,
10+ export async function outputDiffScan (
11+ result : SocketSdkReturnType < 'GetOrgDiffScan' > [ 'data' ] ,
12+ {
4013 depth,
4114 file,
42- orgSlug,
43- outputJson,
44- apiToken
45- } )
46- }
47- export async function getDiffScanWithToken ( {
48- after,
49- apiToken,
50- before,
51- depth,
52- file,
53- orgSlug,
54- outputJson
55- } : {
56- after : string
57- apiToken : string
58- depth : number
59- before : string
60- file : string
61- orgSlug : string
62- outputJson : boolean
63- } ) : Promise < void > {
64- // Lazily access constants.spinner.
65- const { spinner } = constants
66-
67- spinner . start ( 'Getting diff scan...' )
68-
69- const response = await queryAPI (
70- `orgs/${ orgSlug } /full-scans/diff?before=${ encodeURIComponent ( before ) } &after=${ encodeURIComponent ( after ) } ` ,
71- apiToken
72- )
73-
74- if ( ! response . ok ) {
75- const err = await handleAPIError ( response . status )
76- spinner . errorAndStop (
77- `${ colors . bgRed ( colors . white ( response . statusText ) ) } : ${ err } `
78- )
79- return
15+ outputKind
16+ } : {
17+ depth : number
18+ file : string
19+ outputKind : 'json' | 'markdown' | 'text'
8020 }
81-
82- const result = await handleApiCall (
83- ( await response . json ( ) ) as Promise <
84- SocketSdkReturnType < 'GetOrgDiffScan' > [ 'data' ]
85- > ,
86- 'Deserializing json'
87- )
88-
89- spinner . stop ( )
90-
91- const dashboardUrl = ( result as any ) ?. [ 'diff_report_url' ]
21+ ) : Promise < void > {
22+ const dashboardUrl = result . diff_report_url
9223 const dashboardMessage = dashboardUrl
9324 ? `\n View this diff scan in the Socket dashboard: ${ colors . cyan ( dashboardUrl ) } `
9425 : ''
9526
9627 // When forcing json, or dumping to file, serialize to string such that it
9728 // won't get truncated. The only way to dump the full raw JSON to stdout is
9829 // to use `--json --file -` (the dash is a standard notation for stdout)
99- if ( outputJson || file ) {
30+ if ( outputKind === 'json' || file ) {
10031 let json
10132 try {
10233 json = JSON . stringify ( result , null , 2 )
0 commit comments