aboutsummaryrefslogtreecommitdiff
path: root/app/static/package_create.js
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-05-09 17:44:58 +0100
committerrubenwardy <rw@rubenwardy.com>2018-05-09 17:44:58 +0100
commit552e35f6aaa4bbbb680aa8fae89fd0904dd5f5f4 (patch)
tree7786a52ab33b9a794879574350545d3abc56dbaf /app/static/package_create.js
parent6e1921f68b9cdef739cb1bd91b972a47fc7cf7b0 (diff)
downloadcheatdb-552e35f6aaa4bbbb680aa8fae89fd0904dd5f5f4.tar.xz
Add meta importing from Github
Fixes #11
Diffstat (limited to 'app/static/package_create.js')
-rw-r--r--app/static/package_create.js106
1 files changed, 106 insertions, 0 deletions
diff --git a/app/static/package_create.js b/app/static/package_create.js
new file mode 100644
index 0000000..0bea7ca
--- /dev/null
+++ b/app/static/package_create.js
@@ -0,0 +1,106 @@
+$(function() {
+ function readConfig(text) {
+ var retval = {}
+
+ const lines = text.split("\n")
+ for (var i = 0; i < lines.length; i++) {
+ const idx = lines[i].indexOf("=")
+ if (idx > 0) {
+ const name = lines[i].substring(0, idx - 1).trim()
+ const value = lines[i].substring(idx + 1).trim()
+ retval[name] = value
+ }
+ }
+
+ return retval
+ }
+
+ function finish() {
+ $(".pkg_wiz_1").hide()
+ $(".pkg_wiz_2").hide()
+ $(".pkg_repo").show()
+ $(".pkg_meta").show()
+ }
+
+ function repoIsSupported(url) {
+ try {
+ return URI(url).hostname() == "github.com"
+ } catch(e) {
+ return false
+ }
+ }
+
+ function getFile(url) {
+ return new Promise(function(resolve, reject) {
+ fetch(url).then(function(response) {
+ response.text().then(resolve).catch(reject)
+ }).catch(reject)
+ })
+ }
+
+ function getInfo(baseUrl) {
+ return new Promise(function(resolve, reject) {
+ getFile(baseUrl + "/mod.conf").then(function(text) {
+ var config = readConfig(text)
+
+ if (config["name"]) {
+ $("#name").val(config["name"])
+ }
+
+ if (config["description"]) {
+ const desc = config["description"]
+ const idx = desc.indexOf(".")
+ $("#shortDesc").val((idx < 5 || idx > 100) ? desc.substring(0, 100) : desc.substring(0, idx))
+ $("#desc").val(desc)
+ }
+
+ resolve()
+ }).catch(function() {
+ reject()
+ })
+ })
+ }
+
+ function importInfo(urlstr) {
+ // Convert to HTTPs
+ try {
+ var url = URI(urlstr).scheme("https")
+ .username("")
+ .password("")
+ } catch(e) {
+ return Promise.reject(e)
+ }
+ // Change domain
+ url = url.hostname("raw.githubusercontent.com")
+
+ // Rewrite path
+ const re = /^\/([^\/]+)\/([^\/]+)\/?$/
+ const results = re.exec(url.path())
+ if (results == null || results.length != 3) {
+ return Promise.reject("Unable to parse URL - please provide a direct URL to the repo")
+ }
+ url.path("/" + results[1] + "/" + results[2].replace(".git", "") + "/master")
+
+ return getInfo(url.toString())
+ }
+
+ $(".pkg_meta").hide()
+ $(".pkg_wiz_1").show()
+ $("#pkg_wiz_1_next").click(function() {
+ const repoURL = $("#repo").val();
+ if (repoIsSupported(repoURL)) {
+ $(".pkg_wiz_1").hide()
+ $(".pkg_wiz_2").show()
+ $(".pkg_repo").hide()
+
+ importInfo(repoURL).then(finish).catch(function(x) {
+ alert(x)
+ $(".pkg_wiz_1").show()
+ $(".pkg_wiz_2").hide()
+ $(".pkg_repo").show()
+ })
+ } else {
+ finish()
+ }
+ })
+})