-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathoutput-diff-scan.ts
79 lines (70 loc) · 2.23 KB
/
output-diff-scan.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import fs from 'node:fs'
import util from 'node:util'
import colors from 'yoctocolors-cjs'
import { logger } from '@socketsecurity/registry/lib/logger'
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
export async function outputDiffScan(
result: SocketSdkReturnType<'GetOrgDiffScan'>['data'],
{
depth,
file,
outputKind
}: {
depth: number
file: string
outputKind: 'json' | 'markdown' | 'text'
}
): Promise<void> {
const dashboardUrl = result.diff_report_url
const dashboardMessage = dashboardUrl
? `\n View this diff scan in the Socket dashboard: ${colors.cyan(dashboardUrl)}`
: ''
// When forcing json, or dumping to file, serialize to string such that it
// won't get truncated. The only way to dump the full raw JSON to stdout is
// to use `--json --file -` (the dash is a standard notation for stdout)
if (outputKind === 'json' || file) {
let json
try {
json = JSON.stringify(result, null, 2)
} catch (e) {
process.exitCode = 1
// Most likely caused by a circular reference (or OOM)
logger.fail('There was a problem converting the data to JSON')
logger.error(e)
return
}
if (file && file !== '-') {
logger.log(`Writing json to \`${file}\``)
fs.writeFile(file, JSON.stringify(result, null, 2), err => {
if (err) {
logger.fail(`Writing to \`${file}\` failed...`)
logger.error(err)
} else {
logger.log(`Data successfully written to \`${file}\``)
}
logger.error(dashboardMessage)
})
} else {
// TODO: expose different method for writing to stderr when simply dodging stdout
logger.error(`\n Diff scan result: \n`)
logger.log(json)
logger.error(dashboardMessage)
}
return
}
// In this case neither the --json nor the --file flag was passed
// Dump the JSON to CLI and let NodeJS deal with truncation
logger.log('Diff scan result:')
logger.log(
util.inspect(result, {
showHidden: false,
depth: depth > 0 ? depth : null,
colors: true,
maxArrayLength: null
})
)
logger.log(
`\n 📝 To display the detailed report in the terminal, use the --json flag \n`
)
logger.log(dashboardMessage)
}