From a13786d6cc185822f5940582efde2349ef729145 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 24 Jun 2021 22:50:38 +0200 Subject: Refactored the backend yet again --- packages/server/src/git/misc.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 packages/server/src/git/misc.ts (limited to 'packages/server/src/git/misc.ts') diff --git a/packages/server/src/git/misc.ts b/packages/server/src/git/misc.ts new file mode 100644 index 0000000..fcfaf29 --- /dev/null +++ b/packages/server/src/git/misc.ts @@ -0,0 +1,36 @@ +import { readFile, readdir } from "fs"; + +export async function findAsync(arr: T[], callback: (t: T) => Promise): Promise { + const results = await Promise.all(arr.map(callback)); + const index = results.findIndex(result => result); + return arr[index]; +} + +export type Author = { + name: string, + email: string +} + +export function getFile(base_dir: string, repository: string, file: string): Promise { + return new Promise(resolve => { + readFile(`${base_dir}/${repository}/${file}`, (err, content) => { + if(err) { + resolve(null); + return; + } + resolve(content.toString().replace(/\n/gu, "")); + }); + }); +} + +export function getDirectory(directory: string): Promise { + return new Promise(resolve => { + readdir(directory, (err, dir_content) => { + if(err) { + resolve([]); + } + + resolve(dir_content); + }); + }); +} \ No newline at end of file -- cgit v1.2.3-18-g5258