aboutsummaryrefslogtreecommitdiffstats
path: root/pkg93.js
blob: b7e3fd262f6ea9c537910cab3879888749f8fc30 (plain) (blame)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/***************************
 * This code was created   *
 * by 1024x2, and is       *
 * licensed under the MIT  *
 * License, which means    *
 * you can do whatever you *
 * want on it as long as   *
 * you keep this copyright *
 * notice included with    *
 * this software. Please   *
 * respect that by not     *
 * deleting 'LICENSE' or   *
 * this notice. Thank you! *
 *               ~1024x2   *
 ***************************/

// Thanks, Draco!
function loadJS (source, onready){
  var sc = document.createElement("script");
  sc.src = source;
  sc.type = "text/javascript";
  if (onready) sc.addEventListener("load", onready);
  document.head.appendChild(sc);
  return sc;
}

// Taken from https://github.com/substack/semver-compare, thanks substack!
function cmp (a, b) {
  var pa = a.split('.');
  var pb = b.split('.');
  for (var i = 0; i < 3; i++) {
    var na = Number(pa[i]);
    var nb = Number(pb[i]);
    if (na > nb) return 1;
    if (nb > na) return -1;
    if (!isNaN(na) && isNaN(nb)) return 1;
    if (isNaN(na) && !isNaN(nb)) return -1;
  }
  return 0;
}

console.group("[pkg93]");
var failed = false;
console.log("[pkg93] Injecting packages...");
try {
  var config = JSON.parse(localStorage[".pkg93/config.json"]);
} catch (err) {
  console.error("[pkg93] Couldn't load package information.");
  failed = true;
}
if (!failed) {
  console.log("[pkg93] Not implemented.")
}
console.log("[pkg93] Done!");
console.groupEnd();

le._apps.pkg93 = {
  exec: function() {
    const protected = ["3d","acid","acidbox","ansi","anthology","arena93","bananamp","base64","bytebeat","calc","castlegafa","catex","cd","clear","clearhist","clippy","code","contact","crazy","defrag","dmg","do a barrel roll","doctor","download","find","font","format","fullscreen","fx","gameoflife","glitch","global thermonuclear war","gravity","hampster","hello","help","hexed","history","hl3","hydra","ie6","iframe","img","info","js","key","killall","layer","lenna","lisa","ls","manifesto","marburg","messenger","mines","necronomicoin","pd","piskel","pkg93","pony","potato","progressquest","pwd","reboot","robby","rotate","shutdown","skifree","solitude","speech","starwars","superplayer","takethis","terminal","textarea","tree","trollbox","vega","virtualpc","vm","wat","whatif","whois","win","zkype"];
    const args = this.arg.arguments;
    const version = "v0.1.0";
    if (localStorage[".pkg93/config.json"] === undefined) {
      localStorage[".pkg93/config.json"] = '{"sources": ["http://codinggamerhd.com/main-repo"], "installed": [], "pkglist": []}';
    }
    if (localStorage[".pkg93/downloaded/"] === undefined) {
      localStorage[".pkg93/downloaded/"] = "";
    }
    localStorage[".pkg93/README.txt"] = "WARNING!\nThis folder contains important data about pkg93. Do not edit anything in here unless you want pkg93 to not work!\n\n~1024x2";
    var config = JSON.parse(localStorage[".pkg93/config.json"]);
    var request = new XMLHttpRequest();
    if (args.length === 0) {
      $log(`<b>pkg93 ${version}</b>
<b>Usage:</b> pkg93 [command]

<b><u>List of Commands</u></b>
<span style='color:#0f0'>pull</span>                      Updates package listing
<span style='color:#0f0'>get</span> <span style='color:#77f'>[package]</span>             Installs a package
<span style='color:#0f0'>rm</span> <span style='color:#77f'>[package]</span>              Uninstalls a package
<span style='color:#0f0'>add-repo</span> <span style='color:#77f'>[url]</span>            Adds a repository
<span style='color:#0f0'>rm-repo</span> <span style='color:#77f'>[id]</span>              Removes a repository
<span style='color:#0f0'>ls</span> <span style='color:#77f'>[pkgs|installed|repos]</span> Lists all packages, installed
                          packages or repositories.
<b><u>Color meanings</u></b>
<b><span style='color:#f0f'>Executing</span> <span style='color:#0f0'>OK</span> <span style='color:#f00'>Error</span> <span style='color:#ff0'>Warning</span></b>

<b><u>Examples</u></b>
pkg93 <span style='color:#0f0'>get</span> <span style='color:#77f'>gud</span>
pkg93 <span style='color:#0f0'>rm</span> <span style='color:#77f'>kebab</span>

<b><span style='color:#ff0'>WARN</span></b> Not all packages are safe. Treat packages like EXE files.`);
    } else if (args[0] == "pull") {
      var pkgs = [];
      config.pkglist = [];
      config.sources.forEach(function (source) {
        $log("<b><span style='color:#f0f'>GET</span></b>  " + source);
        request.open('GET', source + "/repo.json", false);
        try {
          request.send(null);
          var json = JSON.parse(request.responseText);
          json.packages.forEach(function(item) {
            $log("<b><span style='color:#0f0'>OK</span></b>   " + item + "@" + source);
            pkgs.push(item + "@" + source);
          });
          config.pkglist = config.pkglist.concat(pkgs);
        } catch (err) {
          $log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
        }
      });
    } else if (args[0] == "get") {
      if (args.length < 2) {
        $log("<b><span style='color:#f00'>ERR</span></b>  No package specified.");
      } else if (protected.includes(args[1])) {
        $log("<b><span style='color:#f00'>ERR</span></b>  You're trying to modify a pre-installed Windows93 app.\n      <b>Don't do that!</b>");
      } else {
        $log("<b><span style='color:#f0f'>SRCH</span></b> " + args[1]);
        var index = config.pkglist.findIndex(function(string) {
          return string.split("@")[0] == args[1];
        });
        if (index < 0) {
          $log("<b><span style='color:#f00'>ERR</span></b>  Not found.");
        } else {
          $log("<b><span style='color:#0f0'>OK</span></b>   Found!");
          var pkgname = config.pkglist[index].split("@")[0];
          var pkgsource = config.pkglist[index].split("@")[1];
          request.open('GET', pkgsource + "/" + pkgname + "/package.json", false);
          try {
            request.send(null);
            var json = JSON.parse(request.responseText);
            request.open('GET', pkgsource + "/" + pkgname + "/" + json.inject, false);
            request.send(null);
            localStorage[".pkg93/downloaded/" + pkgname + ".js"] = request.responseText;
            eval(request.responseText);
            $log("<b><span style='color:#0f0'>OK</span></b>   Injected package!");
            config.installed.push(pkgname);
          } catch (err) {
            $log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
          }
        }
      }
    } else if (args[0] == "rm") {
      if (args.length < 2) {
        $log("<b><span style='color:#f00'>ERR</span></b>  No package specified.");
      } else if (protected.includes(args[1])) {
        $log("<b><span style='color:#f00'>ERR</span></b>  You're trying to modify a pre-installed Windows93 app.\n      <b>Don't do that!</b>");
      } else if (!!localStorage[".pkg93/downloaded/un" + args[1] + ".js"]) {
        eval(localStorage[".pkg93/downloaded/un" + args[1] + ".js"]); // Typing eval makes me feel dirty.
      } else {
        var index = config.installed.indexOf(args[1]);
        if (index < 0) {
          $log("<b><span style='color:#f00'>ERR</span></b>  Not found.");
        } else {
          try {
            if (le._apps[config.installed[index]] === null) {
              $log("<b><span style='color:#f00'>ERR</span></b>  Already removed.");
            } else {
              le._apps[config.installed[index]] = null;
              $log("<b><span style='color:#0f0'>OK</span></b>   Removed!");
            }
          } catch (err) {
            $log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
          }
        }
      }
    } else {
      $log("<b><span style='color:#f00'>ERR</span></b>  Invalid command. Type \"pkg93\" without any arguments for help.");
    }
    localStorage[".pkg93/config.json"] = JSON.stringify(config);
  },
  icon: "/c/sys/skins/w93/install.png",
  terminal: true,
  hascli: true
};