Skip to content

Commit 4695377

Browse files
author
Pelle Wessman
committed
Sync up report create command with report view
1 parent ede722c commit 4695377

File tree

1 file changed

+60
-28
lines changed

1 file changed

+60
-28
lines changed

lib/commands/report/create.js

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,47 @@
33
import { stat } from 'node:fs/promises'
44
import path from 'node:path'
55

6-
import chalk from 'chalk'
76
import meow from 'meow'
87
import ora from 'ora'
98
import { ErrorWithCause } from 'pony-cause'
109

10+
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api-helpers.js'
1111
import { ChalkOrMarkdown, logSymbols } from '../../utils/chalk-markdown.js'
12-
import { AuthError, InputError } from '../../utils/errors.js'
12+
import { InputError } from '../../utils/errors.js'
1313
import { printFlagList } from '../../utils/formatting.js'
1414
import { createDebugLogger } from '../../utils/misc.js'
1515
import { setupSdk } from '../../utils/sdk.js'
1616
import { isErrnoException } from '../../utils/type-helpers.js'
1717

18-
const description = 'Create a project report'
18+
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
19+
export const create = {
20+
description: 'Create a project report',
21+
async run (argv, importMeta, { parentName }) {
22+
const name = parentName + ' view'
23+
24+
const input = await setupCommand(name, create.description, argv, importMeta)
25+
26+
if (input) {
27+
const { cwd, debugLog, dryRun, outputJson, outputMarkdown, packagePaths } = input
28+
const result = input && await createReport(packagePaths, { cwd, debugLog, dryRun })
29+
30+
if (result) {
31+
formatReportCreationOutput(result.data, { outputJson, outputMarkdown })
32+
}
33+
}
34+
}
35+
}
1936

20-
/** @type {import('../../utils/meow-with-subcommands').CliSubcommandRun} */
21-
const run = async (argv, importMeta, { parentName }) => {
22-
const name = parentName + ' create'
37+
// Internal functions
2338

39+
/**
40+
* @param {string} name
41+
* @param {string} description
42+
* @param {readonly string[]} argv
43+
* @param {ImportMeta} importMeta
44+
* @returns {Promise<void|{ cwd: string, debugLog: typeof console.error, dryRun: boolean, outputJson: boolean, outputMarkdown: boolean, packagePaths: string[] }>}
45+
*/
46+
async function setupCommand (name, description, argv, importMeta) {
2447
const cli = meow(`
2548
Usage
2649
$ ${name} <paths-to-package-folders-and-files>
@@ -80,50 +103,59 @@ const run = async (argv, importMeta, { parentName }) => {
80103
const cwd = process.cwd()
81104
const packagePaths = await resolvePackagePaths(cwd, cli.input)
82105

106+
return {
107+
cwd,
108+
debugLog,
109+
dryRun,
110+
outputJson,
111+
outputMarkdown,
112+
packagePaths
113+
}
114+
}
115+
116+
/**
117+
* @param {string[]} packagePaths
118+
* @param {{ cwd: string, debugLog: typeof console.error, dryRun: boolean }} context
119+
* @returns {Promise<void|import('@socketsecurity/sdk').SocketSdkReturnType<'createReport'>>}
120+
*/
121+
async function createReport (packagePaths, { cwd, debugLog, dryRun }) {
83122
debugLog(`${logSymbols.info} Uploading:`, packagePaths.join(`\n${logSymbols.info} Uploading:`))
84123

85124
if (dryRun) {
86125
return
87126
}
88127

89128
const socketSdk = await setupSdk()
90-
91129
const spinner = ora(`Creating report with ${packagePaths.length} package files`).start()
92-
93-
/** @type {Awaited<ReturnType<typeof socketSdk.createReportFromFilePaths>>} */
94-
let result
95-
96-
try {
97-
result = await socketSdk.createReportFromFilePaths(packagePaths, cwd)
98-
} catch (cause) {
99-
spinner.fail()
100-
throw new ErrorWithCause('Failed creating report', { cause })
101-
}
130+
const result = await handleApiCall(socketSdk.createReportFromFilePaths(packagePaths, cwd), spinner, 'creating report')
102131

103132
if (result.success === false) {
104-
if (result.status === 401 || result.status === 403) {
105-
spinner.stop()
106-
throw new AuthError(result.error.message)
107-
}
108-
spinner.fail(chalk.white.bgRed('API returned an error:') + ' ' + result.error.message)
109-
process.exit(1)
133+
return handleUnsuccessfulApiResponse(result, spinner)
110134
}
111135

136+
// Conclude the status of the API call
137+
112138
spinner.succeed()
113139

140+
return result
141+
}
142+
143+
/**
144+
* @param {import('@socketsecurity/sdk').SocketSdkReturnType<'createReport'>["data"]} data
145+
* @param {{ outputJson: boolean, outputMarkdown: boolean }} context
146+
* @returns {void}
147+
*/
148+
function formatReportCreationOutput (data, { outputJson, outputMarkdown }) {
114149
if (outputJson) {
115-
console.log(JSON.stringify(result.data, undefined, 2))
150+
console.log(JSON.stringify(data, undefined, 2))
116151
return
117152
}
118153

119154
const format = new ChalkOrMarkdown(!!outputMarkdown)
120155

121-
console.log('\nNew report: ' + format.hyperlink(result.data.id, result.data.url, { fallbackToUrl: true }))
156+
console.log('\nNew report: ' + format.hyperlink(data.id, data.url, { fallbackToUrl: true }))
122157
}
123158

124-
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
125-
export const create = { description, run }
126-
127159
// TODO: Add globbing support with support for ignoring, as a "./**/package.json" in a project also traverses eg. node_modules
128160
/**
129161
* Takes paths to folders and/or package.json / package-lock.json files and resolves to package.json + package-lock.json pairs (where feasible)

0 commit comments

Comments
 (0)