This commit is contained in:
2025-06-11 23:47:31 +09:00
parent a8fe191ab0
commit a2a58510e3

View File

@ -72,46 +72,53 @@ export async function initializeJMdict(): Promise<void> {
return; return;
} }
// Try to load existing database with retry logic // Check if we have the JSON file first
let retryCount = 0; const dataDir = path.dirname(JMDICT_DB_PATH);
const maxRetries = 2; const jsonPath = path.join(dataDir, "jmdict-eng-3.1.0.json");
let hasJsonFile = false;
try {
await fs.access(jsonPath);
hasJsonFile = true;
console.log("JMdict JSONファイルが見つかりました");
} catch {
console.log("JMdict JSONファイルが見つかりません");
}
// Try to load existing database (only if it might exist)
let databaseLoaded = false;
try {
// Check if database directory exists and has content
const dbStats = await fs.stat(JMDICT_DB_PATH);
if (dbStats.isDirectory()) {
const dbContents = await fs.readdir(JMDICT_DB_PATH);
if (dbContents.length > 0) {
console.log(
"既存のデータベースディレクトリが見つかりました。読み込みを試行します...",
);
while (retryCount <= maxRetries) {
try { try {
jmdictDb = await setup(JMDICT_DB_PATH); jmdictDb = await setup(JMDICT_DB_PATH);
console.log( console.log(
`JMdict データベース読み込み完了 (辞書日付: ${jmdictDb.dictDate})`, `JMdict データベース読み込み完了 (辞書日付: ${jmdictDb.dictDate})`,
); );
return; databaseLoaded = true;
} catch (error) { } catch (loadError) {
console.log( console.log("既存データベースの読み込みに失敗:", loadError.message);
`データベース読み込み試行 ${retryCount + 1}/${maxRetries + 1} 失敗:`, // Clean up corrupted database
error,
);
if (retryCount < maxRetries) {
// Clean up potentially corrupted database and retry
await cleanupDatabase(JMDICT_DB_PATH); await cleanupDatabase(JMDICT_DB_PATH);
retryCount++;
await new Promise((resolve) => setTimeout(resolve, 1000)); // Wait 1 second
} else {
throw error;
} }
} }
} }
} catch {
// Database directory doesn't exist, which is fine
console.log("既存のデータベースが見つかりません");
}
// If we get here, all retries failed // If database loading failed or no database exists, try to create from JSON
console.log( if (!databaseLoaded && hasJsonFile) {
"既存のデータベース読み込みに失敗しました。新規作成を試行します...",
);
// Check if we have the JSON file locally
const dataDir = path.dirname(JMDICT_DB_PATH);
const jsonPath = path.join(dataDir, "jmdict-eng-3.1.0.json");
try { try {
await fs.access(jsonPath); console.log("JMdict JSONファイルからデータベースを作成中...");
console.log("JMdict JSONファイルを使用してデータベースを作成中...");
// Ensure clean directory for new database // Ensure clean directory for new database
await cleanupDatabase(JMDICT_DB_PATH); await cleanupDatabase(JMDICT_DB_PATH);
@ -120,13 +127,23 @@ export async function initializeJMdict(): Promise<void> {
console.log( console.log(
`JMdict データベース作成完了 (辞書日付: ${jmdictDb.dictDate})`, `JMdict データベース作成完了 (辞書日付: ${jmdictDb.dictDate})`,
); );
} catch (jsonError) { databaseLoaded = true;
} catch (createError) {
console.error("JSONからのデータベース作成に失敗:", createError.message);
}
}
// If still no database, provide instructions and use minimal database
if (!databaseLoaded) {
if (!hasJsonFile) {
console.log("JMdict JSONファイルが見つかりません。"); console.log("JMdict JSONファイルが見つかりません。");
console.log(`手動でダウンロードしてください: ${JMDICT_DATA_URL}`); console.log(`手動でダウンロードしてください: ${JMDICT_DATA_URL}`);
console.log( console.log(
`ダウンロード後、解凍して以下のパスに配置してください: ${jsonPath}`, `ダウンロード後、解凍して以下のパスに配置してください: ${jsonPath}`,
); );
}
console.log("最小限のデータベースを使用します。");
await createMinimalJMdictDatabase(); await createMinimalJMdictDatabase();
} }
} catch (error) { } catch (error) {