diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-24 22:50:38 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-24 22:50:38 +0200 |
commit | a13786d6cc185822f5940582efde2349ef729145 (patch) | |
tree | 7d4f49b50fc30ced65c5661b22b027456b79948e /packages/server/src/git/misc.ts | |
parent | 01e5d215dbc152e34ecd005111171457f87c235d (diff) |
Refactored the backend yet again
Diffstat (limited to 'packages/server/src/git/misc.ts')
-rw-r--r-- | packages/server/src/git/misc.ts | 36 |
1 files changed, 36 insertions, 0 deletions
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<T>(arr: T[], callback: (t: T) => Promise<boolean>): Promise<T> { + 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<string | null> { + 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<string[]> { + return new Promise<string[]>(resolve => { + readdir(directory, (err, dir_content) => { + if(err) { + resolve([]); + } + + resolve(dir_content); + }); + }); +}
\ No newline at end of file |