11import chalk from 'chalk'
2+ import fs from 'fs'
23import meow from 'meow'
34import ora from 'ora'
45import util from 'util'
@@ -24,7 +25,7 @@ export const get: CliSubcommand = {
2425 }
2526 const spinnerText = 'Getting diff scan... \n'
2627 const spinner = ora ( spinnerText ) . start ( )
27- await getDiffScan ( input . before , input . after , spinner , apiKey , input . orgSlug )
28+ await getDiffScan ( input , spinner , apiKey )
2829 }
2930 }
3031}
@@ -48,6 +49,12 @@ const getDiffScanFlags: { [key: string]: any } = {
4849 default : true ,
4950 description : 'A boolean flag to persist or not the diff scan result'
5051 } ,
52+ file : {
53+ type : 'string' ,
54+ shortFlag : 'f' ,
55+ default : '' ,
56+ description : 'Path to a local file where the output should be saved'
57+ }
5158}
5259
5360// Internal functions
@@ -59,6 +66,7 @@ type CommandContext = {
5966 after : string
6067 preview : boolean
6168 orgSlug : string
69+ file : string
6270}
6371
6472function setupCommand (
@@ -75,13 +83,13 @@ function setupCommand(
7583 const cli = meow (
7684 `
7785 Usage
78- $ ${ name }
86+ $ ${ name } <org slug> --before=<before> --after=<after>
7987
8088 Options
8189 ${ printFlagList ( flags , 6 ) }
8290
8391 Examples
84- $ ${ name }
92+ $ ${ name } FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
8593 ` ,
8694 {
8795 argv,
@@ -97,6 +105,7 @@ function setupCommand(
97105 before,
98106 after,
99107 preview,
108+ file
100109 } = cli . flags
101110
102111 if ( ! before || ! after ) {
@@ -123,34 +132,37 @@ function setupCommand(
123132 before,
124133 after,
125134 preview,
126- orgSlug
135+ orgSlug,
136+ file
127137 }
128138}
129139
130140async function getDiffScan (
131- before : string ,
132- after : string ,
141+ { before, after, orgSlug, file } : CommandContext ,
133142 spinner : Ora ,
134143 apiKey : string ,
135- orgSlug : string
136144) : Promise < void > {
137145 const response = await queryAPI ( `${ orgSlug } /full-scans/diff?before=${ before } &after=${ after } &preview` , apiKey )
138146 const data = await response . json ( ) ;
139147
140- if ( response . status !== 200 ) {
148+ if ( ! response . ok ) {
141149 spinner . stop ( )
142150 const err = await handleAPIError ( response . status )
143- console . error ( err )
151+ console . error (
152+ `${ chalk . bgRed . white ( response . statusText ) } : ${ err } \n`
153+ )
144154 return
145155 }
146156
147157 spinner . stop ( )
148158
149- // before: dfc4cf0c-aefd-4081-9e4e-7385257f26e2
150- // after: 922e45f5-8a7b-4b16-95a5-e98ad00470f1
159+ if ( file ) {
160+ fs . writeFile ( file , JSON . stringify ( data ) , err => {
161+ err ? console . error ( err ) : console . log ( `Data successfully written to ${ file } ` )
162+ } )
163+ return
164+ }
151165
152166 console . log ( `\n Diff scan result: \n` )
153- // console.log(data);
154-
155167 console . log ( util . inspect ( data , { showHidden : false , depth : null , colors : true } ) )
156168}
0 commit comments