|
1 | 1 | import firebaseAdmin from "firebase-admin"; |
2 | 2 | import fs from "fs"; |
3 | 3 | import path from "path"; |
| 4 | +import { MeiliSearch } from "meilisearch"; |
| 5 | + |
4 | 6 | import { parseMarkdown } from "./parser.js"; |
5 | 7 |
|
| 8 | +const client = new MeiliSearch({ |
| 9 | + host: process.env.MEILISEARCH_HOST, |
| 10 | + apiKey: process.env.MEILISEARCH_API_KEY, |
| 11 | +}); |
| 12 | + |
6 | 13 | firebaseAdmin.initializeApp({ |
7 | 14 | credential: firebaseAdmin.credential.cert({ |
8 | 15 | type: "service_account", |
@@ -44,46 +51,66 @@ async function main() { |
44 | 51 | const blogFileNames = getBlogsFileNames(); |
45 | 52 |
|
46 | 53 | for (let filename of blogFileNames) { |
47 | | - const fileNameWithoutExtension = filename.split(".")[0].split("/").pop(); |
48 | | - const blogExists = ( |
49 | | - await firestore.collection("blogs").doc(fileNameWithoutExtension).get() |
50 | | - ).exists; |
| 54 | + try { |
| 55 | + const fileNameWithoutExtension = filename.split(".")[0].split("/").pop(); |
| 56 | + const blogExists = ( |
| 57 | + await firestore.collection("blogs").doc(fileNameWithoutExtension).get() |
| 58 | + ).exists; |
51 | 59 |
|
52 | | - const blog = getBlogFileContent(filename); |
| 60 | + const blog = getBlogFileContent(filename); |
53 | 61 |
|
54 | | - const { source, content, data } = await parseMarkdown({ markdown: blog }); |
| 62 | + const { source, content, data } = await parseMarkdown({ markdown: blog }); |
55 | 63 |
|
56 | | - if (!blogExists) { |
57 | | - await firestore |
58 | | - .collection("blogs") |
59 | | - .doc(fileNameWithoutExtension) |
60 | | - .set( |
61 | | - { |
62 | | - source, |
63 | | - content: content, |
64 | | - ...data, |
65 | | - dateCreated: data.dateCreated.toUTCString(), |
66 | | - dateUpdated: new Date().toUTCString(), |
67 | | - likes: 0, |
68 | | - link: fileNameWithoutExtension, |
69 | | - }, |
70 | | - { merge: true }, |
71 | | - ); |
72 | | - } else { |
73 | | - await firestore |
74 | | - .collection("blogs") |
75 | | - .doc(fileNameWithoutExtension) |
76 | | - .set( |
| 64 | + if (!blogExists) { |
| 65 | + await firestore |
| 66 | + .collection("blogs") |
| 67 | + .doc(fileNameWithoutExtension) |
| 68 | + .set( |
| 69 | + { |
| 70 | + source, |
| 71 | + content: content, |
| 72 | + ...data, |
| 73 | + dateCreated: data.dateCreated.toUTCString(), |
| 74 | + dateUpdated: new Date().toUTCString(), |
| 75 | + likes: 0, |
| 76 | + link: fileNameWithoutExtension, |
| 77 | + }, |
| 78 | + { merge: true }, |
| 79 | + ); |
| 80 | + } else { |
| 81 | + await firestore |
| 82 | + .collection("blogs") |
| 83 | + .doc(fileNameWithoutExtension) |
| 84 | + .set( |
| 85 | + { |
| 86 | + source, |
| 87 | + content: content, |
| 88 | + ...data, |
| 89 | + dateCreated: data.dateCreated.toUTCString(), |
| 90 | + dateUpdated: new Date().toUTCString(), |
| 91 | + link: fileNameWithoutExtension, |
| 92 | + }, |
| 93 | + { merge: true }, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + try { |
| 98 | + await client.index("blogs").addDocuments([ |
77 | 99 | { |
78 | | - source, |
79 | | - content: content, |
80 | | - ...data, |
81 | | - dateCreated: data.dateCreated.toUTCString(), |
82 | | - dateUpdated: new Date().toUTCString(), |
| 100 | + id: fileNameWithoutExtension, |
| 101 | + title: data.title, |
| 102 | + content, |
| 103 | + author: data.author, |
| 104 | + tags: data.tags, |
83 | 105 | link: fileNameWithoutExtension, |
| 106 | + dateCreated: new Date(data.dateCreated).toISOString(), |
| 107 | + description: data.description, |
84 | 108 | }, |
85 | | - { merge: true }, |
86 | | - ); |
| 109 | + ]); |
| 110 | + } catch (error) {} |
| 111 | + } catch (error) { |
| 112 | + console.log("Error with file: " + filename); |
| 113 | + console.error(error); |
87 | 114 | } |
88 | 115 | } |
89 | 116 | } |
|
0 commit comments