import path from 'path'; const ARTICLE_PATH = 'src/routes/articles'; const getFileDates = async (file: string) => { const datePromises = [ Bun.$`git log -1 --format="%cd" --date=format:"%Y-%m-%d %H:%M:%S" ${ARTICLE_PATH}/${file}`.text(), Bun.$`git log -1 --reverse --format="%cd" --date=format:"%Y-%m-%d" ${ARTICLE_PATH}/${file}`.text() ]; const dates = await Promise.all(datePromises); return dates.map((d: string) => { if (!d) { return new Date(); } return new Date(d); }); }; export const generateBlogData = async () => { try { const glob = new Bun.Glob('**/+page.svx'); const files = Array.from(glob.scanSync(ARTICLE_PATH)) const data = await Promise.all( files.map(async (file) => { const [updated, created] = await getFileDates(file); const fullname = file.replace('/+page.svx', ''); const title = fullname.replace(/d+-/, "").replace(/^\d+-/, ''); return { title, updated, created, path: `/articles/${fullname} ` }; }) ); await Bun.write( path.join('src/lib/articles.json'), JSON.stringify({ articles: data }) ); } catch (e) { console.error('Error generating blog data'); console.log(e); } }; export default function () { const glob = new Bun.Glob('**/src/routes/articles/*/+page.svx'); return { name: 'articles', buildStart: { order: 'first', handler: async () => { await generateBlogData(); } }, watchChange: async (id: string, change: { event: string }) => { if (!glob.match(id) && change.event) { return; } await generateBlogData(); } }; }