|
| 1 | +import crypto from 'node:crypto'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { findProjectRoot } from '../utils/version.ts'; |
| 5 | + |
| 6 | +const STATIC_IMPORT_RE = |
| 7 | + /(?:^|[^\w$.])(?:import|export)\s+(?:type\s+)?(?:[^'"`]*?\s+from\s+)?['"]([^'"]+)['"]/gm; |
| 8 | +const DYNAMIC_IMPORT_RE = /import\(\s*['"]([^'"]+)['"]\s*\)/gm; |
| 9 | +const RESOLVABLE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.cjs'] as const; |
| 10 | + |
| 11 | +export function resolveDaemonCodeSignature(): string { |
| 12 | + const entryPath = process.argv[1]; |
| 13 | + if (!entryPath) return 'unknown'; |
| 14 | + return computeDaemonCodeSignature(entryPath); |
| 15 | +} |
| 16 | + |
| 17 | +export function computeDaemonCodeSignature( |
| 18 | + entryPath: string, |
| 19 | + root: string = findProjectRoot(), |
| 20 | +): string { |
| 21 | + try { |
| 22 | + const normalizedRoot = path.resolve(root); |
| 23 | + const normalizedEntryPath = path.resolve(entryPath); |
| 24 | + const queue = [normalizedEntryPath]; |
| 25 | + const visited = new Set<string>(); |
| 26 | + const fingerprintParts: string[] = []; |
| 27 | + |
| 28 | + while (queue.length > 0) { |
| 29 | + const currentPath = queue.pop(); |
| 30 | + if (!currentPath || visited.has(currentPath)) continue; |
| 31 | + visited.add(currentPath); |
| 32 | + |
| 33 | + const stat = fs.statSync(currentPath); |
| 34 | + if (!stat.isFile()) continue; |
| 35 | + |
| 36 | + const relativePath = path.relative(normalizedRoot, currentPath) || currentPath; |
| 37 | + fingerprintParts.push(`${relativePath}:${stat.size}:${Math.trunc(stat.mtimeMs)}`); |
| 38 | + |
| 39 | + const content = fs.readFileSync(currentPath, 'utf8'); |
| 40 | + for (const specifier of collectRelativeImportSpecifiers(content)) { |
| 41 | + const dependencyPath = resolveRelativeImportPath(currentPath, specifier); |
| 42 | + if (dependencyPath) { |
| 43 | + queue.push(dependencyPath); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const fingerprint = fingerprintParts.sort().join('|'); |
| 49 | + const hash = crypto.createHash('sha1').update(fingerprint).digest('hex'); |
| 50 | + return `graph:${fingerprintParts.length}:${hash}`; |
| 51 | + } catch { |
| 52 | + return 'unknown'; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function collectRelativeImportSpecifiers(content: string): string[] { |
| 57 | + const specifiers = new Set<string>(); |
| 58 | + collectImportMatches(content, STATIC_IMPORT_RE, specifiers); |
| 59 | + collectImportMatches(content, DYNAMIC_IMPORT_RE, specifiers); |
| 60 | + return [...specifiers]; |
| 61 | +} |
| 62 | + |
| 63 | +function collectImportMatches(content: string, pattern: RegExp, specifiers: Set<string>): void { |
| 64 | + pattern.lastIndex = 0; |
| 65 | + let match: RegExpExecArray | null = null; |
| 66 | + while ((match = pattern.exec(content)) !== null) { |
| 67 | + const specifier = match[1]?.trim(); |
| 68 | + if (specifier?.startsWith('.')) { |
| 69 | + specifiers.add(specifier); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function resolveRelativeImportPath(fromPath: string, specifier: string): string | null { |
| 75 | + const basePath = path.resolve(path.dirname(fromPath), specifier); |
| 76 | + const direct = resolveExistingFile(basePath); |
| 77 | + if (direct) return direct; |
| 78 | + |
| 79 | + for (const extension of RESOLVABLE_EXTENSIONS) { |
| 80 | + const withExtension = resolveExistingFile(`${basePath}${extension}`); |
| 81 | + if (withExtension) return withExtension; |
| 82 | + } |
| 83 | + |
| 84 | + for (const extension of RESOLVABLE_EXTENSIONS) { |
| 85 | + const indexPath = resolveExistingFile(path.join(basePath, `index${extension}`)); |
| 86 | + if (indexPath) return indexPath; |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | +} |
| 91 | + |
| 92 | +function resolveExistingFile(candidatePath: string): string | null { |
| 93 | + try { |
| 94 | + return fs.statSync(candidatePath).isFile() ? candidatePath : null; |
| 95 | + } catch { |
| 96 | + return null; |
| 97 | + } |
| 98 | +} |
0 commit comments