Skip to content

Lack of error handling can lead to uninformative crashes. #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 50 additions & 62 deletions publish/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,67 @@ 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() {
return fs.readdirSync(path.resolve(process.cwd(), "blogs"));
}

async function main() {
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"
);
try{

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,
],
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 }
);
},
});
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){
console.error("Error processing blogs :", error)
}
}

main();