Skip to content

Commit e9364af

Browse files
deongroenewaldplantain-00
authored andcommitted
Add JSON Output argument to CLI
1 parent 5085b1e commit e9364af

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

packages/cli/src/index.ts

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ import * as packageJson from '../package.json'
77
import { lint } from 'type-coverage-core'
88

99
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+
1037
const existsAsync = util.promisify(fs.exists)
1138
const readFileAsync = util.promisify(fs.readFile)
1239
const writeFileAsync = util.promisify(fs.writeFile)
@@ -44,6 +71,7 @@ function printHelp() {
4471
-- file1.ts file2.ts ... string[]? only checks these files, useful for usage with tools like lint-staged
4572
--cache-directory string? set cache directory
4673
--not-only-in-cwd boolean? include results outside current working directory
74+
--json-output boolean? output results as JSON
4775
`)
4876
}
4977

@@ -85,6 +113,7 @@ interface CliArgs extends BaseArgs {
85113
['report-semantic-error']: boolean
86114
['cache-directory']: string
87115
['not-only-in-cwd']: boolean
116+
['json-output']: boolean
88117
}
89118

90119
interface PkgArgs extends BaseArgs {
@@ -107,6 +136,7 @@ interface PkgArgs extends BaseArgs {
107136
reportSemanticError: boolean
108137
cacheDirectory: string
109138
notOnlyInCWD: boolean
139+
jsonOutput: boolean
110140
}
111141

112142
interface PackageJson {
@@ -180,13 +210,27 @@ async function executeCommandLine() {
180210
const isFailed = is && percent !== is
181211

182212
if (detail || (!noDetailWhenFailed && (atLeastFailed || isFailed))) {
213+
output.details = []
183214
for (const { file, line, character, text } of anys) {
184215
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+
})
186222
}
187223
}
188224
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
190234

191235
if (update) {
192236
await saveTarget(+percentString)
@@ -234,6 +278,7 @@ async function getTarget(argv: CliArgs) {
234278
}
235279

236280
suppressError = getArgOrCfgVal(['suppressError']) || false
281+
jsonOutput = getArgOrCfgVal(['json-output', 'jsonOutput']) || false
237282

238283
const atLeast = getArgOrCfgVal(['at-least', 'atLeast'])
239284
const debug = getArgOrCfgVal(['debug'])
@@ -329,14 +374,44 @@ async function saveHistory(percentage: number, historyFile?:string) {
329374
}
330375
}
331376

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+
332402
executeCommandLine().then(() => {
333-
console.log('type-coverage success.')
403+
output.succeeded = true;
404+
printOutput(output, jsonOutput)
334405
}, (error: Error | string) => {
406+
output.succeeded = false;
335407
if (error instanceof Error) {
336-
console.log(error.message)
408+
output.error = error.message
337409
} else {
338-
console.log(error)
410+
output.error = error
339411
}
412+
413+
printOutput(output, jsonOutput)
414+
340415
if (!suppressError) {
341416
process.exit(1)
342417
}

0 commit comments

Comments
 (0)