@@ -9,6 +9,7 @@ import type {
99 Diagnostic ,
1010 DiffInfo ,
1111 EstimatedScoreResult ,
12+ FailOnLevel ,
1213 ReactDoctorConfig ,
1314 ScanOptions ,
1415} from "./types.js" ;
@@ -37,8 +38,23 @@ interface CliFlags {
3738 ami : boolean ;
3839 project ?: string ;
3940 diff ?: boolean | string ;
41+ failOn : string ;
4042}
4143
44+ const VALID_FAIL_ON_LEVELS = new Set < FailOnLevel > ( [ "error" , "warning" , "none" ] ) ;
45+
46+ const isValidFailOnLevel = ( level : string ) : level is FailOnLevel =>
47+ VALID_FAIL_ON_LEVELS . has ( level as FailOnLevel ) ;
48+
49+ const shouldFailForDiagnostics = (
50+ diagnostics : Diagnostic [ ] ,
51+ failOnLevel : FailOnLevel ,
52+ ) : boolean => {
53+ if ( failOnLevel === "none" ) return false ;
54+ if ( failOnLevel === "warning" ) return diagnostics . length > 0 ;
55+ return diagnostics . some ( ( diagnostic ) => diagnostic . severity === "error" ) ;
56+ } ;
57+
4258const exitWithFixHint = ( ) => {
4359 logger . break ( ) ;
4460 logger . log ( "Cancelled." ) ;
@@ -129,6 +145,7 @@ const program = new Command()
129145 . option ( "--diff [base]" , "scan only files changed vs base branch" )
130146 . option ( "--offline" , "skip telemetry (anonymous, not stored, only used to calculate score)" )
131147 . option ( "--no-ami" , "skip Ami-related prompts" )
148+ . option ( "--fail-on <level>" , "exit with error code on diagnostics: error, warning, none" , "none" )
132149 . option ( "--fix" , "open Ami to auto-fix all issues" )
133150 . action ( async ( directory : string , flags : CliFlags ) => {
134151 const isScoreOnly = flags . score ;
@@ -203,6 +220,18 @@ const program = new Command()
203220 }
204221 }
205222
223+ const resolvedFailOn =
224+ program . getOptionValueSource ( "failOn" ) === "cli"
225+ ? flags . failOn
226+ : ( userConfig ?. failOn ?? flags . failOn ) ;
227+ const effectiveFailOn : FailOnLevel = isValidFailOnLevel ( resolvedFailOn )
228+ ? resolvedFailOn
229+ : "none" ;
230+
231+ if ( shouldFailForDiagnostics ( allDiagnostics , effectiveFailOn ) ) {
232+ process . exitCode = 1 ;
233+ }
234+
206235 if ( flags . fix ) {
207236 openAmiToFix ( resolvedDirectory ) ;
208237 }
0 commit comments