Skip to content

Commit edf65a6

Browse files
Merge pull request #91 from SocketDev/cg/addPackageScore
Add package scores to "socket info" command
2 parents 137f0b6 + cbd4785 commit edf65a6

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

lib/commands/info/index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function setupCommand (name, description, argv, importMeta) {
115115
* @typedef PackageData
116116
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getIssuesByNPMPackage'>["data"]} data
117117
* @property {Record<import('../../utils/format-issues').SocketIssue['severity'], number>} severityCount
118+
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getScoreByNPMPackage'>["data"]} score
118119
*/
119120

120121
/**
@@ -127,11 +128,16 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
127128
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
128129
const spinner = ora(`Looking up data for version ${pkgVersion} of ${pkgName}`).start()
129130
const result = await handleApiCall(socketSdk.getIssuesByNPMPackage(pkgName, pkgVersion), 'looking up package')
131+
const scoreResult = await handleApiCall(socketSdk.getScoreByNPMPackage(pkgName, pkgVersion), 'looking up package score')
130132

131133
if (result.success === false) {
132134
return handleUnsuccessfulApiResponse('getIssuesByNPMPackage', result, spinner)
133135
}
134136

137+
if (scoreResult.success === false) {
138+
return handleUnsuccessfulApiResponse('getScoreByNPMPackage', scoreResult, spinner)
139+
}
140+
135141
// Conclude the status of the API call
136142

137143
const severityCount = getSeverityCount(result.data, includeAllIssues ? undefined : 'high')
@@ -146,6 +152,7 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
146152
return {
147153
data: result.data,
148154
severityCount,
155+
score: scoreResult.data
149156
}
150157
}
151158

@@ -154,10 +161,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
154161
* @param {{ name: string } & CommandContext} context
155162
* @returns {void}
156163
*/
157-
function formatPackageDataOutput ({ data, severityCount }, { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict }) {
164+
function formatPackageDataOutput ({ data, severityCount, score }, { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict }) {
158165
if (outputJson) {
159166
console.log(JSON.stringify(data, undefined, 2))
160167
} else {
168+
console.log('\nPackage report card:\n')
169+
170+
const scoreResult = {
171+
'Supply Chain Risk': Math.floor(score.supplyChainRisk.score * 100),
172+
'Maintenance': Math.floor(score.maintenance.score * 100),
173+
'Quality': Math.floor(score.quality.score * 100),
174+
'Vulnerabilities': Math.floor(score.vulnerability.score * 100),
175+
'License': Math.floor(score.license.score * 100)
176+
}
177+
Object.entries(scoreResult).map(score => console.log(`- ${score[0]}: ${formatScore(score[1])}`))
178+
161179
const format = new ChalkOrMarkdown(!!outputMarkdown)
162180
const url = `https://socket.dev/npm/package/${pkgName}/overview/${pkgVersion}`
163181

@@ -171,3 +189,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
171189
process.exit(1)
172190
}
173191
}
192+
193+
/**
194+
* @param {number} score
195+
* @returns {string}
196+
*/
197+
function formatScore (score) {
198+
const error = chalk.hex('#de7c7b')
199+
const warning = chalk.hex('#e59361')
200+
const success = chalk.hex('#a4cb9d')
201+
202+
if (score > 80) {
203+
return `${success(score)}`
204+
} else if (score < 80 && score > 60) {
205+
return `${warning(score)}`
206+
} else {
207+
return `${error(score)}`
208+
}
209+
}

0 commit comments

Comments
 (0)