11import * as core from '@actions/core'
22import * as path from 'path'
3+ import * as fs from 'fs'
34import {
45 Idea as HLintIdea ,
56 Severity as HLintSeverity ,
@@ -15,6 +16,7 @@ export type CheckMode = HLintSeverity | 'STATUS' | 'NEVER';
1516export interface RunArgs {
1617 baseDir : string ,
1718 hlintCmd : string ,
19+ jsonFile : string ,
1820 pathList : string [ ] ,
1921 failOn : CheckMode ,
2022} ;
@@ -42,6 +44,26 @@ async function runHLint(cmd: string, args: string[]): Promise<HLintResult> {
4244 return { ideas, statusCode} ;
4345}
4446
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+
4567function getOverallCheckResult ( failOn : CheckMode , { ideas, statusCode} : HLintResult ) : CheckResult {
4668 const hintsBySev = HLINT_SEV_LEVELS . map ( sev => ( [ sev , ideas . filter ( hint => hint . severity === sev ) . length ] ) ) ;
4769 const hintSummary = hintsBySev
@@ -66,10 +88,16 @@ function getOverallCheckResult(failOn: CheckMode, {ideas, statusCode}: HLintResu
6688 return { ok, hintSummary}
6789}
6890
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+ }
75103}
0 commit comments