Skip to content

Commit 83941c0

Browse files
committed
refactor
1 parent c050b04 commit 83941c0

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

lib/commands/info/index.js

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ export const info = {
2020
const name = parentName + ' info'
2121

2222
const input = setupCommand(name, info.description, argv, importMeta)
23-
const packageData = input && await fetchPackageData(input.pkgName, input.pkgVersion, input)
24-
25-
if (packageData) {
26-
formatPackageDataOutput(packageData, { name, ...input })
23+
if (input) {
24+
const spinner = ora(`Looking up data for version ${input.pkgVersion} of ${input.pkgName}\n`).start()
25+
const packageData = await fetchPackageData(input.pkgName, input.pkgVersion, input, spinner)
26+
if (packageData) {
27+
formatPackageDataOutput(packageData, { name, ...input }, spinner)
28+
}
2729
}
2830
}
2931
}
@@ -121,12 +123,12 @@ function setupCommand (name, description, argv, importMeta) {
121123
/**
122124
* @param {string} pkgName
123125
* @param {string} pkgVersion
124-
* @param {Pick<CommandContext, 'includeAllIssues' | 'strict'>} context
126+
* @param {Pick<CommandContext, 'includeAllIssues'>} context
127+
* @param {import('ora').Ora} spinner
125128
* @returns {Promise<void|PackageData>}
126129
*/
127-
async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict }) {
130+
async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues }, spinner) {
128131
const socketSdk = await setupSdk(getDefaultKey() || FREE_API_KEY)
129-
const spinner = ora(`Looking up data for version ${pkgVersion} of ${pkgName}`).start()
130132
const result = await handleApiCall(socketSdk.getIssuesByNPMPackage(pkgName, pkgVersion), 'looking up package')
131133
const scoreResult = await handleApiCall(socketSdk.getScoreByNPMPackage(pkgName, pkgVersion), 'looking up package score')
132134

@@ -142,33 +144,8 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
142144

143145
const severityCount = getSeverityCount(result.data, includeAllIssues ? undefined : 'high')
144146

145-
if (objectSome(severityCount)) {
146-
const issueSummary = formatSeverityCount(severityCount)
147-
spinner[strict ? 'fail' : 'succeed'](`Package has these issues: ${issueSummary}`)
148-
149-
// Return the alert types for critical and high alerts
150-
const issueDetails = result.data.filter(d => d.value?.severity === 'high' || d.value?.severity === 'critical')
151-
const uniqueIssues = issueDetails.reduce((/** @type {{ [key: string]: number }} */ acc, issue) => {
152-
const { type } = issue
153-
if (type) {
154-
let count = 0
155-
if (!acc[type]) {
156-
count += 1
157-
acc[type] = count
158-
} else {
159-
acc[type]++
160-
}
161-
}
162-
return acc
163-
}, {})
164-
Object.keys(uniqueIssues).map(issue => {
165-
if (uniqueIssues[issue] === 1) {
166-
return console.log(`- ${issue}`)
167-
}
168-
return console.log(`- ${issue}: ${uniqueIssues[issue]}`)
169-
})
170-
} else {
171-
spinner.succeed('Package has no issues')
147+
if (!objectSome(severityCount)) {
148+
spinner.succeed('\nPackage has no issues')
172149
}
173150

174151
return {
@@ -181,14 +158,14 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
181158
/**
182159
* @param {PackageData} packageData
183160
* @param {{ name: string } & CommandContext} context
161+
* @param {import('ora').Ora} spinner
184162
* @returns {void}
185163
*/
186-
function formatPackageDataOutput ({ data, severityCount, score }, { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict }) {
164+
function formatPackageDataOutput ({ data, severityCount, score }, { name, outputJson, outputMarkdown, pkgName, pkgVersion, strict }, spinner) {
187165
if (outputJson) {
188166
console.log(JSON.stringify(data, undefined, 2))
189167
} else {
190168
console.log('\nPackage report card:')
191-
192169
const scoreResult = {
193170
'Supply Chain Risk': Math.floor(score.supplyChainRisk.score * 100),
194171
'Maintenance': Math.floor(score.maintenance.score * 100),
@@ -198,9 +175,15 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
198175
}
199176
Object.entries(scoreResult).map(score => console.log(`- ${score[0]}: ${formatScore(score[1])}`))
200177

178+
// Package issues list
179+
const issueSummary = formatSeverityCount(severityCount)
180+
console.log('\n')
181+
spinner[strict ? 'fail' : 'succeed'](`Package has these issues: ${issueSummary}`)
182+
formatPackageIssuesDetails(data)
183+
184+
// Link to issues list
201185
const format = new ChalkOrMarkdown(!!outputMarkdown)
202186
const url = `https://socket.dev/npm/package/${pkgName}/overview/${pkgVersion}`
203-
204187
console.log('\nDetailed info on socket.dev: ' + format.hyperlink(`${pkgName} v${pkgVersion}`, url, { fallbackToUrl: true }))
205188
if (!outputMarkdown) {
206189
console.log(chalk.dim('\nOr rerun', chalk.italic(name), 'using the', chalk.italic('--json'), 'flag to get full JSON output'))
@@ -212,6 +195,33 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues, strict
212195
}
213196
}
214197

198+
/**
199+
* @param {import('@socketsecurity/sdk').SocketSdkReturnType<'getIssuesByNPMPackage'>["data"]} packageData
200+
* @returns {void[]}
201+
*/
202+
function formatPackageIssuesDetails (packageData) {
203+
const issueDetails = packageData.filter(d => d.value?.severity === 'high' || d.value?.severity === 'critical')
204+
const uniqueIssues = issueDetails.reduce((/** @type {{ [key: string]: number }} */ acc, issue) => {
205+
const { type } = issue
206+
if (type) {
207+
let count = 0
208+
if (!acc[type]) {
209+
count += 1
210+
acc[type] = count
211+
} else {
212+
acc[type]++
213+
}
214+
}
215+
return acc
216+
}, {})
217+
return Object.keys(uniqueIssues).map(issue => {
218+
if (uniqueIssues[issue] === 1) {
219+
return console.log(`- ${issue}`)
220+
}
221+
return console.log(`- ${issue}: ${uniqueIssues[issue]}`)
222+
})
223+
}
224+
215225
/**
216226
* @param {number} score
217227
* @returns {string}

0 commit comments

Comments
 (0)