@@ -115,6 +115,7 @@ function setupCommand (name, description, argv, importMeta) {
115
115
* @typedef PackageData
116
116
* @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getIssuesByNPMPackage'>["data"] } data
117
117
* @property {Record<import('../../utils/format-issues').SocketIssue['severity'], number> } severityCount
118
+ * @property {import('@socketsecurity/sdk').SocketSdkReturnType<'getScoreByNPMPackage'>["data"] } score
118
119
*/
119
120
120
121
/**
@@ -127,6 +128,7 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
127
128
const socketSdk = await setupSdk ( getDefaultKey ( ) || FREE_API_KEY )
128
129
const spinner = ora ( `Looking up data for version ${ pkgVersion } of ${ pkgName } ` ) . start ( )
129
130
const result = await handleApiCall ( socketSdk . getIssuesByNPMPackage ( pkgName , pkgVersion ) , 'looking up package' )
131
+ const scoreResult = await handleApiCall ( socketSdk . getScoreByNPMPackage ( pkgName , pkgVersion ) , 'looking up package score' )
130
132
131
133
if ( result . success === false ) {
132
134
return handleUnsuccessfulApiResponse ( 'getIssuesByNPMPackage' , result , spinner )
@@ -146,6 +148,7 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
146
148
return {
147
149
data : result . data ,
148
150
severityCount,
151
+ score : scoreResult . data
149
152
}
150
153
}
151
154
@@ -154,10 +157,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
154
157
* @param {{ name: string } & CommandContext } context
155
158
* @returns {void }
156
159
*/
157
- function formatPackageDataOutput ( { data, severityCount } , { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict } ) {
160
+ function formatPackageDataOutput ( { data, severityCount, score } , { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict } ) {
158
161
if ( outputJson ) {
159
162
console . log ( JSON . stringify ( data , undefined , 2 ) )
160
163
} else {
164
+ console . log ( '\nPackage report card:\n' )
165
+
166
+ const scoreResult = {
167
+ 'Supply Chain Risk' : Math . floor ( score . supplyChainRisk . score * 100 ) ,
168
+ 'Maintenance' : Math . floor ( score . maintenance . score * 100 ) ,
169
+ 'Quality' : Math . floor ( score . quality . score * 100 ) ,
170
+ 'Vulnerabilities' : Math . floor ( score . vulnerability . score * 100 ) ,
171
+ 'License' : Math . floor ( score . license . score * 100 )
172
+ }
173
+ Object . entries ( scoreResult ) . map ( score => console . log ( `- ${ score [ 0 ] } : ${ formatScore ( score [ 1 ] ) } ` ) )
174
+
161
175
const format = new ChalkOrMarkdown ( ! ! outputMarkdown )
162
176
const url = `https://socket.dev/npm/package/${ pkgName } /overview/${ pkgVersion } `
163
177
@@ -171,3 +185,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
171
185
process . exit ( 1 )
172
186
}
173
187
}
188
+
189
+ /**
190
+ * @param {number } score
191
+ * @returns {string }
192
+ */
193
+ function formatScore ( score ) {
194
+ const error = chalk . hex ( '#de7c7b' )
195
+ const warning = chalk . hex ( '#e59361' )
196
+ const success = chalk . hex ( '#a4cb9d' )
197
+
198
+ if ( score > 80 ) {
199
+ return `${ success ( score ) } `
200
+ } else if ( score < 80 && score > 60 ) {
201
+ return `${ warning ( score ) } `
202
+ } else {
203
+ return `${ error ( score ) } `
204
+ }
205
+ }
0 commit comments