@@ -32,82 +32,102 @@ export function contentCheckTaskFactory(
3232 task : ListrTaskWrapper < Context , ListrDefaultRenderer >
3333 ) {
3434 const { name, type, contentPattern, contentPatternFlags } = definition ;
35- const files = getCheckFiles (
36- definition ,
37- DEFAULT_EXCLUDE_FILES_PATTERN_CONTENT
38- ) ;
3935
40- task . title = `${ task . title } , found ${ files . length } files` ;
41-
42- if ( ! files . length ) {
43- ctx . results . checks ! [ name ] = {
44- name,
45- type,
46- value : false ,
47- } ;
48- task . title = `${ CheckResultSymbol . UNFULFILLED } ${ task . title } ` ;
49- resolveCheckParentTaskProgress ( parentTask ) ;
50- return ;
51- }
52-
53- return new Promise < void > ( ( resolve , reject ) => {
54- setTimeout (
55- ( ) => reject ( `Check "${ name } " timeout` ) ,
56- DEFAULT_CHECK_EXECUTION_TIMEOUT
36+ try {
37+ const files = getCheckFiles (
38+ definition ,
39+ DEFAULT_EXCLUDE_FILES_PATTERN_CONTENT
5740 ) ;
5841
59- let finishedCounter = 0 ;
60- const matches : ProjectCheckMatch [ ] = [ ] ;
61- for ( const file of files ) {
62- const content = fs . readFile ( file ) ;
42+ task . title = `${ task . title } , found ${ files . length } files` ;
6343
64- const activeContentPatternFlags = resolveActiveFlags (
65- contentPatternFlags ,
66- DEFAULT_CONTENT_PATTERN_FLAGS
44+ if ( ! files . length ) {
45+ ctx . results . checks ! [ name ] = {
46+ name,
47+ type,
48+ value : false ,
49+ } ;
50+ task . title = `${ CheckResultSymbol . UNFULFILLED } ${ task . title } ` ;
51+ resolveCheckParentTaskProgress ( parentTask ) ;
52+ return ;
53+ }
54+
55+ return new Promise < void > ( ( resolve , reject ) => {
56+ setTimeout (
57+ ( ) => reject ( `Check "${ name } " timeout` ) ,
58+ DEFAULT_CHECK_EXECUTION_TIMEOUT
6759 ) ;
68- const regexp = new RegExp ( contentPattern , activeContentPatternFlags ) ;
6960
70- const matchesForFile = [ ] ;
61+ let finishedCounter = 0 ;
62+ const errors : Error [ ] = [ ] ;
63+ const matches : ProjectCheckMatch [ ] = [ ] ;
64+ for ( const file of files ) {
65+ try {
66+ const content = fs . readFile ( file ) ;
7167
72- let match ;
73- if ( activeContentPatternFlags . includes ( 'g' ) ) {
74- // const matchesForFile = [...(content as any).matchAll(regexp)]; // TODO node v12+
75- while ( ( match = regexp . exec ( content ) ) !== null ) {
76- matchesForFile . push ( match ) ;
77- }
78- } else {
79- match = regexp . exec ( content ) ;
80- if ( match ) {
81- matchesForFile . push ( match ) ;
68+ const activeContentPatternFlags = resolveActiveFlags (
69+ contentPatternFlags ,
70+ DEFAULT_CONTENT_PATTERN_FLAGS
71+ ) ;
72+ const regexp = new RegExp ( contentPattern , activeContentPatternFlags ) ;
73+
74+ const matchesForFile = [ ] ;
75+
76+ let match ;
77+ if ( activeContentPatternFlags . includes ( 'g' ) ) {
78+ // const matchesForFile = [...(content as any).matchAll(regexp)]; // TODO node v12+
79+ while ( ( match = regexp . exec ( content ) ) !== null ) {
80+ matchesForFile . push ( match ) ;
81+ }
82+ } else {
83+ match = regexp . exec ( content ) ;
84+ if ( match ) {
85+ matchesForFile . push ( match ) ;
86+ }
87+ }
88+
89+ if ( matchesForFile ?. length ) {
90+ matches . push ( {
91+ file,
92+ matches : matchesForFile . map (
93+ ( m ) =>
94+ ( {
95+ match : m [ 0 ] ,
96+ groups : m . groups ,
97+ } as ProjectCheckMatchDetails )
98+ ) ,
99+ } ) ;
100+ }
101+ } catch ( err : any ) {
102+ const error = new Error (
103+ `[content] "${ name } "\n File: ${ file } \n Error: ${ err . message } `
104+ ) ;
105+ errors . push ( error ) ;
106+ ctx . handledCheckFailures . push ( error ) ;
82107 }
83- }
84108
85- if ( matchesForFile ?. length ) {
86- matches . push ( {
87- file,
88- matches : matchesForFile . map (
89- ( m ) =>
90- ( {
91- match : m [ 0 ] ,
92- groups : m . groups ,
93- } as ProjectCheckMatchDetails )
94- ) ,
95- } ) ;
96- }
97- finishedCounter ++ ;
98- if ( finishedCounter >= files . length ) {
99- ctx . results . checks ! [ name ] = {
100- name,
101- type,
102- value : matches . length > 0 ,
103- matches,
104- } ;
105- task . title = resolveCheckTaskFulfilledTitle ( task , matches ) ;
106- resolveCheckParentTaskProgress ( parentTask ) ;
107- resolve ( ) ;
109+ finishedCounter ++ ;
110+ if ( finishedCounter >= files . length ) {
111+ ctx . results . checks ! [ name ] = {
112+ name,
113+ type,
114+ value : matches . length > 0 ,
115+ matches,
116+ } ;
117+ task . title = errors . length
118+ ? errors . map ( ( e ) => e . message ) . join ( ',' )
119+ : resolveCheckTaskFulfilledTitle ( task , matches ) ;
120+ resolveCheckParentTaskProgress ( parentTask ) ;
121+ resolve ( ) ;
122+ }
108123 }
109- }
110- } ) ;
124+ } ) ;
125+ } catch ( err : any ) {
126+ const error = new Error ( `[content] "${ name } "\n Error: ${ err . message } ` ) ;
127+ ctx . handledCheckFailures . push ( error ) ;
128+ task . title = `${ CheckResultSymbol . ERROR } ${ task . title } - ${ err . message } ` ;
129+ resolveCheckParentTaskProgress ( parentTask ) ;
130+ }
111131 }
112132 return contentCheckTask ;
113133}
0 commit comments