Skip to content

Commit d30b79c

Browse files
committed
Cleanup output for fix command
1 parent 51de259 commit d30b79c

File tree

4 files changed

+91
-91
lines changed

4 files changed

+91
-91
lines changed

src/commands/info/cmd-info.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async function run(
5252
parentName
5353
})
5454

55+
const { all, json, markdown, strict } = cli.flags
5556
const [rawPkgName = ''] = cli.input
5657

5758
if (!rawPkgName || cli.input.length > 1) {
@@ -78,11 +79,10 @@ async function run(
7879

7980
await getPackageInfo({
8081
commandName: `${parentName} ${config.commandName}`,
81-
includeAllIssues: Boolean(cli.flags['all']),
82-
outputJson: Boolean(cli.flags['json']),
83-
outputMarkdown: Boolean(cli.flags['markdown']),
82+
includeAllIssues: Boolean(all),
83+
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
8484
pkgName,
8585
pkgVersion,
86-
strict: Boolean(cli.flags['strict'])
86+
strict: Boolean(strict)
8787
})
8888
}

src/commands/info/fetch-package-info.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { Spinner } from '@socketsecurity/registry/lib/spinner'
2-
31
import { PackageData } from './get-package-info'
2+
import constants from '../../constants'
43
import { getSeverityCount } from '../../utils/alert/severity'
54
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
65
import { getPublicToken, setupSdk } from '../../utils/sdk'
76

87
export async function fetchPackageInfo(
98
pkgName: string,
109
pkgVersion: string,
11-
includeAllIssues: boolean,
12-
spinner: Spinner
10+
includeAllIssues: boolean
1311
): Promise<void | PackageData> {
12+
// Lazily access constants.spinner.
13+
const { spinner } = constants
14+
15+
spinner.start(
16+
pkgVersion === 'latest'
17+
? `Looking up data for the latest version of ${pkgName}`
18+
: `Looking up data for version ${pkgVersion} of ${pkgName}`
19+
)
20+
1421
const socketSdk = await setupSdk(getPublicToken())
1522
const result = await handleApiCall(
1623
socketSdk.getIssuesByNPMPackage(pkgName, pkgVersion),
@@ -42,6 +49,8 @@ export async function fetchPackageInfo(
4249
includeAllIssues ? undefined : 'high'
4350
)
4451

52+
spinner?.successAndStop('Data fetched')
53+
4554
return {
4655
data: result.data,
4756
severityCount,

src/commands/info/format-package-info.ts

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import process from 'node:process'
2-
31
import colors from 'yoctocolors-cjs'
42

53
import constants from '@socketsecurity/registry/lib/constants'
64
import { logger } from '@socketsecurity/registry/lib/logger'
7-
import { Spinner } from '@socketsecurity/registry/lib/spinner'
85

96
import { PackageData } from './get-package-info'
107
import { formatSeverityCount } from '../../utils/alert/severity'
@@ -23,69 +20,73 @@ export function formatPackageInfo(
2320
{ data, score, severityCount }: PackageData,
2421
{
2522
name,
26-
outputJson,
27-
outputMarkdown,
23+
outputKind,
2824
pkgName,
29-
pkgVersion,
30-
strict
25+
pkgVersion
3126
}: {
3227
includeAllIssues: boolean
33-
outputJson: boolean
34-
outputMarkdown: boolean
28+
name: string
29+
outputKind: 'json' | 'markdown' | 'print'
3530
pkgName: string
3631
pkgVersion: string
37-
strict: boolean
38-
} & { name: string },
39-
spinner: Spinner
32+
}
4033
): void {
41-
if (outputJson) {
34+
if (outputKind === 'json') {
4235
logger.log(JSON.stringify(data, undefined, 2))
36+
return
37+
}
38+
39+
if (outputKind === 'markdown') {
40+
logger.log(`\n# Package report for ${pkgName}\n`)
41+
logger.log('Package report card:\n')
4342
} else {
44-
logger.log('\nPackage report card:')
45-
const scoreResult = {
46-
'Supply Chain Risk': Math.floor(score.supplyChainRisk.score * 100),
47-
Maintenance: Math.floor(score.maintenance.score * 100),
48-
Quality: Math.floor(score.quality.score * 100),
49-
Vulnerabilities: Math.floor(score.vulnerability.score * 100),
50-
License: Math.floor(score.license.score * 100)
43+
logger.log(`\nPackage report card for ${pkgName}:\n`)
44+
}
45+
const scoreResult = {
46+
'Supply Chain Risk': Math.floor(score.supplyChainRisk.score * 100),
47+
Maintenance: Math.floor(score.maintenance.score * 100),
48+
Quality: Math.floor(score.quality.score * 100),
49+
Vulnerabilities: Math.floor(score.vulnerability.score * 100),
50+
License: Math.floor(score.license.score * 100)
51+
}
52+
Object.entries(scoreResult).map(score =>
53+
logger.log(`- ${score[0]}: ${formatScore(score[1])}`)
54+
)
55+
logger.log('\n')
56+
57+
if (objectSome(severityCount)) {
58+
if (outputKind === 'markdown') {
59+
logger.log('# Issues\n')
5160
}
52-
Object.entries(scoreResult).map(score =>
53-
logger.log(`- ${score[0]}: ${formatScore(score[1])}`)
61+
logger.log(
62+
`Package has these issues: ${formatSeverityCount(severityCount)}\n`
5463
)
55-
logger.log('\n')
56-
if (objectSome(severityCount)) {
57-
spinner[strict ? 'error' : 'success'](
58-
`Package has these issues: ${formatSeverityCount(severityCount)}`
59-
)
60-
formatPackageIssuesDetails(data, outputMarkdown)
61-
} else {
62-
spinner.successAndStop('Package has no issues')
63-
}
64+
formatPackageIssuesDetails(data, outputKind === 'markdown')
65+
} else {
66+
logger.log('Package has no issues')
67+
}
6468

65-
const format = new ColorOrMarkdown(!!outputMarkdown)
66-
const url = getSocketDevPackageOverviewUrl(NPM, pkgName, pkgVersion)
69+
const format = new ColorOrMarkdown(outputKind === 'markdown')
70+
const url = getSocketDevPackageOverviewUrl(NPM, pkgName, pkgVersion)
6771

68-
logger.log('\n')
69-
if (pkgVersion === 'latest') {
70-
logger.log(
71-
`Detailed info on socket.dev: ${format.hyperlink(`${pkgName}`, url, { fallbackToUrl: true })}`
72-
)
73-
} else {
74-
logger.log(
75-
`Detailed info on socket.dev: ${format.hyperlink(`${pkgName} v${pkgVersion}`, url, { fallbackToUrl: true })}`
76-
)
77-
}
78-
if (!outputMarkdown) {
79-
logger.log(
80-
colors.dim(
81-
`\nOr rerun ${colors.italic(name)} using the ${colors.italic('--json')} flag to get full JSON output`
82-
)
83-
)
84-
}
72+
logger.log('\n')
73+
if (pkgVersion === 'latest') {
74+
logger.log(
75+
`Detailed info on socket.dev: ${format.hyperlink(`${pkgName}`, url, { fallbackToUrl: true })}`
76+
)
77+
} else {
78+
logger.log(
79+
`Detailed info on socket.dev: ${format.hyperlink(`${pkgName} v${pkgVersion}`, url, { fallbackToUrl: true })}`
80+
)
8581
}
86-
87-
if (strict && objectSome(severityCount)) {
88-
process.exit(1)
82+
if (outputKind !== 'markdown') {
83+
logger.log(
84+
colors.dim(
85+
`\nOr rerun ${colors.italic(name)} using the ${colors.italic('--json')} flag to get full JSON output`
86+
)
87+
)
88+
} else {
89+
logger.log('')
8990
}
9091
}
9192

@@ -118,7 +119,7 @@ function formatPackageIssuesDetails(
118119
{}
119120
)
120121

121-
const format = new ColorOrMarkdown(!!outputMarkdown)
122+
const format = new ColorOrMarkdown(outputMarkdown)
122123
for (const issue of Object.keys(uniqueIssues)) {
123124
const issueWithLink = format.hyperlink(
124125
`${uniqueIssues[issue]?.label}`,
Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import process from 'node:process'
2+
13
import { fetchPackageInfo } from './fetch-package-info'
24
import { formatPackageInfo } from './format-package-info'
3-
import constants from '../../constants'
5+
import { objectSome } from '../../utils/objects'
46

57
import type { SocketSdkAlert } from '../../utils/alert/severity'
68
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
@@ -14,48 +16,36 @@ export interface PackageData {
1416
export async function getPackageInfo({
1517
commandName,
1618
includeAllIssues,
17-
outputJson,
18-
outputMarkdown,
19+
outputKind,
1920
pkgName,
2021
pkgVersion,
2122
strict
2223
}: {
2324
commandName: string
2425
includeAllIssues: boolean
25-
outputJson: boolean
26-
outputMarkdown: boolean
26+
outputKind: 'json' | 'markdown' | 'print'
2727
pkgName: string
2828
pkgVersion: string
2929
strict: boolean
3030
}) {
31-
// Lazily access constants.spinner.
32-
const { spinner } = constants
33-
34-
spinner.start(
35-
pkgVersion === 'latest'
36-
? `Looking up data for the latest version of ${pkgName}`
37-
: `Looking up data for version ${pkgVersion} of ${pkgName}`
38-
)
39-
4031
const packageData = await fetchPackageInfo(
4132
pkgName,
4233
pkgVersion,
43-
includeAllIssues,
44-
spinner
34+
includeAllIssues
4535
)
36+
4637
if (packageData) {
47-
formatPackageInfo(
48-
packageData,
49-
{
50-
name: commandName,
51-
includeAllIssues,
52-
outputJson,
53-
outputMarkdown,
54-
pkgName,
55-
pkgVersion,
56-
strict
57-
},
58-
spinner
59-
)
38+
formatPackageInfo(packageData, {
39+
name: commandName,
40+
includeAllIssues,
41+
outputKind,
42+
pkgName,
43+
pkgVersion
44+
})
45+
46+
if (strict && objectSome(packageData.severityCount)) {
47+
// Let NodeJS exit gracefully but with exit(1)
48+
process.exitCode = 1
49+
}
6050
}
6151
}

0 commit comments

Comments
 (0)