You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

112 lines
2.2 KiB
JavaScript

class Model {
constructor(http) {
// Initialize isomorphic-git with a file system
window.fs = new LightningFS("fs");
// I prefer using the Promisified version honestly
window.pfs = window.fs.promises;
window.dir = "/";
window.http = http;
}
async dirList(dir = "/") {
let list = await pfs.readdir(dir);
for (let u = 0; u < list.length; u++) {
let re = /^\..*$/;
if (re.test(list[u])) {
list.splice(u, 1);
u--;
}
}
let dirtree = [dir];
for (let i = 0; i < list.length; i++) {
let itemStat = await pfs.stat(dir + list[i]);
if (itemStat.isDirectory()) {
dirtree[i + 1] = await this.dirList(dir + list[i] + "/");
} else {
dirtree[i + 1] = list[i];
}
}
return dirtree;
}
async getDirTree() {
var baseDir = localStorage.getItem("baseDir");
var stat = await pfs.stat(baseDir).catch((e) => {});
if (stat == null) {
var baseDir = dir;
}
var tree = await this.dirList(baseDir);
return tree;
}
async getGitStatusTree() {
var dirtree = await this.getDirTree();
var tree = await this.dirGitStatus(dirtree);
return tree;
}
async dirGitStatus(list) {
let dirlist = [list[0]];
for (let i = 1; i < list.length; i++) {
if (Array.isArray(list[i])) {
await this.dirGitStatus(list[i]);
} else {
list[i] = await git.status({
fs,
dir,
filepath: list[0].substring(1) + list[i],
});
}
}
return list;
}
wipeFS() {
delete window.fs;
window.fs = new LightningFS("fs", { wipe: true });
}
async cloneRep() {
this.wipeFS();
await git.clone({
fs,
http,
dir,
url: localStorage.getItem("repo"),
corsProxy: "https://cors.isomorphic-git.org",
});
}
setRepo(repoURL) {
localStorage.setItem("repo", repoURL);
}
setBaseDir(baseDir) {
if (!baseDir.endsWith("/")) {
baseDir += "/";
}
localStorage.setItem("baseDir", baseDir);
}
readFile(file) {
return pfs.readFile(file, "utf8");
}
saveFile(cesta, obsah) {
pfs.writeFile(cesta, obsah, "utf8");
}
}