aboutsummaryrefslogtreecommitdiff
path: root/app/static/package_create.js
blob: 0bea7ca04166ae634ac1cba466c76d42489aa795 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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()
		}
	})
})