|
| 1 | +/** |
| 2 | + * Cursor asset path resolution for Node.js environment |
| 3 | + * Used by the processing pipeline (Sharp-based cursor rendering) |
| 4 | + */ |
| 5 | + |
| 6 | +import { existsSync } from 'fs'; |
| 7 | +import { join } from 'path'; |
| 8 | +import { CURSOR_SHAPE_MAP } from './constants'; |
| 9 | + |
| 10 | +// Create a simple logger function to avoid circular dependencies |
| 11 | +function logDebug(message: string, ...args: unknown[]): void { |
| 12 | + if (process.env.DEBUG_CURSOR) { |
| 13 | + console.log(`[CursorAssets] ${message}`, ...args); |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function logWarn(message: string, ...args: unknown[]): void { |
| 18 | + console.warn(`[CursorAssets] ${message}`, ...args); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Get the assets directory path based on environment |
| 23 | + * Internal function - not exported from module |
| 24 | + */ |
| 25 | +function getAssetsDir(): string { |
| 26 | + // Try to get app from electron, but handle case where it's not available |
| 27 | + let app: { isPackaged: boolean; getPath: (name: string) => string } | undefined; |
| 28 | + try { |
| 29 | + app = require('electron').app; |
| 30 | + } catch { |
| 31 | + // Electron not available, assume development |
| 32 | + } |
| 33 | + |
| 34 | + const isDev = process.env.NODE_ENV === 'development' || !app?.isPackaged; |
| 35 | + logDebug('Getting assets directory, isDev:', isDev, '__dirname:', __dirname, 'cwd:', process.cwd()); |
| 36 | + |
| 37 | + if (isDev) { |
| 38 | + // In development, try multiple possible paths |
| 39 | + // __dirname in compiled code will be dist/main/cursor or dist/cursor |
| 40 | + const possiblePaths = [ |
| 41 | + join(__dirname, '../../src/assets'), // From dist/main/cursor |
| 42 | + join(__dirname, '../../../src/assets'), // From dist/cursor |
| 43 | + join(process.cwd(), 'src/assets'), // From project root |
| 44 | + ]; |
| 45 | + |
| 46 | + logDebug('Trying possible asset paths:', possiblePaths); |
| 47 | + |
| 48 | + for (const path of possiblePaths) { |
| 49 | + logDebug('Checking path:', path, 'exists:', existsSync(path)); |
| 50 | + if (existsSync(path)) { |
| 51 | + logDebug('Found assets directory at:', path); |
| 52 | + return path; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Fallback to project root |
| 57 | + const fallbackPath = join(process.cwd(), 'src/assets'); |
| 58 | + logDebug('Using fallback path:', fallbackPath); |
| 59 | + return fallbackPath; |
| 60 | + } |
| 61 | + |
| 62 | + // In production, use app resources |
| 63 | + const possibleProdPaths: string[] = []; |
| 64 | + |
| 65 | + if (app) { |
| 66 | + try { |
| 67 | + const resourcesPath = app.getPath('resources'); |
| 68 | + possibleProdPaths.push(join(resourcesPath, 'assets')); |
| 69 | + logDebug('Added path from app.getPath("resources"):', join(resourcesPath, 'assets')); |
| 70 | + } catch (error) { |
| 71 | + logDebug('Could not get resources path:', error); |
| 72 | + } |
| 73 | + |
| 74 | + try { |
| 75 | + const exePath = app.getPath('exe'); |
| 76 | + possibleProdPaths.push(join(exePath, '../../Resources/assets')); |
| 77 | + logDebug('Added path from exe calculation:', join(exePath, '../../Resources/assets')); |
| 78 | + } catch (error) { |
| 79 | + logDebug('Could not get exe path:', error); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // Calculate from __dirname |
| 84 | + const fromDistPath = join(__dirname, '../../../../assets'); |
| 85 | + possibleProdPaths.push(fromDistPath); |
| 86 | + logDebug('Added path from __dirname (4 levels up):', fromDistPath); |
| 87 | + |
| 88 | + const fromDistPathAlt = join(__dirname, '../../../../../assets'); |
| 89 | + possibleProdPaths.push(fromDistPathAlt); |
| 90 | + logDebug('Added alternative __dirname path (5 levels up):', fromDistPathAlt); |
| 91 | + |
| 92 | + // Check each path and return the first one that exists |
| 93 | + for (const path of possibleProdPaths) { |
| 94 | + logDebug('Checking production path:', path, 'exists:', existsSync(path)); |
| 95 | + if (existsSync(path)) { |
| 96 | + logDebug('Found production assets directory at:', path); |
| 97 | + return path; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // If none found, return the first calculated path (will be checked by caller) |
| 102 | + const fallbackPath = possibleProdPaths[0] || join(process.cwd(), 'resources/assets'); |
| 103 | + logWarn('No production assets directory found, using fallback:', fallbackPath); |
| 104 | + return fallbackPath; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Get cursor asset file path for a given shape |
| 109 | + * Internal function - used by getCursorAssetFilePath |
| 110 | + * @returns Full path to the asset file, or null if not found |
| 111 | + */ |
| 112 | +function getCursorAssetPath(shape: string): string | null { |
| 113 | + const assetFileName = CURSOR_SHAPE_MAP[shape] || CURSOR_SHAPE_MAP.arrow; |
| 114 | + const assetsDir = getAssetsDir(); |
| 115 | + const assetPath = join(assetsDir, assetFileName); |
| 116 | + |
| 117 | + logDebug('Getting cursor asset for shape:', shape, 'file:', assetFileName, 'path:', assetPath); |
| 118 | + |
| 119 | + if (existsSync(assetPath)) { |
| 120 | + logDebug('Cursor asset found at:', assetPath); |
| 121 | + return assetPath; |
| 122 | + } |
| 123 | + |
| 124 | + logDebug('Cursor asset not found at:', assetPath); |
| 125 | + return null; |
| 126 | +} |
| 127 | + |
| 128 | +/** |
| 129 | + * Get cursor asset file path with fallback verification |
| 130 | + * Tries multiple possible locations |
| 131 | + */ |
| 132 | +export function getCursorAssetFilePath(shape: string): string | null { |
| 133 | + logDebug('getCursorAssetFilePath called for shape:', shape); |
| 134 | + const path = getCursorAssetPath(shape); |
| 135 | + |
| 136 | + if (path) { |
| 137 | + // Verify the file actually exists |
| 138 | + if (existsSync(path)) { |
| 139 | + logDebug('Cursor asset file verified at:', path); |
| 140 | + return path; |
| 141 | + } else { |
| 142 | + logWarn('Cursor asset path resolved but file does not exist:', path); |
| 143 | + // Try to find it in common locations |
| 144 | + const assetFileName = CURSOR_SHAPE_MAP[shape] || CURSOR_SHAPE_MAP.arrow; |
| 145 | + const fallbackPaths = [ |
| 146 | + join(process.cwd(), 'src/assets', assetFileName), |
| 147 | + join(__dirname, '../../src/assets', assetFileName), |
| 148 | + join(__dirname, '../../../src/assets', assetFileName), |
| 149 | + ]; |
| 150 | + |
| 151 | + logDebug('Trying fallback paths:', fallbackPaths); |
| 152 | + |
| 153 | + for (const fallbackPath of fallbackPaths) { |
| 154 | + logDebug('Checking fallback path:', fallbackPath, 'exists:', existsSync(fallbackPath)); |
| 155 | + if (existsSync(fallbackPath)) { |
| 156 | + logDebug('Found cursor asset at fallback path:', fallbackPath); |
| 157 | + return fallbackPath; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + logWarn('Could not find cursor asset in any fallback path'); |
| 162 | + } |
| 163 | + } else { |
| 164 | + logWarn('getCursorAssetPath returned null for shape:', shape); |
| 165 | + } |
| 166 | + |
| 167 | + return null; |
| 168 | +} |
0 commit comments