|
| 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 | +export function resolveDaemonCodeSignature( |
| 7 | + entryPath: string | undefined = process.argv[1], |
| 8 | + root: string = findProjectRoot(), |
| 9 | +): string { |
| 10 | + if (!entryPath) return 'unknown'; |
| 11 | + return computeDaemonCodeSignature(entryPath, root); |
| 12 | +} |
| 13 | + |
| 14 | +export function computeDaemonCodeSignature( |
| 15 | + entryPath: string, |
| 16 | + root: string = findProjectRoot(), |
| 17 | +): string { |
| 18 | + const targetPath = resolveDaemonCodeSignatureTarget(entryPath, root); |
| 19 | + try { |
| 20 | + const stat = fs.statSync(targetPath); |
| 21 | + if (!stat.isDirectory()) { |
| 22 | + return formatSingleFileSignature(targetPath, stat, root); |
| 23 | + } |
| 24 | + |
| 25 | + const hash = crypto.createHash('sha256'); |
| 26 | + let fileCount = 0; |
| 27 | + for (const filePath of walkSignatureFiles(targetPath)) { |
| 28 | + const fileStat = fs.statSync(filePath); |
| 29 | + const relativePath = path.relative(root, filePath) || path.basename(filePath); |
| 30 | + hash.update( |
| 31 | + `${relativePath}:${fileStat.size}:${Math.trunc(fileStat.mtimeMs)}:${fileStat.mode}\n`, |
| 32 | + ); |
| 33 | + fileCount += 1; |
| 34 | + } |
| 35 | + |
| 36 | + const relativeTarget = path.relative(root, targetPath) || path.basename(targetPath); |
| 37 | + return `${relativeTarget}:${fileCount}:${hash.digest('hex').slice(0, 16)}`; |
| 38 | + } catch { |
| 39 | + return 'unknown'; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function resolveDaemonCodeSignatureTarget(entryPath: string, root: string): string { |
| 44 | + const resolvedEntryPath = path.resolve(entryPath); |
| 45 | + const sourceDaemonEntry = path.join(root, 'src', 'daemon.ts'); |
| 46 | + const distDaemonEntry = path.join(root, 'dist', 'src', 'daemon.js'); |
| 47 | + if (resolvedEntryPath === sourceDaemonEntry) { |
| 48 | + return path.join(root, 'src'); |
| 49 | + } |
| 50 | + if (resolvedEntryPath === distDaemonEntry) { |
| 51 | + return path.join(root, 'dist', 'src'); |
| 52 | + } |
| 53 | + return resolvedEntryPath; |
| 54 | +} |
| 55 | + |
| 56 | +function formatSingleFileSignature(filePath: string, stat: fs.Stats, root: string): string { |
| 57 | + const relativePath = path.relative(root, filePath) || path.basename(filePath); |
| 58 | + return `${relativePath}:${stat.size}:${Math.trunc(stat.mtimeMs)}`; |
| 59 | +} |
| 60 | + |
| 61 | +function* walkSignatureFiles(dirPath: string): Generator<string> { |
| 62 | + const entries = fs |
| 63 | + .readdirSync(dirPath, { withFileTypes: true }) |
| 64 | + .sort((left, right) => left.name.localeCompare(right.name)); |
| 65 | + for (const entry of entries) { |
| 66 | + const entryPath = path.join(dirPath, entry.name); |
| 67 | + if (entry.isDirectory()) { |
| 68 | + yield* walkSignatureFiles(entryPath); |
| 69 | + continue; |
| 70 | + } |
| 71 | + if (entry.isFile()) { |
| 72 | + yield entryPath; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments