update build

This commit is contained in:
peter
2019-08-19 15:35:36 +02:00
parent b33f900806
commit 5d4a7a2aad
6 changed files with 213 additions and 29 deletions
+92
View File
@@ -0,0 +1,92 @@
"use strict";
import { assert } from "bsert"
import * as git from "simple-git/promise"
import * as gyt from "git-describe"
import { promises as fs, mkdirSync } from "fs"
import * as rimraf from "rimraf"
import * as path from "path"
const rmrf = (path:string, options = {}) => {
return new Promise((acc, _) => rimraf(path, options, acc))
}
type RepoFolderStatus = {
exists: boolean
empty: boolean
currentTag?: string
tags?: string[]
latestTag?: string
}
export class GitUpdater{
private readonly repo
constructor(private readonly folderPath:string){
this.folderPath = folderPath;
mkdirSync(this.folderPath, { recursive: true });
this.repo = git(this.folderPath)
}
async getStatus():Promise<RepoFolderStatus>{
try {
await fs.access(this.folderPath);
}
catch (e) {
return { exists: false, empty: true };
}
const files = await fs.readdir(this.folderPath)
const empty:boolean = files.length === 0
await this.repo.fetch("origin", "master", {
"--tags": null
});
const remoteTags = await this.repo.tags();
const desc = await gyt.gitDescribe(this.folderPath, { match: "*" });
return {
exists: true,
empty: empty,
currentTag: !desc.tag?null:desc.tag,
tags: !remoteTags.all?[]:remoteTags.all,
latestTag: remoteTags.latest
};
}
async cloneRepo(from:string, force:boolean = false):Promise<RepoFolderStatus> {
if(force){
await rmrf(this.folderPath)
await fs.mkdir(this.folderPath, { recursive: true });
}
const status = await this.getStatus()
assert(status.exists && status.empty)
await git(this.folderPath).clone(from, path.resolve(this.folderPath));
return await this.getStatus();
}
async checkoutTag(tag:string):Promise<RepoFolderStatus> {
const status = await this.getStatus()
assert(status.exists && status.tags && status.tags.length !== 0)
await this.repo.checkout(tag);
return await this.getStatus();
}
}
/*
(async () => {
const staticPath = path.join(__dirname, "..", "static")
const gl = new GitUpdater(staticPath)
let status = await gl.getStatus()
console.log(status)
if(status.empty){
status = await gl.cloneRepo("https://gitea.frontblock.me/fb-dist/dashboard.git")
console.log(status)
}
status = await gl.checkoutTag(status.latestTag!)
console.log(status)
})()
*/