Skip to content

Commit 41747e3

Browse files
committed
refactor: enhance getPath function in canvasFactory to support multiple asset file locations and ensure path existence check
1 parent 378604a commit 41747e3

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

packages/thumbnail/src/canvasFactory.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,33 @@ if (typeof document === 'undefined') {
1919
GlobalFonts,
2020
} = canvasModule;
2121

22-
const getPath = (filename: string) => {
22+
const getPath = (filename: string): string => {
2323
const workingDir = process.cwd();
2424

25-
const fullPath = path.join(
26-
workingDir,
27-
filename.split('/').join(path.sep),
28-
);
25+
// Try different possible locations for the asset files
26+
const possiblePaths = [
27+
// When running from package directory
28+
path.join(workingDir, filename.split('/').join(path.sep)),
29+
// When running from root directory
30+
path.join(
31+
workingDir,
32+
'packages',
33+
'thumbnail',
34+
filename.split('/').join(path.sep),
35+
),
36+
];
37+
38+
// Check which path exists
39+
const fs = require('fs');
40+
41+
for (const fullPath of possiblePaths) {
42+
if (fs.existsSync(fullPath)) {
43+
return fullPath;
44+
}
45+
}
2946

30-
return fullPath;
47+
// Default to first path if none exist
48+
return possiblePaths[0]!;
3149
};
3250

3351
const saveToImage = (canvas: NapiRs.Canvas) => canvas.encode('png');

0 commit comments

Comments
 (0)