@@ -26,10 +26,10 @@ export const analytics: CliSubcommand = {
2626 }
2727 const spinner = ora ( 'Fetching analytics data' ) . start ( )
2828 if ( input . scope === 'org' ) {
29- await fetchOrgAnalyticsData ( input . time , spinner , apiKey )
29+ await fetchOrgAnalyticsData ( input . time , spinner , apiKey , input . outputJson )
3030 } else {
3131 if ( input . repo ) {
32- await fetchRepoAnalyticsData ( input . repo , input . time , spinner , apiKey )
32+ await fetchRepoAnalyticsData ( input . repo , input . time , spinner , apiKey , input . outputJson )
3333 }
3434 }
3535 }
@@ -42,6 +42,7 @@ type CommandContext = {
4242 scope : string
4343 time : string
4444 repo : string | undefined
45+ outputJson : boolean
4546}
4647
4748function setupCommand ( name : string , description : string , argv : readonly string [ ] , importMeta : ImportMeta ) : void | CommandContext {
@@ -67,6 +68,10 @@ function setupCommand (name: string, description: string, argv: readonly string[
6768 flags
6869 } )
6970
71+ const {
72+ json : outputJson
73+ } = cli . flags
74+
7075 const scope = cli . input [ 0 ]
7176
7277 if ( ! scope ) {
@@ -93,12 +98,12 @@ function setupCommand (name: string, description: string, argv: readonly string[
9398 throw new InputError ( 'The time filter must either be 7, 30 or 60' )
9499 }
95100
96- return {
97- scope, time, repo
101+ return < CommandContext > {
102+ scope, time, repo, outputJson
98103 }
99104}
100105
101- async function fetchOrgAnalyticsData ( time : string , spinner : Ora , apiKey : string ) : Promise < void > {
106+ async function fetchOrgAnalyticsData ( time : string , spinner : Ora , apiKey : string , outputJson : boolean ) : Promise < void > {
102107 const socketSdk = await setupSdk ( apiKey )
103108 const result = await handleApiCall ( socketSdk . getOrgAnalytics ( time ) , 'fetching analytics data' )
104109
@@ -128,6 +133,10 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
128133 return acc
129134 } , { } )
130135
136+ if ( outputJson ) {
137+ return console . log ( data )
138+ }
139+
131140 const screen = blessed . screen ( )
132141 // eslint-disable-next-line
133142 const grid = new contrib . grid ( { rows : 4 , cols : 4 , screen} )
@@ -161,7 +170,7 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string
161170 } )
162171}
163172
164- async function fetchRepoAnalyticsData ( repo : string , time : string , spinner : Ora , apiKey : string ) : Promise < void > {
173+ async function fetchRepoAnalyticsData ( repo : string , time : string , spinner : Ora , apiKey : string , outputJson : boolean ) : Promise < void > {
165174 const socketSdk = await setupSdk ( apiKey )
166175 const result = await handleApiCall ( socketSdk . getRepoAnalytics ( repo , time ) , 'fetching analytics data' )
167176
@@ -190,6 +199,10 @@ async function fetchRepoAnalyticsData (repo: string, time: string, spinner: Ora,
190199 return acc
191200 } , { } )
192201
202+ if ( outputJson ) {
203+ return console . log ( data )
204+ }
205+
193206 const screen = blessed . screen ( )
194207 // eslint-disable-next-line
195208 const grid = new contrib . grid ( { rows : 4 , cols : 4 , screen} )
0 commit comments