Skip to content

Commit 71fd817

Browse files
author
Pelle Wessman
committed
Add --view flag to report create
1 parent 4695377 commit 71fd817

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

lib/commands/report/create.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,37 @@ 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'
17+
import { fetchReportData, formatReportDataOutput } from './view.js'
1718

1819
/** @type {import('../../utils/meow-with-subcommands').CliSubcommand} */
1920
export const create = {
2021
description: 'Create a project report',
2122
async run (argv, importMeta, { parentName }) {
22-
const name = parentName + ' view'
23+
const name = parentName + ' create'
2324

2425
const input = await setupCommand(name, create.description, argv, importMeta)
2526

2627
if (input) {
27-
const { cwd, debugLog, dryRun, outputJson, outputMarkdown, packagePaths } = input
28+
const {
29+
cwd,
30+
debugLog,
31+
dryRun,
32+
outputJson,
33+
outputMarkdown,
34+
packagePaths,
35+
view,
36+
} = input
37+
2838
const result = input && await createReport(packagePaths, { cwd, debugLog, dryRun })
2939

30-
if (result) {
40+
if (result && view) {
41+
const reportId = result.data.id
42+
const reportResult = input && await fetchReportData(reportId)
43+
44+
if (reportResult) {
45+
formatReportDataOutput(reportResult.data, { name, outputJson, outputMarkdown, reportId })
46+
}
47+
} else if (result) {
3148
formatReportCreationOutput(result.data, { outputJson, outputMarkdown })
3249
}
3350
}
@@ -41,7 +58,7 @@ export const create = {
4158
* @param {string} description
4259
* @param {readonly string[]} argv
4360
* @param {ImportMeta} importMeta
44-
* @returns {Promise<void|{ cwd: string, debugLog: typeof console.error, dryRun: boolean, outputJson: boolean, outputMarkdown: boolean, packagePaths: string[] }>}
61+
* @returns {Promise<void|{ cwd: string, debugLog: typeof console.error, dryRun: boolean, outputJson: boolean, outputMarkdown: boolean, packagePaths: string[], view: boolean }>}
4562
*/
4663
async function setupCommand (name, description, argv, importMeta) {
4764
const cli = meow(`
@@ -54,6 +71,7 @@ async function setupCommand (name, description, argv, importMeta) {
5471
'--dry-run': 'Only output what will be done without actually doing it',
5572
'--json': 'Output result as json',
5673
'--markdown': 'Output result as markdown',
74+
'--view': 'Will wait for and return the created report'
5775
}, 6)}
5876
5977
Examples
@@ -84,13 +102,19 @@ async function setupCommand (name, description, argv, importMeta) {
84102
alias: 'm',
85103
default: false,
86104
},
105+
view: {
106+
type: 'boolean',
107+
alias: 'v',
108+
default: false,
109+
},
87110
}
88111
})
89112

90113
const {
91114
dryRun,
92115
json: outputJson,
93116
markdown: outputMarkdown,
117+
view,
94118
} = cli.flags
95119

96120
if (!cli.input[0]) {
@@ -109,7 +133,8 @@ async function setupCommand (name, description, argv, importMeta) {
109133
dryRun,
110134
outputJson,
111135
outputMarkdown,
112-
packagePaths
136+
packagePaths,
137+
view,
113138
}
114139
}
115140

lib/commands/report/view.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function setupCommand (name, description, argv, importMeta) {
100100
* @param {string} reportId
101101
* @returns {Promise<void|import('@socketsecurity/sdk').SocketSdkReturnType<'getReport'>>}
102102
*/
103-
async function fetchReportData (reportId) {
103+
export async function fetchReportData (reportId) {
104104
// Do the API call
105105

106106
const socketSdk = await setupSdk()
@@ -124,7 +124,7 @@ async function fetchReportData (reportId) {
124124
* @param {{ name: string, outputJson: boolean, outputMarkdown: boolean, reportId: string }} context
125125
* @returns {void}
126126
*/
127-
function formatReportDataOutput (data, { name, outputJson, outputMarkdown, reportId }) {
127+
export function formatReportDataOutput (data, { name, outputJson, outputMarkdown, reportId }) {
128128
// If JSON, output and return...
129129

130130
if (outputJson) {

0 commit comments

Comments
 (0)