diff options
author | Minteck <contact@minteck.org> | 2022-02-12 10:33:06 +0100 |
---|---|---|
committer | Minteck <contact@minteck.org> | 2022-02-12 10:33:06 +0100 |
commit | 01160246e4a0c0052181c72a53737e356ea7d02d (patch) | |
tree | c6f8ea675f9147d4c06ef503697fb35d58493991 /hooks | |
parent | af898a152a14e31bdbcbbedb952ad333697553ef (diff) | |
download | twilight-01160246e4a0c0052181c72a53737e356ea7d02d.tar.gz twilight-01160246e4a0c0052181c72a53737e356ea7d02d.tar.bz2 twilight-01160246e4a0c0052181c72a53737e356ea7d02d.zip |
First commit
Diffstat (limited to 'hooks')
-rw-r--r-- | hooks/apply_update.js | 8 | ||||
-rw-r--r-- | hooks/clone.js | 28 | ||||
-rw-r--r-- | hooks/diff.js | 42 | ||||
-rw-r--r-- | hooks/files.js | 21 | ||||
-rw-r--r-- | hooks/size.js | 31 | ||||
-rw-r--r-- | hooks/update.js | 69 |
6 files changed, 199 insertions, 0 deletions
diff --git a/hooks/apply_update.js b/hooks/apply_update.js new file mode 100644 index 0000000..8ad9108 --- /dev/null +++ b/hooks/apply_update.js @@ -0,0 +1,8 @@ +module.exports = (pkg, tempDir) => { + files = require('./files')(tempDir); + for (let file of files) { + if (file.trim() !== "") { + fs.copyFileSync(home + "/packages/" + tempDir + "/" + file, home + "/packages/" + pkg + "/" + file); + } + } +}
\ No newline at end of file diff --git a/hooks/clone.js b/hooks/clone.js new file mode 100644 index 0000000..77c5a33 --- /dev/null +++ b/hooks/clone.js @@ -0,0 +1,28 @@ +module.exports = (repo, branch, callback) => { + const spinner = ora("Downloading package...").start(); + + if (os.platform() === "win32") { + git = require('child_process').execSync("where git").toString().trim(); + } else { + git = require('child_process').execSync("which git").toString().trim(); + } + + cp = require('child_process').spawn(git, ["clone", "--progress", "-b", branch, "--", repo, home + "/buildroot"], {/*stdio: "inherit"*/}); + + cp.stdout.on('data', (data) => { + spinner.text = data.toString().trim().split("\n")[data.toString().trim().split("\n").length - 1]; + }) + + cp.stderr.on('data', (data) => { + spinner.text = data.toString().trim().split("\n")[data.toString().trim().split("\n").length - 1].replace(/[^0-9a-zA-z: -,.!? \/\(\)]*/gm, ""); + }) + + cp.on('close', (code) => { + if (code !== 0) { + throw new Error("Process exited with code " + code); + } else { + spinner.succeed("Downloading package... done"); + callback(); + } + }) +}
\ No newline at end of file diff --git a/hooks/diff.js b/hooks/diff.js new file mode 100644 index 0000000..b4b49e4 --- /dev/null +++ b/hooks/diff.js @@ -0,0 +1,42 @@ +const getAllFiles = function(dirPath, arrayOfFiles) { + files = fs.readdirSync(dirPath) + + arrayOfFiles = arrayOfFiles || [] + + files.forEach(function(file) { + if (file !== ".git") { + if (fs.statSync(dirPath + "/" + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) + } else { + arrayOfFiles.push(dirPath + "/" + file) + } + } + }) + + return arrayOfFiles +} + +module.exports = (pkg1, pkg2) => { + asize1 = getAllFiles((home + "/packages/" + pkg1).replaceAll("\\", "/")).map(i => i.replaceAll("\\", "/").replaceAll((home + "/packages/" + pkg1).replaceAll("\\", "/") + "/", "")).map(i => fs.readFileSync(home + "/packages/" + pkg1 + "/" + i).length).reduce((a, b) => a + b); + asize2 = getAllFiles((home + "/packages/" + pkg2).replaceAll("\\", "/")).map(i => i.replaceAll("\\", "/").replaceAll((home + "/packages/" + pkg2).replaceAll("\\", "/") + "/", "")).map(i => fs.readFileSync(home + "/packages/" + pkg2 + "/" + i).length).reduce((a, b) => a + b); + + asize = asize2 - asize1; + if (asize > 0) { + s = "+"; + } else if (asize < 0) { + s = "-"; + } else { + s = "±"; + } + asize = Math.abs(asize); + + if (asize > 1024) { + if (asize > 1048576) { + return s + (asize / 1048576).toFixed(1) + "M"; + } else { + return s + (asize / 1024).toFixed(1) + "K"; + } + } else { + return s + asize + "B"; + } +}
\ No newline at end of file diff --git a/hooks/files.js b/hooks/files.js new file mode 100644 index 0000000..42a16aa --- /dev/null +++ b/hooks/files.js @@ -0,0 +1,21 @@ +const getAllFiles = function(dirPath, arrayOfFiles) { + files = fs.readdirSync(dirPath) + + arrayOfFiles = arrayOfFiles || [] + + files.forEach(function(file) { + if (file !== ".git") { + if (fs.statSync(dirPath + "/" + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) + } else { + arrayOfFiles.push(dirPath + "/" + file) + } + } + }) + + return arrayOfFiles +} + +module.exports = (pkg) => { + return getAllFiles((home + "/packages/" + pkg).replaceAll("\\", "/")).map(i => i.replaceAll("\\", "/").replaceAll((home + "/packages/" + pkg).replaceAll("\\", "/") + "/", "")); +}
\ No newline at end of file diff --git a/hooks/size.js b/hooks/size.js new file mode 100644 index 0000000..1068e11 --- /dev/null +++ b/hooks/size.js @@ -0,0 +1,31 @@ +const getAllFiles = function(dirPath, arrayOfFiles) { + files = fs.readdirSync(dirPath) + + arrayOfFiles = arrayOfFiles || [] + + files.forEach(function(file) { + if (file !== ".git") { + if (fs.statSync(dirPath + "/" + file).isDirectory()) { + arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles) + } else { + arrayOfFiles.push(dirPath + "/" + file) + } + } + }) + + return arrayOfFiles +} + +module.exports = (pkg) => { + asize = getAllFiles((home + "/packages/" + pkg).replaceAll("\\", "/")).map(i => i.replaceAll("\\", "/").replaceAll((home + "/packages/" + pkg).replaceAll("\\", "/") + "/", "")).map(i => fs.readFileSync(home + "/packages/" + pkg + "/" + i).length).reduce((a, b) => a + b); + + if (asize > 1024) { + if (asize > 1048576) { + return (asize / 1048576).toFixed(1) + "M"; + } else { + return (asize / 1024).toFixed(1) + "K"; + } + } else { + return asize + "B"; + } +}
\ No newline at end of file diff --git a/hooks/update.js b/hooks/update.js new file mode 100644 index 0000000..f871889 --- /dev/null +++ b/hooks/update.js @@ -0,0 +1,69 @@ +module.exports = async () => { + const spinner = ora("Fetching package lists...").start(); + let list = (await axios.get("https://twipkg.cdn.minteck.org/packages.json")).data; + + if (fs.existsSync(home + "/repository")) { + fs.rmSync(home + "/repository", { recursive: true }); + } + fs.mkdirSync(home + "/repository"); + + fs.writeFileSync(home + "/repository/list.json", JSON.stringify(list)); + + let index = 0; + for (let pkg of list) { + spinner.text = "Fetching package lists... " + Math.round((index / list.length) * 100) + "%"; + let dir = pkg.substring(0, 1).replace(/[^a-zA-Z0-9]/gm, "#"); + let pack = (await axios.get("https://twipkg.cdn.minteck.org/" + dir + "/" + pkg + ".json")).data; + + let verdata = (await axios.get(pack.version)).data; + pack.verdata = { + latest: verdata.commit.short_id, + publisher: { + name: verdata.commit.author_name, + email: verdata.commit.author_email + }, + date: verdata.commit.created_at + } + + let signRaw = { error: "404 Not Found" }; + try { + signRaw = (await axios.get(pack.signature.replace("{version}", verdata.commit.id))).data; + } catch (e) {} + pack.sign = { + signed: false, + verified: false, + key: null, + signer: { + name: null, + email: null + } + } + if (signRaw.error !== "404 Not Found") { + pack.sign.signed = true; + if (signRaw.verification_status === "verified") { + pack.sign.verified = true; + } else { + pack.sign.verified = false; + } + if (signRaw.gpg_key_user_name) { + pack.sign.signer.name = signRaw.gpg_key_user_name; + } + if (signRaw.gpg_key_user_email) { + pack.sign.signer.email = signRaw.gpg_key_user_email; + } + if (signRaw.gpg_key_primary_keyid) { + pack.sign.key = signRaw.gpg_key_primary_keyid; + } + } + + if (!fs.existsSync(home + "/repository/" + dir)) { + fs.mkdirSync(home + "/repository/" + dir); + } + fs.writeFileSync(home + "/repository/" + dir + "/" + pkg + ".json", JSON.stringify(pack)); + + index++; + } + + spinner.succeed("Fetching package lists... done"); + //process.exit(); +}
\ No newline at end of file |