diff --git a/main.js b/main.js index 40e3822..b9c35e4 100644 --- a/main.js +++ b/main.js @@ -1,17 +1,20 @@ const express = require('express'); const rest = require('superagent'); +const simpleGit = require('simple-git'); +const fs = require('fs'); + +const app = express(); const giteaHost = 'git.adb.sh'; -const app = express(); app.get('/', function (req, res) { res.send('Hello World'); }); app.listen(3000); -let updateRepos = setInterval(()=>{ - rest.get('https://git.adb.sh/api/v1/repos/search?q=git.adb.sh').then(res=>{ +let findRepos = setInterval(()=>{ + rest.get(`https://${giteaHost}/api/v1/repos/search?q=${giteaHost}`).then(res=>{ console.log(res.text); let content = JSON.parse(res.text); content.data.forEach(repo=>{ @@ -19,7 +22,20 @@ let updateRepos = setInterval(()=>{ let namePattern = new RegExp(`^(.*.)|()${repo.owner.username}.${giteaHost}`); if (!repo.name.match(namePattern)) return; //matches owner? console.log(`found => ${repo.owner.username}.${giteaHost}`) - + updateRepo(repo); }); }).catch(console.error); -},1000); \ No newline at end of file +},5*1000); + +function updateRepo(repo){ + if (!fs.existsSync(`./repoStorage/${repo.name}`)){ + let git = simpleGit(`./repoStorage/`); + git.clone(repo.clone_url); + console.log(`clone ${repo.name}`); + }else{ + let git = simpleGit(`./repoStorage/`); + git.fetch(repo.clone_url); + git.reset('hard'); + console.log(`fetch/reset ${repo.name}`); + } +} \ No newline at end of file