1
1
import * as core from '@actions/core'
2
2
import * as path from 'path'
3
+ import * as fs from 'fs'
3
4
import {
4
5
Idea as HLintIdea ,
5
6
Severity as HLintSeverity ,
@@ -15,6 +16,7 @@ export type CheckMode = HLintSeverity | 'STATUS' | 'NEVER';
15
16
export interface RunArgs {
16
17
baseDir : string ,
17
18
hlintCmd : string ,
19
+ jsonFile : string ,
18
20
pathList : string [ ] ,
19
21
failOn : CheckMode ,
20
22
} ;
@@ -42,6 +44,26 @@ async function runHLint(cmd: string, args: string[]): Promise<HLintResult> {
42
44
return { ideas, statusCode} ;
43
45
}
44
46
47
+ async function readHLintFile ( path : string ) : Promise < HLintResult > {
48
+ const fileContents = await fs . promises . readFile ( path , 'utf8' ) ;
49
+ const hints : Array < HLintIdea > = JSON . parse ( fileContents ) || [ ] ;
50
+ hints . forEach ( hint => {
51
+ const fromTo = hint . to
52
+ ? [ `(Found: ${ hint . from } )` , `(Perhaps: ${ hint . to } )` ]
53
+ : [ `(Remove: ${ hint . from } )` ] ;
54
+ const message = [ ...fromTo , ...hint . note ] . join ( '\n' ) ;
55
+ const properties = { ...hint , title : `${ hint . severity } : ${ hint . hint } ` } ;
56
+ if ( hint . severity == "Error" ) {
57
+ core . error ( message , properties ) ;
58
+ } else {
59
+ core . warning ( message , properties ) ;
60
+ }
61
+ } ) ;
62
+ const ideas = hints ;
63
+ const statusCode = ideas . length ;
64
+ return { ideas, statusCode} ;
65
+ }
66
+
45
67
function getOverallCheckResult ( failOn : CheckMode , { ideas, statusCode} : HLintResult ) : CheckResult {
46
68
const hintsBySev = HLINT_SEV_LEVELS . map ( sev => ( [ sev , ideas . filter ( hint => hint . severity === sev ) . length ] ) ) ;
47
69
const hintSummary = hintsBySev
@@ -66,10 +88,16 @@ function getOverallCheckResult(failOn: CheckMode, {ideas, statusCode}: HLintResu
66
88
return { ok, hintSummary}
67
89
}
68
90
69
- export default async function run ( { baseDir, hlintCmd, pathList, failOn} : RunArgs ) : Promise < RunResult > {
70
- const hlintArgs = [ '-j' , '--json' , '--' , ...pathList ]
71
- const matcherDefPath = path . join ( baseDir , MATCHER_DEF_PATH ) ;
72
- const { ideas, statusCode} = await withMatcherAtPath ( matcherDefPath , ( ) => runHLint ( hlintCmd , hlintArgs ) ) ;
73
- const { ok, hintSummary} = getOverallCheckResult ( failOn , { ideas, statusCode} ) ;
74
- return { ok, statusCode, ideas, hintSummary} ;
91
+ export default async function run ( { baseDir, hlintCmd, jsonFile, pathList, failOn} : RunArgs ) : Promise < RunResult > {
92
+ if ( jsonFile ) {
93
+ const { ideas, statusCode} = await readHLintFile ( jsonFile ) ;
94
+ const { ok, hintSummary} = getOverallCheckResult ( failOn , { ideas, statusCode} ) ;
95
+ return { ok, statusCode, ideas, hintSummary} ;
96
+ } else {
97
+ const hlintArgs = [ '-j' , '--json' , '--' , ...pathList ]
98
+ const matcherDefPath = path . join ( baseDir , MATCHER_DEF_PATH ) ;
99
+ const { ideas, statusCode} = await withMatcherAtPath ( matcherDefPath , ( ) => runHLint ( hlintCmd , hlintArgs ) ) ;
100
+ const { ok, hintSummary} = getOverallCheckResult ( failOn , { ideas, statusCode} ) ;
101
+ return { ok, statusCode, ideas, hintSummary} ;
102
+ }
75
103
}
0 commit comments