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