-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitTypesense.js
119 lines (101 loc) · 3.33 KB
/
initTypesense.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Typesense from 'typesense';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
dotenv.config();
const client = new Typesense.Client({
nodes: [{
host: process.env.TYPESENSE_HOST,
port: process.env.TYPESENSE_PORT,
protocol: process.env.TYPESENSE_PROTOCOL
}],
apiKey: process.env.TYPESENSE_API_KEY,
connectionTimeoutSeconds: 2
});
// Define collections
const collections = ['docs_admin', 'docs_guest', 'docs_editor', 'docs_companyMember'];
async function initializeTypesense() {
const schema = {
fields: [
{ name: 'title', type: 'string' },
{ name: 'slug', type: 'string' },
{ name: 'description', type: 'string' },
{ name: 'content', type: 'string' },
{ name: 'path', type: 'string' }
]
};
try {
// Remove existing collections
await removeExistingCollections();
// Create new collections
for (const collection of collections) {
const collectionSchema = { ...schema, name: collection };
await client.collections().create(collectionSchema);
console.log(`Collection ${collection} created successfully`);
}
// Index Markdown files
await indexMarkdownFiles('src/content/docs');
console.log('Markdown files indexed successfully');
} catch (error) {
console.error('Error initializing Typesense:', error);
}
}
async function removeExistingCollections() {
try {
const existingCollections = await client.collections().retrieve();
for (const collection of existingCollections) {
await client.collections(collection.name).delete();
console.log(`Deleted existing collection: ${collection.name}`);
}
} catch (error) {
if (error.httpStatus !== 404) {
console.error('Error removing existing collections:', error);
}
}
}
async function indexMarkdownFiles(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
await indexMarkdownFiles(filePath);
} else if (path.extname(file) === '.md') {
const fileContent = fs.readFileSync(filePath, 'utf8');
const { data, content } = matter(fileContent);
const document = {
title: data.title || '',
slug: data.slug || '',
description: data.description || '',
content: content,
path: filePath.replace('src/content/docs/', '')
};
// Determine the collection based on the file path
const collectionName = getCollectionNameFromPath(filePath);
try {
await client.collections(collectionName).documents().create(document);
console.log(`Indexed in ${collectionName}: ${filePath}`);
} catch (error) {
console.error(`Error indexing ${filePath} in ${collectionName}:`, error);
}
}
}
}
function getCollectionNameFromPath(filePath) {
const relativePath = path.relative('src/content/docs', filePath);
const topLevelFolder = relativePath.split(path.sep)[0];
switch (topLevelFolder) {
case 'admin':
return 'docs_admin';
case 'guest':
return 'docs_guest';
case 'editor':
return 'docs_editor';
case 'companyMember':
return 'docs_companyMember';
default:
throw new Error(`Unknown folder: ${topLevelFolder}`);
}
}
initializeTypesense();