From 400ba1053343f3d4e61d73affc10741813d2728e Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 8 Jun 2018 23:04:18 +0100 Subject: progress bar, improved installer --- .eslintrc.js | 6 ++- install.js | 40 +++++++++++++------ pkg93.js | 127 +++++++++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 125 insertions(+), 48 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 9487f17..a882e02 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -17,9 +17,11 @@ module.exports = { "ecmaVersion": 2017 }, "globals": { - "$log": function(a) {}, + "$log": function(){}, "le": { apps: {} - } + }, + "$window": function(){}, + "$alert": function(){} } }; diff --git a/install.js b/install.js index 3dd03ae..0a72476 100644 --- a/install.js +++ b/install.js @@ -1,13 +1,29 @@ -var _main = `var interval = setInterval(function () { - try { - if (!!le) { - localStorage[".pkg93/userscript"] = ""; - clearInterval(interval); - $loader.script("https://rawgit.com/pkg93/pkg93/master/pkg93.js"); - } - } catch (err) { - console.error(err); +$alert({ + msg: "Welcome to teh pkg93 installer, what do you want to do?", + title: "pkg93 installer", + btnOk: "Install/upgrade pkg93", + btnCancel: "Run away", + img: "//cdn.rawgit.com/1024x2/pkg93/70039c02/pkg.png" +}, function(isOK) { + if (isOK) { + var _main = `var interval = setInterval(function () { + try { + if (!!le) { + localStorage[".pkg93/userscript"] = ""; + clearInterval(interval); + $loader.script("https://rawgit.com/pkg93/pkg93/master/pkg93.js"); + } + } catch (err) { + console.error(err); + } + }, 5000);`; + localStorage["boot/pkg93.js"] = _main; + eval(_main); + $alert({ + msg: "pkg93 has installed successfully!", + title: "pkg93 installer", + btnOk: "kthxbai", + img: "//cdn.rawgit.com/1024x2/pkg93/70039c02/pkg.png" + }); } -}, 5000);`; -localStorage["boot/pkg93.js"] = _main; -eval(_main); // jshint ignore:line \ No newline at end of file +}); \ No newline at end of file diff --git a/pkg93.js b/pkg93.js index 7783d51..3b7d3b0 100644 --- a/pkg93.js +++ b/pkg93.js @@ -10,6 +10,7 @@ async function wrap(f) { var newLog = $log(...args); newLog.parentElement.insertBefore(newLog, lastLog.nextSibling); lastLog = newLog; + return newLog; }, arg: this.arg }); @@ -28,7 +29,7 @@ try { } var config = JSON.parse(localStorage[".pkg93/config.json"]); for (let pkg of config.installed) { - eval(localStorage[".pkg93/packages/" + pkg + ".js"]); // jshint ignore:line + eval(localStorage[".pkg93/packages/" + pkg + ".js"]); } } catch (err) { console.error("[pkg93] Couldn't load package information."); @@ -36,6 +37,18 @@ try { console.log("[pkg93] Done!"); console.groupEnd(); +// thanks robbie! sauce: https://gist.github.com/robbie0630/e1386fb10676598e7d60d4f406a41042 +var _abarpkg93uses = (width, percent) => { + if (percent > 1) percent = 1; + let barwidth = width - 7; + let pbar = "["; + for (let i = 0; i < Math.floor(percent*barwidth); ++i) pbar += "="; + for (let i = 0; i < Math.ceil((1-percent)*barwidth); ++i) pbar += "-"; + pbar += "] " + Math.floor(percent * 100) + "%"; + return pbar; +}; + + var pkg93 = { getConfig: function() { try { @@ -47,12 +60,20 @@ var pkg93 = { pull: async function(cli) { cli = cli || {log: (i) => {$log(i);}}; var config = pkg93.getConfig(); - cli.log("WARN Windows93 may lag while getting packages.\n This is a normal thing."); config.pkglist = []; for (let source of config.repos) { - cli.log("GET " + source); - try { - var json = await (await (fetch(source + "/repo.json"))).json(); + cli.log("GET " + source + "/repo.json"); + var bardiv = cli.log(_abarpkg93uses(60, 0)); + var xhr = new XMLHttpRequest(); + xhr.open("GET", source + "/repo.json", true); + xhr.onprogress = e => { + bardiv.innerHTML = _abarpkg93uses(60, e.loaded / e.total); + }; + xhr.onerror = () => { + cli.log("ERR Fatal error while retriving package.json."); + }; + xhr.onload = () => { + var json = JSON.parse(xhr.responseText); cli.log("NAME " + json.name); cli.log("MSG " + json.msg); for (let item of json.packages) { @@ -63,13 +84,12 @@ var pkg93 = { cli.log("ERR " + err.message); } } - } catch (err) { - cli.log("ERR " + err.message); - } + }; + xhr.send(); } localStorage[".pkg93/config.json"] = JSON.stringify(config); }, - get: async function(pkg, cli) { + get: async function(pkg, cli, callback) { cli = cli || {log: (i) => {$log(i);}}; var config = pkg93.getConfig(); cli.log("SRCH " + pkg); @@ -83,38 +103,77 @@ var pkg93 = { cli.log("OK Found!"); var pkgname = config.pkglist[index].split("@")[0]; var pkgsource = config.pkglist[index].split("@")[1]; - try { - var json = await (await (fetch(pkgsource + "/" + pkgname + "/package.json"))).json(); - localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json); - if (json.dependencies) { - for (let pkg of json.dependencies) { + cli.log("GET " + pkgsource + "/" + pkgname + "/package.json"); + var bardiv = cli.log(_abarpkg93uses(60, 0)); + var xhr = new XMLHttpRequest(); + xhr.open("GET", pkgsource + "/" + pkgname + "/package.json", true); + xhr.setRequestHeader("X-Requested-With", "pkg93"); + xhr.onprogress = e => { + try { + bardiv.innerHTML = _abarpkg93uses(60, e.loaded / e.total); + } catch (err) { + // fail silently + } + }; + xhr.onerror = () => { + cli.log("ERR Fatal error while retriving package.json."); + callback(false); + }; + xhr.onload = () => { + try { + cli.log("DONE " + pkgsource + "/" + pkgname + "/package.json"); + var json = JSON.parse(xhr.responseText); + localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json); + cli.log("GET " + pkgsource + "/" + pkgname + "/" + json.inject); + var bardiv2 = cli.log(_abarpkg93uses(60, 0)); + var xhr2 = new XMLHttpRequest(); + xhr2.open("GET", pkgsource + "/" + pkgname + "/" + json.inject); + xhr2.setRequestHeader("X-Requested-With", "pkg93"); + xhr2.onprogress = e => { + try { + bardiv2.innerHTML = _abarpkg93uses(60, e.loaded / e.total); + } catch (err) { + // fail silently + } + }; + xhr2.onerror = () => { + cli.log("ERR Fatal error while retriving " + pkgsource + "/" + pkgname + "/" + json.inject); + callback(false); + }; + xhr2.onload = async () => { try { - cli.log("DPND Getting dependency \"" + pkg + "\""); - var output = await pkg93.get(pkg, cli); - if (!output) { - throw new Error("Dependency \"" + pkg + "\" failed to install. Current package may not work!"); + cli.log("DONE " + pkgsource + "/" + pkgname + "/" + json.inject); + var script = xhr2.responseText; + localStorage[".pkg93/packages/" + pkgname + ".js"] = script; + eval(script); + if (json.uninstall) { + // no xhr this time + var uninst = await (await (fetch(pkgsource + "/" + pkgname + "/" + json.uninstall))).text(); + localStorage[".pkg93/packages/" + pkgname + ".rm.js"] = uninst; + } + cli.log("OK Injected package!"); + if (!config.installed.includes(pkgname)) { + config.installed.push(pkgname); } + localStorage[".pkg93/config.json"] = JSON.stringify(config); + callback(true); + // can i go home now } catch (err) { cli.log("ERR " + err.message); + callback(false, err); } - } - } - var script = await (await (fetch(pkgsource + "/" + pkgname + "/" + json.inject))).text(); - localStorage[".pkg93/packages/" + pkgname + ".js"] = script; - eval(script); - if (json.uninstall) { - var uninst = await (await (fetch(pkgsource + "/" + pkgname + "/" + json.uninstall))).text(); - localStorage[".pkg93/packages/" + pkgname + ".rm.js"] = uninst; + }; + xhr2.send(); + } catch (err) { + cli.log("ERR " + err.message); + callback(false, err); } - cli.log("OK Injected package!"); - if (!config.installed.includes(pkgname)) { - config.installed.push(pkgname); - } - localStorage[".pkg93/config.json"] = JSON.stringify(config); - return true; + }; + try { + xhr.send(); } catch (err) { cli.log("ERR " + err.message); - return false; + callback(false, err); } } }, @@ -227,7 +286,7 @@ If you find my software useful, consider donating ERR You're trying to modify a pre-installed Windows93 app.\n Don't do that!"); } else { - await pkg93.get(args[1], cli); + await pkg93.get(args[1], cli, function() {}); } } else if (args[0] == "rm") { if (args.length < 2) { -- cgit v1.2.3