Posted on • Originally published atkrzysztofzuraw.com on
Auto update readme on GitHub with Deno
GitHub has this nice feature where you can have areadme file in the repository named after your GitHub handle and it will be displayed on your profile page. I used to have this file automatically generated by a script that would fetch my latest blog posts and display them in the readme. I usedPaweł solution and it worked fine.
I used to run this workflow every week on Sundays with a GitHub Actionschedule](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule). Unfortunately recently it started failing - it turns out that switching toAstro broke the RSS structure. As I was fixing the script I decided to completely rewrite it withDeno.
I choose Deno because I wanted out-of-the-box support for TypeScript. One of the objectives of the rewrite was to have better handling of errors when iterating through the RSS feed. To accomplish that I've usedZod.
You can see the result in the snippet below:
import{z}from"https://deno.land/x/zod@v3.21.4/mod.ts";constResponseSchema=z.array(z.object({title:z.string(),url:z.string().url(),})).nonempty();constresponse=awaitfetch("https://krzysztofzuraw.com/blog.json");constblogPosts=ResponseSchema.parse(awaitresponse.json());constmarkdownPosts=blogPosts.map(({title,url})=>`- [${title}](${url})`).join("\n");constopenTag=`<!-- FEED-START -->`;constcloseTag=`<!-- FEED-END -->`;constfilePath="./README.md";constreadme=awaitDeno.readTextFile(filePath);constindexBefore=readme.indexOf(openTag)+openTag.length;constindexAfter=readme.indexOf(closeTag);constreadmeContentChunkBreakBefore=readme.substring(0,indexBefore);constreadmeContentChunkBreakAfter=readme.substring(indexAfter);constreadmeNew=`${readmeContentChunkBreakBefore}\n${markdownPosts}\n${readmeContentChunkBreakAfter}`;awaitDeno.writeTextFile(filePath,readmeNew);console.log("Readme updated! 🎉");
I did a couple of changes to the script. I decided to add a new JSON page to Astro - it contains the last 3 blog posts - you can see the codehere. I fetchblog.json
and validate it using Zod. To be honest I'm impressed by this library - I previously used it only for form field validation but it works great for validating API responses as well.
I also found out that there is niftyDeno GitHub Action that I can use to set up the Deno environment in CI and that Deno has something likepackage.json
calleddeno.json - it allows you to specify dependencies and scripts. I've used it to set updeno task.
The last change was to run this script not on the schedule but to trigger it when I push any changes to the blog repository. It works well but there is one caveat though - Netlify deployment runs on its own (via the GitHub app) and GitHub action runs too early to catch the changes. I poke around the internet to see if there was a way to trigger GitHub Action after Netlify deployed my blog but I didn't find anything officially supported by Netlify. I decided for now that I can live with that and I will just run this script when I orRenovate merge changes to the master. Maybe I will find a better solution in the future.
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse