import { exec } from 'child_process';

export type Post = {
  title: string;
  message?: string;
  author: {
    email: string;
    name: string;
  };
  time: number;
};

const posts: Post[] = [
  {
    author: 'kjuulh',
    post: 'some post\n',
  },
  {
    author: 'kjuulh',
    post: `some other post with
    some more text`,
  },
];

export const getPosts = async (): Promise<Post[]> => {
  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);
    });
  });
};

export const revalidate = 5;
