aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/misc.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-26 23:56:53 +0200
committerHampusM <hampus@hampusmat.com>2021-07-26 23:56:53 +0200
commit09ff261f156a8599d45c1496fe246ded6e035191 (patch)
tree306f79709da372b562eca70d356fa25afcde1f95 /packages/server/src/git/misc.ts
parentc575df758b269db8e05c597d5870e948771d4c2b (diff)
Added backend TSDoc/JSDoc comments & refactored a tiny bit
Diffstat (limited to 'packages/server/src/git/misc.ts')
-rw-r--r--packages/server/src/git/misc.ts20
1 files changed, 20 insertions, 0 deletions
diff --git a/packages/server/src/git/misc.ts b/packages/server/src/git/misc.ts
index bd18322..da9b1ee 100644
--- a/packages/server/src/git/misc.ts
+++ b/packages/server/src/git/misc.ts
@@ -1,12 +1,26 @@
import { readFile, readdir } from "fs";
import { createError, MiscError } from "./error";
+/**
+ * Asynchronously find an item in an array that matches the requirements set by the callback
+ *
+ * @param arr - The array to look in
+ * @param callback - A callback that knowns what you're looking for
+ * @returns The item in the array you wanted to find
+ */
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];
}
+/**
+ * Returns the content of a file inside a repository
+ *
+ * @param base_dir - The directory which the repository is in
+ * @param repository - The directory of a bare repository
+ * @param file - The path of a file
+ */
export function getFile(base_dir: string, repository: string, file: string): Promise<string> {
return new Promise((resolve, reject) => {
readFile(`${base_dir}/${repository}/${file}`, (err, content) => {
@@ -20,6 +34,12 @@ export function getFile(base_dir: string, repository: string, file: string): Pro
});
}
+/**
+ * Returns all of the files & folders inside of a directory
+ *
+ * @param directory - The directory to look in
+ * @returns An array of directory content
+ */
export function getDirectory(directory: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
readdir(directory, (err, dir_content) => {