Skip to content

Commit ab22859

Browse files
author
Tomas Trajan Herich (u803909)
committed
feat: fs access robustness
1 parent 07bda7c commit ab22859

7 files changed

Lines changed: 454 additions & 371 deletions

File tree

lib/checks/content.check.ts

Lines changed: 86 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

lib/checks/file.check.ts

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,40 @@ export function fileCheckTaskFactory(
1818
task: ListrTaskWrapper<Context, ListrDefaultRenderer>
1919
) {
2020
const { name, type } = definition;
21-
const files = getCheckFiles(
22-
definition,
23-
DEFAULT_EXCLUDE_FILES_PATTERN_CONTENT
24-
);
21+
try {
22+
const files = getCheckFiles(
23+
definition,
24+
DEFAULT_EXCLUDE_FILES_PATTERN_CONTENT
25+
);
2526

26-
task.title = `${task.title}, found ${files.length} files`;
27+
task.title = `${task.title}, found ${files.length} files`;
2728

28-
if (!files.length) {
29-
ctx.results.checks![name] = {
30-
name,
31-
type,
32-
value: false,
33-
};
34-
task.title = `${CheckResultSymbol.UNFULFILLED} ${task.title}`;
35-
resolveCheckParentTaskProgress(parentTask);
36-
return;
37-
} else {
38-
ctx.results.checks![name] = {
39-
name,
40-
type,
41-
value: true,
42-
matches: files.map((file) => ({
43-
file,
44-
matches: [],
45-
})),
46-
};
47-
task.title = `${CheckResultSymbol.FULFILLED} ${task.title}`;
29+
if (!files.length) {
30+
ctx.results.checks![name] = {
31+
name,
32+
type,
33+
value: false,
34+
};
35+
task.title = `${CheckResultSymbol.UNFULFILLED} ${task.title}`;
36+
resolveCheckParentTaskProgress(parentTask);
37+
return;
38+
} else {
39+
ctx.results.checks![name] = {
40+
name,
41+
type,
42+
value: true,
43+
matches: files.map((file) => ({
44+
file,
45+
matches: [],
46+
})),
47+
};
48+
task.title = `${CheckResultSymbol.FULFILLED} ${task.title}`;
49+
resolveCheckParentTaskProgress(parentTask);
50+
}
51+
} catch (err: any) {
52+
const error = new Error(`[file] "${name}"\n Error: ${err.message}`);
53+
ctx.handledCheckFailures.push(error);
54+
task.title = `${CheckResultSymbol.ERROR} ${task.title} - ${err.message}`;
4855
resolveCheckParentTaskProgress(parentTask);
4956
}
5057
}

0 commit comments

Comments
 (0)