aboutsummaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/git.js112
-rw-r--r--api/v1.js32
2 files changed, 92 insertions, 52 deletions
diff --git a/api/git.js b/api/git.js
index b8d89c3..59f7371 100644
--- a/api/git.js
+++ b/api/git.js
@@ -1,12 +1,14 @@
const { exec } = require("child_process");
+const { differenceInMilliseconds } = require('date-fns');
+const fs = require('fs');
-const log_format='{"hash": "%H", "author": "%an", "author_email": "%ae", "date": "%at", "subject": "%s"}'
+const log_format='{"hash": "%H", "author": "%an", "author_email": "%ae", "date": "%at", "subject": "%s"}';
-function getLog(path)
+function execGit(path, action , format, args = "")
{
return new Promise((resolve) =>
{
- exec(`git -C ${path} log --format=format:'${log_format}'`, (error, stdout, stderr) =>
+ exec(`git -C ${path} ${action} ${args} --format=format:'${format}'`, (error, stdout, stderr) =>
{
if(error) {
const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`);
@@ -23,40 +25,98 @@ function getLog(path)
return;
}
- const log = stdout.split('\n');
- log.forEach((entry, index) => log[index] = JSON.parse(entry));
+ resolve({ "data": stdout });
+ });
+ });
+}
+
+async function getLog(path)
+{
+ let log = await execGit(path, "log", log_format);
+
+ if(!log["error"]) {
+ log["data"] = log["data"].split('\n');
+ log["data"].forEach((entry, index) => log["data"][index] = JSON.parse(entry));
+ }
+
+ return log;
+}
+
+function getOptimalDateDifference(difference)
+{
+ const time_values = {
+ "second": 1000,
+ "minute": 60000,
+ "hour": 3600000,
+ "day": 86400000,
+ "week": 604800000,
+ "month": 2629800000,
+ "year": 31557600000
+ };
+
+ let last;
+ for(const [key, value] of Object.entries(time_values)) {
+ if(difference > value) {
+ last = key;
+ continue;
+ }
+ break;
+ }
+
+ return `${Math.round(difference / time_values[last])} ${last}s`;
+}
+
+async function getTimeSinceLatestCommit(path)
+{
+ let commit_timestamp = (await execGit(path, "log", "%at", "-n 1"));
+
+ if(!commit_timestamp["error"]) {
+ commit_timestamp = commit_timestamp["data"] * 1000;
+
+ const commit_date = new Date(commit_timestamp);
+ const now_date = new Date();
+
+ const difference = getOptimalDateDifference(differenceInMilliseconds(now_date, commit_date));
+
+ return difference;
+ }
+ return commit_timestamp;
+}
- resolve({ "data": log });
+function getRepoFile(base_dir, repo, file)
+{
+ return new Promise(resolve =>
+ {
+ fs.readFile(`${base_dir}/${repo}/${file}`, async (err, content) =>
+ {
+ if(!err) {
+ resolve(content.toString().replaceAll('\n', ''));
+ return;
+ }
+ resolve("");
});
});
}
-function getTimeSinceLatestCommit(path)
+function getBasicRepoInfo(base_dir, repo_dirs)
{
return new Promise((resolve) =>
{
- exec(`git -C ${path} log -n 1 --date=unix'`, (error, stdout, stderr) =>
+ let repos = {};
+ repo_dirs.forEach(async (repo, index, arr) =>
{
- if(error) {
- const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`);
+ const desc = await getRepoFile(base_dir, repo, "description");
+ const owner = await getRepoFile(base_dir, repo, "owner");
+ const last_commit_date = await getTimeSinceLatestCommit(`${base_dir}/${repo}`);
- if(no_such_fileor_dir.test(error.message)) {
- resolve({ "error": 404 });
- return;
- }
- resolve({ "error": error.message });
- return;
- }
- if(stderr) {
- resolve({ "error": "Failed to communicate with git!" });
- return;
- }
+ let repo_name = "";
+ repo_name = repo.slice(0, -4);
+ repos[repo_name] = { "description": desc, "owner": owner, "last_updated": last_commit_date };
- const
-
- resolve({ "data": log });
- });
+ if(index === 0) resolve(repos);
+ });
});
}
-module.exports.getLog = getLog; \ No newline at end of file
+module.exports.getLog = getLog;
+module.exports.getBasicRepoInfo = getBasicRepoInfo; \ No newline at end of file
diff --git a/api/v1.js b/api/v1.js
index 6b96e94..75509ad 100644
--- a/api/v1.js
+++ b/api/v1.js
@@ -8,40 +8,20 @@ const base_dir="/home/hampus/Projects/"
router.get("/repos", async function(req, res)
{
- fs.readdir(base_dir, (err, files) =>
+ fs.readdir(base_dir, async (err, repo_dirs) =>
{
if(err) {
throw err;
}
+ repo_dirs = repo_dirs.filter(repo => repo.endsWith(".git"));
- var repos = {};
+ console.log("Repo dirs: " + repo_dirs);
- files = files.filter(repo => repo.endsWith(".git"));
+ const repos = await git.getBasicRepoInfo(base_dir, repo_dirs);
- let getRepoInfo = new Promise((resolve) =>
- {
- files.forEach((repo, index, arr) =>
- {
- fs.readFile(`${base_dir}/${repo}/description`, (err, content) =>
- {
- let desc = "";
-
- if(!err) {
- desc = content.toString();
- }
+ console.log("I v1.js\n" + JSON.stringify(repos) + "\n");
- repos[repo.slice(0, -4)] = { "description": desc };
-
- if(index === arr.length -1) resolve();
- })
- });
- });
-
- getRepoInfo.then(() =>
- {
- console.log(`Sist: ${JSON.stringify(repos)}`);
- res.json({ "data": repos });
- })
+ res.json({ "data": repos });
});
});