@@ -7,6 +7,33 @@ import * as packageJson from '../package.json'
7
7
import { lint } from 'type-coverage-core'
8
8
9
9
let suppressError = false
10
+ let jsonOutput = false
11
+
12
+ interface LintResult {
13
+ character : number
14
+ filePath : string
15
+ line : number
16
+ text : string
17
+ }
18
+
19
+ interface Output {
20
+ atLeastFailed ?: boolean
21
+ isFailed ?: boolean | 0
22
+ succeeded : boolean
23
+ correctCount ?: number
24
+ totalCount ?: number
25
+ percent ?: number
26
+ percentString ?: string
27
+ atLeast ?: boolean | number
28
+ is ?: number
29
+ details ?: LintResult [ ]
30
+ error ?: string | unknown
31
+ }
32
+
33
+ const output : Output = {
34
+ succeeded : false ,
35
+ }
36
+
10
37
const existsAsync = util . promisify ( fs . exists )
11
38
const readFileAsync = util . promisify ( fs . readFile )
12
39
const writeFileAsync = util . promisify ( fs . writeFile )
@@ -44,6 +71,7 @@ function printHelp() {
44
71
-- file1.ts file2.ts ... string[]? only checks these files, useful for usage with tools like lint-staged
45
72
--cache-directory string? set cache directory
46
73
--not-only-in-cwd boolean? include results outside current working directory
74
+ --json-output boolean? output results as JSON
47
75
` )
48
76
}
49
77
@@ -85,6 +113,7 @@ interface CliArgs extends BaseArgs {
85
113
[ 'report-semantic-error' ] : boolean
86
114
[ 'cache-directory' ] : string
87
115
[ 'not-only-in-cwd' ] : boolean
116
+ [ 'json-output' ] : boolean
88
117
}
89
118
90
119
interface PkgArgs extends BaseArgs {
@@ -107,6 +136,7 @@ interface PkgArgs extends BaseArgs {
107
136
reportSemanticError : boolean
108
137
cacheDirectory : string
109
138
notOnlyInCWD : boolean
139
+ jsonOutput : boolean
110
140
}
111
141
112
142
interface PackageJson {
@@ -180,13 +210,27 @@ async function executeCommandLine() {
180
210
const isFailed = is && percent !== is
181
211
182
212
if ( detail || ( ! noDetailWhenFailed && ( atLeastFailed || isFailed ) ) ) {
213
+ output . details = [ ]
183
214
for ( const { file, line, character, text } of anys ) {
184
215
const filePath = showRelativePath ? file : path . resolve ( process . cwd ( ) , file )
185
- console . log ( `${ filePath } :${ line + 1 } :${ character + 1 } : ${ text } ` )
216
+ output . details . push ( {
217
+ character,
218
+ filePath,
219
+ line,
220
+ text
221
+ } )
186
222
}
187
223
}
188
224
const percentString = percent . toFixed ( 2 )
189
- console . log ( `${ correctCount } / ${ totalCount } ${ percentString } %` )
225
+
226
+ output . atLeast = atLeast
227
+ output . atLeastFailed = atLeastFailed
228
+ output . correctCount = correctCount
229
+ output . is = is
230
+ output . isFailed = isFailed
231
+ output . percent = percent
232
+ output . percentString = percentString
233
+ output . totalCount = totalCount
190
234
191
235
if ( update ) {
192
236
await saveTarget ( + percentString )
@@ -234,6 +278,7 @@ async function getTarget(argv: CliArgs) {
234
278
}
235
279
236
280
suppressError = getArgOrCfgVal ( [ 'suppressError' ] ) || false
281
+ jsonOutput = getArgOrCfgVal ( [ 'json-output' , 'jsonOutput' ] ) || false
237
282
238
283
const atLeast = getArgOrCfgVal ( [ 'at-least' , 'atLeast' ] )
239
284
const debug = getArgOrCfgVal ( [ 'debug' ] )
@@ -329,14 +374,44 @@ async function saveHistory(percentage: number, historyFile?:string) {
329
374
}
330
375
}
331
376
377
+ function printOutput ( output : Output , asJson : boolean ) {
378
+ if ( asJson ) {
379
+ console . log ( JSON . stringify ( output , null , 2 ) )
380
+ return
381
+ }
382
+
383
+ const { details, correctCount, error, totalCount, percentString, succeeded} = output
384
+
385
+ for ( const detail of details || [ ] ) {
386
+ const { filePath, line, character, text } = detail
387
+ console . log ( `${ filePath } :${ line + 1 } :${ character + 1 } : ${ text } ` )
388
+ }
389
+
390
+ if ( percentString ) {
391
+ console . log ( `${ correctCount } / ${ totalCount } ${ percentString } %` )
392
+ }
393
+
394
+ if ( succeeded ) {
395
+ console . log ( 'type-coverage success.' )
396
+ } else {
397
+ console . log ( error )
398
+ }
399
+
400
+ }
401
+
332
402
executeCommandLine ( ) . then ( ( ) => {
333
- console . log ( 'type-coverage success.' )
403
+ output . succeeded = true ;
404
+ printOutput ( output , jsonOutput )
334
405
} , ( error : Error | string ) => {
406
+ output . succeeded = false ;
335
407
if ( error instanceof Error ) {
336
- console . log ( error . message )
408
+ output . error = error . message
337
409
} else {
338
- console . log ( error )
410
+ output . error = error
339
411
}
412
+
413
+ printOutput ( output , jsonOutput )
414
+
340
415
if ( ! suppressError ) {
341
416
process . exit ( 1 )
342
417
}
0 commit comments