1
1
// @ts -ignore
2
2
import blessed from 'blessed'
3
3
import contrib from 'blessed-contrib'
4
+ import fs from 'fs'
4
5
import meow from 'meow'
5
6
import ora from 'ora'
6
7
@@ -28,10 +29,10 @@ export const analytics: CliSubcommand = {
28
29
}
29
30
const spinner = ora ( 'Fetching analytics data' ) . start ( )
30
31
if ( input . scope === 'org' ) {
31
- await fetchOrgAnalyticsData ( input . time , spinner , apiKey , input . outputJson )
32
+ await fetchOrgAnalyticsData ( input . time , spinner , apiKey , input . outputJson , input . file )
32
33
} else {
33
34
if ( input . repo ) {
34
- await fetchRepoAnalyticsData ( input . repo , input . time , spinner , apiKey , input . outputJson )
35
+ await fetchRepoAnalyticsData ( input . repo , input . time , spinner , apiKey , input . outputJson , input . file )
35
36
}
36
37
}
37
38
}
@@ -57,6 +58,12 @@ const analyticsFlags = {
57
58
default : '' ,
58
59
description : "Name of the repository"
59
60
} ,
61
+ file : {
62
+ type : 'string' ,
63
+ shortFlag : 'f' ,
64
+ default : '' ,
65
+ description : "Path to a local file to save the output"
66
+ }
60
67
}
61
68
62
69
// Internal functions
@@ -66,6 +73,7 @@ type CommandContext = {
66
73
time : number
67
74
repo : string
68
75
outputJson : boolean
76
+ file : string
69
77
}
70
78
71
79
function setupCommand ( name : string , description : string , argv : readonly string [ ] , importMeta : ImportMeta ) : void | CommandContext {
@@ -96,7 +104,8 @@ function setupCommand (name: string, description: string, argv: readonly string[
96
104
json : outputJson ,
97
105
scope,
98
106
time,
99
- repo
107
+ repo,
108
+ file
100
109
} = cli . flags
101
110
102
111
if ( scope !== 'org' && scope !== 'repo' ) {
@@ -116,7 +125,7 @@ function setupCommand (name: string, description: string, argv: readonly string[
116
125
}
117
126
118
127
return < CommandContext > {
119
- scope, time, repo, outputJson
128
+ scope, time, repo, outputJson, file
120
129
}
121
130
}
122
131
@@ -135,7 +144,7 @@ const METRICS = [
135
144
'total_low_prevented'
136
145
]
137
146
138
- async function fetchOrgAnalyticsData ( time : number , spinner : Ora , apiKey : string , outputJson : boolean ) : Promise < void > {
147
+ async function fetchOrgAnalyticsData ( time : number , spinner : Ora , apiKey : string , outputJson : boolean , filePath : string ) : Promise < void > {
139
148
const socketSdk = await setupSdk ( apiKey )
140
149
const result = await handleApiCall ( socketSdk . getOrgAnalytics ( time . toString ( ) ) , 'fetching analytics data' )
141
150
@@ -151,10 +160,17 @@ async function fetchOrgAnalyticsData (time: number, spinner: Ora, apiKey: string
151
160
152
161
const data = formatData ( result . data , 'org' )
153
162
154
- if ( outputJson ) {
163
+ if ( outputJson && ! filePath ) {
155
164
return console . log ( result . data )
156
165
}
157
166
167
+ if ( filePath ) {
168
+ fs . writeFile ( filePath , JSON . stringify ( result . data ) , err => {
169
+ err ? console . error ( err ) : console . log ( `Data successfully written to ${ filePath } ` )
170
+ } )
171
+ return
172
+ }
173
+
158
174
return displayAnalyticsScreen ( data )
159
175
}
160
176
@@ -256,7 +272,7 @@ const formatData = (data: any, scope: string) => {
256
272
return { ...formattedData , top_five_alert_types : sortedTopFivealerts }
257
273
}
258
274
259
- async function fetchRepoAnalyticsData ( repo : string , time : number , spinner : Ora , apiKey : string , outputJson : boolean ) : Promise < void > {
275
+ async function fetchRepoAnalyticsData ( repo : string , time : number , spinner : Ora , apiKey : string , outputJson : boolean , filePath : string ) : Promise < void > {
260
276
const socketSdk = await setupSdk ( apiKey )
261
277
const result = await handleApiCall ( socketSdk . getRepoAnalytics ( repo , time . toString ( ) ) , 'fetching analytics data' )
262
278
@@ -271,10 +287,17 @@ async function fetchRepoAnalyticsData (repo: string, time: number, spinner: Ora,
271
287
272
288
const data = formatData ( result . data , 'repo' )
273
289
274
- if ( outputJson ) {
290
+ if ( outputJson && ! filePath ) {
275
291
return console . log ( result . data )
276
292
}
277
293
294
+ if ( filePath ) {
295
+ fs . writeFile ( filePath , JSON . stringify ( result . data ) , err => {
296
+ err ? console . error ( err ) : console . log ( `Data successfully written to ${ filePath } ` )
297
+ } )
298
+ return
299
+ }
300
+
278
301
return displayAnalyticsScreen ( data )
279
302
}
280
303
0 commit comments