@@ -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,11 +128,16 @@ 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 )
133
135
}
134
136
137
+ if ( scoreResult . success === false ) {
138
+ return handleUnsuccessfulApiResponse ( 'getScoreByNPMPackage' , scoreResult , spinner )
139
+ }
140
+
135
141
// Conclude the status of the API call
136
142
137
143
const severityCount = getSeverityCount ( result . data , includeAllIssues ? undefined : 'high' )
@@ -146,6 +152,7 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
146
152
return {
147
153
data : result . data ,
148
154
severityCount,
155
+ score : scoreResult . data
149
156
}
150
157
}
151
158
@@ -154,10 +161,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
154
161
* @param {{ name: string } & CommandContext } context
155
162
* @returns {void }
156
163
*/
157
- function formatPackageDataOutput ( { data, severityCount } , { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict } ) {
164
+ function formatPackageDataOutput ( { data, severityCount, score } , { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict } ) {
158
165
if ( outputJson ) {
159
166
console . log ( JSON . stringify ( data , undefined , 2 ) )
160
167
} 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
+
161
179
const format = new ChalkOrMarkdown ( ! ! outputMarkdown )
162
180
const url = `https://socket.dev/npm/package/${ pkgName } /overview/${ pkgVersion } `
163
181
@@ -171,3 +189,21 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
171
189
process . exit ( 1 )
172
190
}
173
191
}
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