From c10068c8e829367aec7ecca2115fe7c2c0b8fb80 Mon Sep 17 00:00:00 2001 From: Milan Date: Thu, 10 Oct 2024 10:47:56 +0545 Subject: [PATCH 1/3] Implementing error handling will help in debugging and provide a better user experience. --- publish/index.js | 130 +++++++++++++++++++++++++---------------------- 1 file changed, 68 insertions(+), 62 deletions(-) diff --git a/publish/index.js b/publish/index.js index 4cfb3661..e166e9d9 100644 --- a/publish/index.js +++ b/publish/index.js @@ -33,72 +33,78 @@ function getBlogsFileNames() { } async function main() { - const blogFileNames = getBlogsFileNames(); + try{ - for (let filename of blogFileNames) { - const fileNameWithoutExtension = filename.split(".")[0]; - const blogExists = ( - await firestore.collection("blogs").doc(fileNameWithoutExtension).get() - ).exists; - - const blog = fs.readFileSync( - path.resolve(process.cwd(), "blogs", filename), - "utf-8" - ); - - const blogMatter = matter(blog); - - const source = await serialize(blogMatter.content, { - mdxOptions: { - remarkPlugins: [ - remarkPresetLintConsistent, - remarkPresetLintRecommended, - remarkBreaks, - remarkGfm, - ], - rehypePlugins: [ - rehypeSlug, - rehypeHighlight, - [ - rehypeExternalLinks, - { target: "_blank", rel: ["nofollow", "noreferrer", "noopener"] }, + const blogFileNames = getBlogsFileNames(); + + for (let filename of blogFileNames) { + const fileNameWithoutExtension = filename.split(".")[0]; + const blogExists = ( + await firestore.collection("blogs").doc(fileNameWithoutExtension).get() + ).exists; + + const blog = fs.readFileSync( + path.resolve(process.cwd(), "blogs", filename), + "utf-8" + ); + + const blogMatter = matter(blog); + + const source = await serialize(blogMatter.content, { + mdxOptions: { + remarkPlugins: [ + remarkPresetLintConsistent, + remarkPresetLintRecommended, + remarkBreaks, + remarkGfm, ], - ], - }, - }); - if (!blogExists) { - await firestore - .collection("blogs") - .doc(fileNameWithoutExtension) - .set( - { - source, - content: blogMatter.content, - ...blogMatter.data, - dateCreated: blogMatter.data.dateCreated.toUTCString(), - dateUpdated: new Date().toUTCString(), - likes: 0, - link: fileNameWithoutExtension, - }, - { merge: true } - ); - } else { - await firestore - .collection("blogs") - .doc(fileNameWithoutExtension) - .set( - { - source, - content: blogMatter.content, - ...blogMatter.data, - dateCreated: blogMatter.data.dateCreated.toUTCString(), - dateUpdated: new Date().toUTCString(), - link: fileNameWithoutExtension, - }, - { merge: true } - ); + rehypePlugins: [ + rehypeSlug, + rehypeHighlight, + [ + rehypeExternalLinks, + { target: "_blank", rel: ["nofollow", "noreferrer", "noopener"] }, + ], + ], + }, + }); + if (!blogExists) { + await firestore + .collection("blogs") + .doc(fileNameWithoutExtension) + .set( + { + source, + content: blogMatter.content, + ...blogMatter.data, + dateCreated: blogMatter.data.dateCreated.toUTCString(), + dateUpdated: new Date().toUTCString(), + likes: 0, + link: fileNameWithoutExtension, + }, + { merge: true } + ); + } else { + await firestore + .collection("blogs") + .doc(fileNameWithoutExtension) + .set( + { + source, + content: blogMatter.content, + ...blogMatter.data, + dateCreated: blogMatter.data.dateCreated.toUTCString(), + dateUpdated: new Date().toUTCString(), + link: fileNameWithoutExtension, + }, + { merge: true } + ); + } } } + catch(error){ + console.error("Error processing blogs :", error) + } } main(); From 3adfa3781db8a2639428bdeb677d64d96d0a7a70 Mon Sep 17 00:00:00 2001 From: Milan Date: Thu, 10 Oct 2024 11:14:03 +0545 Subject: [PATCH 2/3] Change blog proceessing code to elimiate redundancy --- publish/index.js | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/publish/index.js b/publish/index.js index e166e9d9..76ead56c 100644 --- a/publish/index.js +++ b/publish/index.js @@ -68,38 +68,17 @@ async function main() { ], }, }); - if (!blogExists) { - await firestore - .collection("blogs") - .doc(fileNameWithoutExtension) - .set( - { - source, - content: blogMatter.content, - ...blogMatter.data, - dateCreated: blogMatter.data.dateCreated.toUTCString(), - dateUpdated: new Date().toUTCString(), - likes: 0, - link: fileNameWithoutExtension, - }, - { merge: true } - ); - } else { - await firestore - .collection("blogs") - .doc(fileNameWithoutExtension) - .set( - { - source, - content: blogMatter.content, - ...blogMatter.data, - dateCreated: blogMatter.data.dateCreated.toUTCString(), - dateUpdated: new Date().toUTCString(), - link: fileNameWithoutExtension, - }, - { merge: true } - ); - } + const blogData = { + source, + content: blogMatter.content, + ...blogMatter.data, + dateCreated: blogMatter.data.dateCreated.toUTCString(), + dateUpdated: new Date().toUTCString(), + likes: blogExists ? (blogMatter.data.likes || 0) : 0, + link: fileNameWithoutExtension, + }; + await firestore.collection('blogs').doc(fileNameWithoutExtension).set(blogData, {merge: true}); + } } catch(error){ From e322caf99e491ee6bcba3273ddf16ba3964e4a76 Mon Sep 17 00:00:00 2001 From: Milan Date: Thu, 10 Oct 2024 11:31:40 +0545 Subject: [PATCH 3/3] directly accessing env variables without checks can lead to runtime errors if any are undefined --- publish/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/publish/index.js b/publish/index.js index 76ead56c..d8818004 100644 --- a/publish/index.js +++ b/publish/index.js @@ -26,6 +26,9 @@ firebaseAdmin.initializeApp({ }), }); +if(!process.env.private_key || !process.env.private_key_id || !process.env.client_email || !process.env.client_id || !process.env.client_cert_url){ + throw new Error("Misssing required environment variables for Firbase initialization"); +} const firestore = firebaseAdmin.firestore(); function getBlogsFileNames() {