fix(core): recopy Yomitan extension when patched scripts drift

This commit is contained in:
2026-03-04 22:50:09 -08:00
parent fdbf769760
commit beeeee5ebd
3 changed files with 107 additions and 21 deletions

View File

@@ -0,0 +1,53 @@
import * as fs from 'fs';
import * as path from 'path';
const YOMITAN_SYNC_SCRIPT_PATHS = [
path.join('js', 'app', 'popup.js'),
path.join('js', 'display', 'popup-main.js'),
path.join('js', 'display', 'display.js'),
path.join('js', 'display', 'display-audio.js'),
];
function readManifestVersion(manifestPath: string): string | null {
try {
const parsed = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')) as { version?: unknown };
return typeof parsed.version === 'string' ? parsed.version : null;
} catch {
return null;
}
}
function areFilesEqual(sourcePath: string, targetPath: string): boolean {
if (!fs.existsSync(sourcePath) || !fs.existsSync(targetPath)) return false;
try {
return fs.readFileSync(sourcePath).equals(fs.readFileSync(targetPath));
} catch {
return false;
}
}
export function shouldCopyYomitanExtension(sourceDir: string, targetDir: string): boolean {
if (!fs.existsSync(targetDir)) {
return true;
}
const sourceManifest = path.join(sourceDir, 'manifest.json');
const targetManifest = path.join(targetDir, 'manifest.json');
if (!fs.existsSync(sourceManifest) || !fs.existsSync(targetManifest)) {
return true;
}
const sourceVersion = readManifestVersion(sourceManifest);
const targetVersion = readManifestVersion(targetManifest);
if (sourceVersion === null || targetVersion === null || sourceVersion !== targetVersion) {
return true;
}
for (const relativePath of YOMITAN_SYNC_SCRIPT_PATHS) {
if (!areFilesEqual(path.join(sourceDir, relativePath), path.join(targetDir, relativePath))) {
return true;
}
}
return false;
}