microblog/lib/getPosts.tsx

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-11-06 21:07:22 +01:00
import { exec } from 'child_process';
2022-11-06 21:46:56 +01:00
import { cache } from 'react';
2022-11-06 21:07:22 +01:00
export type Post = {
title: string;
message?: string;
author: {
email: string;
name: string;
};
time: number;
};
2022-11-06 21:46:56 +01:00
export const getPosts = cache(async (): Promise<Post[]> => {
2022-11-06 22:52:29 +01:00
const updatePosts = async () => {
return new Promise((resolve, reject) => {
exec(
`(cd ${process.env.POSTS_DIR}; git pull --rebase)`,
(error, stdout, stderr) => {
if (error) {
console.warn(error);
reject(
'could not update posts stderr: ' + stderr + 'stdout: ' + stdout,
);
return;
}
resolve(true);
},
);
});
};
2022-11-06 21:46:56 +01:00
await updatePosts();
2022-11-06 21:07:22 +01:00
return new Promise((resolve, reject) => {
exec(`git-log --path ${process.env.POSTS_DIR}`, (error, stdout, stderr) => {
if (error) {
console.warn(error);
reject('could not load posts stderr: ' + stderr + 'stdout: ' + stdout);
return;
}
const posts: Post[] = JSON.parse(stdout);
resolve(posts);
});
});
2022-11-06 21:46:56 +01:00
});