-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sync-index.mjs
47 lines (37 loc) · 1.26 KB
/
sync-index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { sync } from 'fumadocs-core/search/orama-cloud';
import * as fs from 'node:fs/promises';
import { CloudManager } from '@oramacloud/client';
import * as path from 'path';
import * as dotenv from 'dotenv';
// 加载 .env 文件
dotenv.config();
export async function updateSearchIndexes() {
const apiKey = process.env.ORAMA_PRIVATE_API_KEY;
const indexId = 'k2hnq39jnt7u8l41bfv0ezhd'; // 你的索引 ID
if (!apiKey) {
console.log('未找到 Orama 私钥, 跳过索引更新');
return;
}
try {
// 使用绝对路径
const staticJsonPath = path.join(process.cwd(), '.next/server/app/static.json.body');
try {
await fs.access(staticJsonPath);
} catch {
console.error('static.json.body 文件不存在,请先运行 next build');
process.exit(1);
}
const content = await fs.readFile(staticJsonPath);
const records = JSON.parse(content.toString());
const manager = new CloudManager({ api_key: apiKey });
await sync(manager, {
index: indexId,
documents: records,
});
console.log(`搜索索引更新完成: ${records.length} 条记录`);
} catch (error) {
console.error('更新搜索索引时发生错误:', error);
process.exit(1);
}
}
void updateSearchIndexes();