1
1
import chalk from 'chalk'
2
+ import fs from 'fs'
2
3
import meow from 'meow'
3
4
import ora from 'ora'
4
5
import util from 'util'
@@ -24,7 +25,7 @@ export const get: CliSubcommand = {
24
25
}
25
26
const spinnerText = 'Getting diff scan... \n'
26
27
const spinner = ora ( spinnerText ) . start ( )
27
- await getDiffScan ( input . before , input . after , spinner , apiKey , input . orgSlug )
28
+ await getDiffScan ( input , spinner , apiKey )
28
29
}
29
30
}
30
31
}
@@ -48,6 +49,12 @@ const getDiffScanFlags: { [key: string]: any } = {
48
49
default : true ,
49
50
description : 'A boolean flag to persist or not the diff scan result'
50
51
} ,
52
+ file : {
53
+ type : 'string' ,
54
+ shortFlag : 'f' ,
55
+ default : '' ,
56
+ description : 'Path to a local file where the output should be saved'
57
+ }
51
58
}
52
59
53
60
// Internal functions
@@ -59,6 +66,7 @@ type CommandContext = {
59
66
after : string
60
67
preview : boolean
61
68
orgSlug : string
69
+ file : string
62
70
}
63
71
64
72
function setupCommand (
@@ -75,13 +83,13 @@ function setupCommand(
75
83
const cli = meow (
76
84
`
77
85
Usage
78
- $ ${ name }
86
+ $ ${ name } <org slug> --before=<before> --after=<after>
79
87
80
88
Options
81
89
${ printFlagList ( flags , 6 ) }
82
90
83
91
Examples
84
- $ ${ name }
92
+ $ ${ name } FakeCorp --before=aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 --after=aaa1aa1a-aaaa-1111-1a1a-1111111a11a1
85
93
` ,
86
94
{
87
95
argv,
@@ -97,6 +105,7 @@ function setupCommand(
97
105
before,
98
106
after,
99
107
preview,
108
+ file
100
109
} = cli . flags
101
110
102
111
if ( ! before || ! after ) {
@@ -123,34 +132,37 @@ function setupCommand(
123
132
before,
124
133
after,
125
134
preview,
126
- orgSlug
135
+ orgSlug,
136
+ file
127
137
}
128
138
}
129
139
130
140
async function getDiffScan (
131
- before : string ,
132
- after : string ,
141
+ { before, after, orgSlug, file } : CommandContext ,
133
142
spinner : Ora ,
134
143
apiKey : string ,
135
- orgSlug : string
136
144
) : Promise < void > {
137
145
const response = await queryAPI ( `${ orgSlug } /full-scans/diff?before=${ before } &after=${ after } &preview` , apiKey )
138
146
const data = await response . json ( ) ;
139
147
140
- if ( response . status !== 200 ) {
148
+ if ( ! response . ok ) {
141
149
spinner . stop ( )
142
150
const err = await handleAPIError ( response . status )
143
- console . error ( err )
151
+ console . error (
152
+ `${ chalk . bgRed . white ( response . statusText ) } : ${ err } \n`
153
+ )
144
154
return
145
155
}
146
156
147
157
spinner . stop ( )
148
158
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
+ }
151
165
152
166
console . log ( `\n Diff scan result: \n` )
153
- // console.log(data);
154
-
155
167
console . log ( util . inspect ( data , { showHidden : false , depth : null , colors : true } ) )
156
168
}
0 commit comments