33import chalk from 'chalk'
44import meow from 'meow'
55import ora from 'ora'
6- import { ErrorWithCause } from 'pony-cause'
76
7+ import { handleApiCall , handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
88import { ChalkOrMarkdown } from '../../utils/chalk-markdown.js'
9- import { AuthError , InputError } from '../../utils/errors.js'
9+ import { InputError } from '../../utils/errors.js'
1010import { getSeveritySummary } from '../../utils/format-issues.js'
1111import { printFlagList } from '../../utils/formatting.js'
1212import { setupSdk } from '../../utils/sdk.js'
1313
14- const description = 'Look up info regarding a package'
14+ /** @type {import('../../utils/meow-with-subcommands').CliSubcommand } */
15+ export const info = {
16+ description : 'Look up info regarding a package' ,
17+ async run ( argv , importMeta , { parentName } ) {
18+ const name = parentName + ' info'
1519
16- /** @type {import('../../utils/meow-with-subcommands').CliSubcommandRun } */
17- const run = async ( argv , importMeta , { parentName } ) => {
18- const name = parentName + ' info'
20+ const input = setupCommand ( name , info . description , argv , importMeta )
21+ const result = input && await fetchPackageData ( input . pkgName , input . pkgVersion )
1922
23+ if ( result ) {
24+ formatPackageDataOutput ( result . data , { name, ...input } )
25+ }
26+ }
27+ }
28+
29+ // Internal functions
30+
31+ /**
32+ * @param {string } name
33+ * @param {string } description
34+ * @param {readonly string[] } argv
35+ * @param {ImportMeta } importMeta
36+ * @returns {void|{ outputJson: boolean, outputMarkdown: boolean, pkgName: string, pkgVersion: string } }
37+ */
38+ function setupCommand ( name , description , argv , importMeta ) {
2039 const cli = meow ( `
2140 Usage
2241 $ ${ name } <name>
2342
2443 Options
2544 ${ printFlagList ( {
26- '--debug' : 'Output debug information' ,
2745 '--json' : 'Output result as json' ,
2846 '--markdown' : 'Output result as markdown' ,
2947 } , 6 ) }
@@ -36,11 +54,6 @@ const run = async (argv, importMeta, { parentName }) => {
3654 description,
3755 importMeta,
3856 flags : {
39- debug : {
40- type : 'boolean' ,
41- alias : 'd' ,
42- default : false ,
43- } ,
4457 json : {
4558 type : 'boolean' ,
4659 alias : 'j' ,
@@ -83,47 +96,56 @@ const run = async (argv, importMeta, { parentName }) => {
8396 throw new InputError ( 'Need to specify a version, like eg: webtorrent@1.0.0' )
8497 }
8598
86- const socketSdk = await setupSdk ( )
99+ return {
100+ outputJson,
101+ outputMarkdown,
102+ pkgName,
103+ pkgVersion
104+ }
105+ }
87106
107+ /**
108+ * @param {string } pkgName
109+ * @param {string } pkgVersion
110+ * @returns {Promise<void|import('@socketsecurity/sdk').SocketSdkReturnType<'getIssuesByNPMPackage'>> }
111+ */
112+ async function fetchPackageData ( pkgName , pkgVersion ) {
113+ const socketSdk = await setupSdk ( )
88114 const spinner = ora ( `Looking up data for version ${ pkgVersion } of ${ pkgName } ` ) . start ( )
89-
90- /** @type {Awaited<ReturnType<typeof socketSdk.getIssuesByNPMPackage>> } */
91- let result
92-
93- try {
94- result = await socketSdk . getIssuesByNPMPackage ( pkgName , pkgVersion )
95- } catch ( cause ) {
96- spinner . fail ( )
97- throw new ErrorWithCause ( 'Failed to look up package' , { cause } )
98- }
115+ const result = await handleApiCall ( socketSdk . getIssuesByNPMPackage ( pkgName , pkgVersion ) , spinner , 'looking up package' )
99116
100117 if ( result . success === false ) {
101- if ( result . status === 401 || result . status === 403 ) {
102- spinner . stop ( )
103- throw new AuthError ( result . error . message )
104- }
105- spinner . fail ( chalk . white . bgRed ( 'API returned an error:' ) + ' ' + result . error . message )
106- process . exit ( 1 )
118+ return handleUnsuccessfulApiResponse ( result , spinner )
107119 }
108120
109- const issueSummary = getSeveritySummary ( result . data )
121+ // Conclude the status of the API call
110122
123+ const issueSummary = getSeveritySummary ( result . data )
111124 spinner . succeed ( `Found ${ issueSummary || 'no' } issues for version ${ pkgVersion } of ${ pkgName } ` )
112125
126+ return result
127+ }
128+
129+ /**
130+ * @param {import('@socketsecurity/sdk').SocketSdkReturnType<'getIssuesByNPMPackage'>["data"] } data
131+ * @param {{ name: string, outputJson: boolean, outputMarkdown: boolean, pkgName: string, pkgVersion: string } } context
132+ * @returns {void }
133+ */
134+ function formatPackageDataOutput ( data , { name, outputJson, outputMarkdown, pkgName, pkgVersion } ) {
135+ // If JSON, output and return...
136+
113137 if ( outputJson ) {
114- console . log ( JSON . stringify ( result . data , undefined , 2 ) )
138+ console . log ( JSON . stringify ( data , undefined , 2 ) )
115139 return
116140 }
117141
142+ // ...else do the CLI / Markdown output dance
143+
118144 const format = new ChalkOrMarkdown ( ! ! outputMarkdown )
119145 const url = `https://socket.dev/npm/package/${ pkgName } /overview/${ pkgVersion } `
120146
121147 console . log ( '\nDetailed info on socket.dev: ' + format . hyperlink ( `${ pkgName } v${ pkgVersion } ` , url , { fallbackToUrl : true } ) )
122-
123148 if ( ! outputMarkdown ) {
124149 console . log ( chalk . dim ( '\nOr rerun' , chalk . italic ( name ) , 'using the' , chalk . italic ( '--json' ) , 'flag to get full JSON output' ) )
125150 }
126151}
127-
128- /** @type {import('../../utils/meow-with-subcommands').CliSubcommand } */
129- export const info = { description, run }
0 commit comments