|
| 1 | +import { readFileSync, realpathSync, readlinkSync, lstatSync } from 'fs'; |
| 2 | +import { createRequire } from 'module'; |
| 3 | +import { dirname, resolve } from 'path'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Get library version and source info |
| 7 | + * @returns {Object} { version: string, source: 'local'|'npm', sourcePath: string } |
| 8 | + */ |
| 9 | +export function getLibraryInfo() { |
| 10 | + try { |
| 11 | + const require = createRequire(import.meta.url); |
| 12 | + let packagePath = require.resolve('resilient-llm/package.json'); |
| 13 | + |
| 14 | + // Simple check: use env var if set, otherwise check if path contains node_modules |
| 15 | + const isLocal = process.env.RESILIENT_LLM_SOURCE === 'local' || |
| 16 | + !packagePath.includes('node_modules'); |
| 17 | + |
| 18 | + // For local installs, ALWAYS resolve the symlink to get the actual source package.json |
| 19 | + // This ensures we read the latest version from the source, not a cached copy |
| 20 | + if (isLocal) { |
| 21 | + try { |
| 22 | + packagePath = realpathSync(packagePath); |
| 23 | + } catch (error) { |
| 24 | + // If realpath fails, try to resolve the symlink manually |
| 25 | + const packageDir = dirname(packagePath); |
| 26 | + try { |
| 27 | + const stats = lstatSync(packageDir); |
| 28 | + if (stats.isSymbolicLink()) { |
| 29 | + const symlinkTarget = readlinkSync(packageDir); |
| 30 | + const resolvedTarget = symlinkTarget.startsWith('/') |
| 31 | + ? symlinkTarget |
| 32 | + : resolve(packageDir, symlinkTarget); |
| 33 | + packagePath = resolve(resolvedTarget, 'package.json'); |
| 34 | + } |
| 35 | + } catch { |
| 36 | + // Keep original path |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8')); |
| 42 | + const version = packageJson.version; |
| 43 | + |
| 44 | + // Get source path for link |
| 45 | + let sourcePath = 'https://www.npmjs.com/package/resilient-llm'; |
| 46 | + if (isLocal) { |
| 47 | + // For local, resolve the actual path (follow symlinks) to get the real source folder |
| 48 | + try { |
| 49 | + const packageDir = dirname(packagePath); |
| 50 | + |
| 51 | + // Try to resolve the symlink chain |
| 52 | + // realpathSync follows all symlinks to the final destination |
| 53 | + const realPath = realpathSync(packagePath); |
| 54 | + const libDir = dirname(realPath); |
| 55 | + |
| 56 | + // If the resolved path is still in node_modules, it means the symlink |
| 57 | + // points to another location. Let's check the symlink directly. |
| 58 | + if (libDir.includes('node_modules')) { |
| 59 | + // Check if packageDir itself is a symlink |
| 60 | + try { |
| 61 | + const stats = lstatSync(packageDir); |
| 62 | + if (stats.isSymbolicLink()) { |
| 63 | + const symlinkTarget = readlinkSync(packageDir); |
| 64 | + // Resolve the symlink target |
| 65 | + const resolvedTarget = symlinkTarget.startsWith('/') |
| 66 | + ? symlinkTarget |
| 67 | + : resolve(packageDir, symlinkTarget); |
| 68 | + sourcePath = `file://${resolvedTarget}`; |
| 69 | + } else { |
| 70 | + // Not a direct symlink, but realpath resolved to node_modules |
| 71 | + // This shouldn't happen for local installs, but use the resolved path |
| 72 | + sourcePath = `file://${libDir}`; |
| 73 | + } |
| 74 | + } catch { |
| 75 | + sourcePath = `file://${libDir}`; |
| 76 | + } |
| 77 | + } else { |
| 78 | + // Resolved to a path outside node_modules - this is the source! |
| 79 | + sourcePath = `file://${libDir}`; |
| 80 | + } |
| 81 | + } catch (error) { |
| 82 | + // Fallback |
| 83 | + const libDir = dirname(packagePath); |
| 84 | + sourcePath = `file://${libDir}`; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + version, |
| 90 | + source: isLocal ? 'local' : 'npm', |
| 91 | + sourcePath |
| 92 | + }; |
| 93 | + } catch (error) { |
| 94 | + return { |
| 95 | + version: 'unknown', |
| 96 | + source: 'unknown', |
| 97 | + sourcePath: '#' |
| 98 | + }; |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments