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; } /* ================== Dir Tree ================== */ 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); this.dirTree = tree; await this.getGitStatusTree(); } async getGitStatusTree() { this.gitDirTreeStat = await this.dirGitStatus(this.dirTree); } async dirGitStatus(list) { let statList = []; for (let i = 0; i < list.length; i++) { if (Array.isArray(list[i])) { statList[i] = await this.dirGitStatus(list[i]); } else { if (i == 0) { statList[i] = list[0]; } else { statList[i] = await git.status({ fs, dir, filepath: list[0].substring(1) + list[i], }); } } } return statList; } async getChangedFiles() { this.chFiles = await this.changedFiles(this.dirTree, this.gitDirTreeStat); return this.chFiles; } async changedFiles(dirTree, statTree) { var changedFiles = []; for (let i = 1; i < dirTree.length; i++) { if (Array.isArray(dirTree[i])) { var vnorena = await this.changedFiles(dirTree[i], statTree[i]); if (vnorena.length != 0) { for (let i = 0; i < vnorena.length; i++) { changedFiles.push(vnorena[i]); } } } else { if (statTree[i] != "unmodified") { changedFiles.push(dirTree[0] + dirTree[i]); } } } return changedFiles; } /* ================== Text Area ================== */ readFile(file) { return pfs.readFile(file, "utf8"); } async saveFile(cesta, obsah) { pfs.writeFile(cesta, obsah, "utf8"); await this.getGitStatusTree(); } /* ================== Controls ================== */ async wipeFS() { delete window.fs; window.fs = new LightningFS("fs", { wipe: true }); await this.getDirTree(); localStorage.setItem("pushError", "false") } async cloneRep() { this.wipeFS(); await git.clone({ fs, http, dir, url: localStorage.getItem("repo"), corsProxy: "https://cors.isomorphic-git.org", }); await this.getDirTree(); } async pullRep() { await git.pull({ fs, http, dir, fastForwardOnly: true, author: { name: localStorage.getItem("name"), email: localStorage.getItem("email"), }, }); await this.getDirTree(); } async gitAddAll(dirTree) { if (!dirTree) { dirTree = await this.getDirTree(); } /* for (let i = 1; i < dirTree.length; i++) { if (Array.isArray(dirTree[i])) { await this.gitAddAll(dirTree[i]); } else { await git.add({ fs, dir, filepath: dirTree[0].substring(1) + dirTree[i], }); console.log(dirTree[0].substring(1) + dirTree[i]); } } */ await git.add({ fs, dir, filepath: ".", }); } async gitAdd() { let filesToAdd = this.chFiles; for (let i = 0; i < filesToAdd.length; i++) { console.log(filesToAdd[i]) await git.add({ fs, dir, filepath: filesToAdd[i].substring(1), }); } /* for (let i = 1; i < dirTree.length; i++) { if (Array.isArray(dirTree[i])) { await this.gitAddAll(dirTree[i]); } else { await git.add({ fs, dir, filepath: dirTree[0].substring(1) + dirTree[i], }); console.log(dirTree[0].substring(1) + dirTree[i]); } } */ } async gitCommit(msg) { await git.commit({ fs, dir, message: msg, author: { name: localStorage.getItem("name"), email: localStorage.getItem("email"), }, }); await this.getGitStatusTree(); } async gitPush(gitUser, gitPass) { let pushResult = await git.push({ fs, http, dir, onAuth: (url) => { const auth = { username: gitUser, password: gitPass, }; return auth; }, }); console.log(pushResult); } /* ================== Windows ================== */ /* ------------------ Settings ------------------ */ setRepo(repoURL) { localStorage.setItem("repo", repoURL); } async setBaseDir(baseDir) { if (!baseDir.endsWith("/")) { baseDir += "/"; } localStorage.setItem("baseDir", baseDir); await this.getDirTree(); } setName(name) { localStorage.setItem("name", name); } setEmail(email) { localStorage.setItem("email", email); } }