-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalgolia-index.ts
More file actions
113 lines (93 loc) · 3.31 KB
/
Copy pathalgolia-index.ts
File metadata and controls
113 lines (93 loc) · 3.31 KB
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
import algoliasearch, { SearchClient, SearchIndex } from 'algoliasearch';
import fs from 'fs';
import path from 'path';
import { ApiTool } from 'utils/types';
import { Tool } from '@components/tools';
const algoliaClient: SearchClient = algoliasearch(
process.env.ALGOLIA_APP_ID as string,
process.env.ALGOLIA_ADMIN_KEY as string,
);
const index: SearchIndex = algoliaClient.initIndex(
process.env.ALGOLIA_INDEX_NAME as string,
);
interface ToolsData {
tools: Record<string, Tool>;
}
function loadToolsFromFile(): Tool[] {
const toolsPath = path.join(__dirname, 'data', 'tools.json');
if (!fs.existsSync(toolsPath)) {
throw new Error(
`Tools data file not found at ${toolsPath}. Run 'npm run prebuild' first.`,
);
}
const fileContent = fs.readFileSync(toolsPath, 'utf-8');
const toolsData: ToolsData = JSON.parse(fileContent);
if (!toolsData.tools || Object.keys(toolsData.tools).length === 0) {
throw new Error('No tools data found or empty object returned');
}
// Convert the tools object to an array with IDs
const toolsArray = Object.entries(toolsData.tools).map(([id, tool]) => ({
...tool,
id,
}));
return toolsArray;
}
async function updateIndex(data: ApiTool[]): Promise<void> {
console.log(`Indexing ${data.length} ApiTool entries...`);
try {
const response = await index.clearObjects();
console.log('Algolia index cleared', response);
const algoliaResponse = await index.saveObjects(data);
console.log('Algolia index updated', algoliaResponse);
} catch (error) {
console.error(
'An error occurred while updating the Algolia index:',
error,
);
throw error;
}
}
function prepareDataForIndexing(toolsData: Tool[]): ApiTool[] {
console.log(`Preparing ${toolsData.length} tools for indexing`);
// Convert the tools data into an array of Algolia records
const algoliaRecords = toolsData.map((tool) => ({
objectID: tool.id, // Use the tool's ID as the objectID for Algolia
name: tool.name,
fields: {
slug: `/tool/${tool.id}`,
},
categories: tool.categories,
languages: tool.languages,
licenses: tool.licenses,
types: tool.types,
homepage: tool.homepage,
source: tool.source,
pricing: tool.pricing,
plans: tool.plans,
description: tool.description,
discussion: tool.discussion,
deprecated: tool.deprecated,
resources: tool.resources,
wrapper: tool.wrapper,
votes: tool.votes ?? 0,
upVotes: tool.upVotes,
downVotes: tool.downVotes,
upvotePercentage: tool.upvotePercentage,
other: tool.other,
}));
return algoliaRecords;
}
async function main(): Promise<void> {
try {
console.log('Loading tools from static data file...');
const toolsData = loadToolsFromFile();
console.log(`Loaded ${toolsData.length} tools from data/tools.json`);
const dataToIndex = prepareDataForIndexing(toolsData);
await updateIndex(dataToIndex);
console.log('Algolia indexing completed successfully!');
} catch (error) {
console.error('Failed to fetch or index data:', error);
process.exit(1);
}
}
main();