11// @ts -ignore
22import blessed from 'blessed'
33import contrib from 'blessed-contrib'
4+ import fs from 'fs'
45import meow from 'meow'
56import ora from 'ora'
67
@@ -28,10 +29,10 @@ export const analytics: CliSubcommand = {
2829 }
2930 const spinner = ora ( 'Fetching analytics data' ) . start ( )
3031 if ( input . scope === 'org' ) {
31- await fetchOrgAnalyticsData ( input . time , spinner , apiKey , input . outputJson )
32+ await fetchOrgAnalyticsData ( input . time , spinner , apiKey , input . outputJson , input . file )
3233 } else {
3334 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 )
3536 }
3637 }
3738 }
@@ -57,6 +58,12 @@ const analyticsFlags = {
5758 default : '' ,
5859 description : "Name of the repository"
5960 } ,
61+ file : {
62+ type : 'string' ,
63+ shortFlag : 'f' ,
64+ default : '' ,
65+ description : "Path to a local file to save the output"
66+ }
6067}
6168
6269// Internal functions
@@ -66,6 +73,7 @@ type CommandContext = {
6673 time : number
6774 repo : string
6875 outputJson : boolean
76+ file : string
6977}
7078
7179function 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[
96104 json : outputJson ,
97105 scope,
98106 time,
99- repo
107+ repo,
108+ file
100109 } = cli . flags
101110
102111 if ( scope !== 'org' && scope !== 'repo' ) {
@@ -116,7 +125,7 @@ function setupCommand (name: string, description: string, argv: readonly string[
116125 }
117126
118127 return < CommandContext > {
119- scope, time, repo, outputJson
128+ scope, time, repo, outputJson, file
120129 }
121130}
122131
@@ -135,7 +144,7 @@ const METRICS = [
135144 'total_low_prevented'
136145]
137146
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 > {
139148 const socketSdk = await setupSdk ( apiKey )
140149 const result = await handleApiCall ( socketSdk . getOrgAnalytics ( time . toString ( ) ) , 'fetching analytics data' )
141150
@@ -151,10 +160,17 @@ async function fetchOrgAnalyticsData (time: number, spinner: Ora, apiKey: string
151160
152161 const data = formatData ( result . data , 'org' )
153162
154- if ( outputJson ) {
163+ if ( outputJson && ! filePath ) {
155164 return console . log ( result . data )
156165 }
157166
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+
158174 return displayAnalyticsScreen ( data )
159175}
160176
@@ -256,7 +272,7 @@ const formatData = (data: any, scope: string) => {
256272 return { ...formattedData , top_five_alert_types : sortedTopFivealerts }
257273}
258274
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 > {
260276 const socketSdk = await setupSdk ( apiKey )
261277 const result = await handleApiCall ( socketSdk . getRepoAnalytics ( repo , time . toString ( ) ) , 'fetching analytics data' )
262278
@@ -271,10 +287,17 @@ async function fetchRepoAnalyticsData (repo: string, time: number, spinner: Ora,
271287
272288 const data = formatData ( result . data , 'repo' )
273289
274- if ( outputJson ) {
290+ if ( outputJson && ! filePath ) {
275291 return console . log ( result . data )
276292 }
277293
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+
278301 return displayAnalyticsScreen ( data )
279302}
280303
0 commit comments