Skip to content

Commit 54ee513

Browse files
committed
fix(#141): handle configBaseDir undefined case
1 parent d2bb9d0 commit 54ee513

1 file changed

Lines changed: 12 additions & 8 deletions

File tree

src/parser/alerts.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import { existsSync, readFileSync } from 'fs';
1515
import { homedir } from 'os';
1616
import path from 'path';
1717

18+
const warnAndFallback = (message: string): string => {
19+
return `<script>console.warn("${message}");</script>${fallbackIcon}`;
20+
};
21+
1822
// Resolve option from alertsOptions.icons into raw svg tag
1923
const resolveIcon = (iconOpt: string): string => {
2024
// Case 1: already a raw svg tag
@@ -30,24 +34,24 @@ const resolveIcon = (iconOpt: string): string => {
3034
if (prefix === '~/') {
3135
iconPath = path.join(homedir(), iconPath.slice(2));
3236
} else if (prefix === './' || prefix === '../') {
33-
iconPath = path.join(configBaseDir!, iconPath);
37+
if (!configBaseDir) {
38+
return warnAndFallback(`configBaseDir not set for relative icon path: ${iconPath}`);
39+
}
40+
iconPath = path.join(configBaseDir, iconPath);
3441
}
3542

36-
if (existsSync(iconPath)) {
37-
return readFileSync(iconPath).toString();
38-
} else {
39-
const warn = `<script>console.warn("Icon file not found: ${iconPath}");</script>`;
40-
return `${warn}${fallbackIcon}`;
43+
if (!existsSync(iconPath)) {
44+
return warnAndFallback(`Icon file not found: ${iconPath}`);
4145
}
46+
return readFileSync(iconPath).toString();
4247
}
4348

4449
// Case 3: octicon name (in kebab-case) <https://primer.style/octicons>
4550
const octiconName = iconOpt as keyof typeof octicons;
4651
const octicon = octicons[octiconName]?.toSVG();
4752

4853
if (!octicon) {
49-
const warn = `<script>console.warn("Not a known octicon name: ${iconOpt}");</script>`;
50-
return `${warn}${fallbackIcon}`;
54+
return warnAndFallback(`Not a known octicon name: ${iconOpt}`);
5155
}
5256
return octicon;
5357
};

0 commit comments

Comments
 (0)