aboutsummaryrefslogtreecommitdiffstats
path: root/pkg93.js
blob: 55a377cf81d598ae11878aa0c4b7148aa1186a0f (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
async function wrap(f) {
  var originalPrompt = this.cli.prompt.innerHTML;
  var originalOnenter = this.cli.onenter;
  try  {
    this.cli.prompt.innerHTML = "";
    this.cli.onenter = () => false;
    var lastLog = $log("");
    await f({
      log: (...args) => {
        var newLog = $log(...args);
        newLog.parentElement.insertBefore(newLog, lastLog.nextSibling);
        lastLog = newLog;
        return newLog;
      },
      arg: this.arg
    });
  } finally {
    this.cli.prompt.innerHTML = originalPrompt;
    this.cli.onenter = originalOnenter;
  }
}

console.group("[pkg93]");
console.log("[pkg93] Injecting packages...");
try {
  if (localStorage[".pkg93/config.json"] === undefined) {
    console.log("[pkg93] You seem new. Creating config...");
    localStorage[".pkg93/config.json"] = `{"repos": ["//codinggamerhd.com/main-repo"], "installed": [], "pkglist": []}`;
  }
  var config = JSON.parse(localStorage[".pkg93/config.json"]);
  for (let pkg of config.installed) {
    eval(localStorage[".pkg93/packages/" + pkg + ".js"]);
  }
} catch (err) {
  console.error("[pkg93] Couldn't load package information.");
}
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 {
      return JSON.parse(localStorage[".pkg93/config.json"]);
    } catch (err) {
      return err;
    }
  },
  pull: async function(cli) {
    cli = cli || {log: (i) => {$log(i);}};
    var config = pkg93.getConfig();
    config.pkglist = [];
    for (let source of config.repos) {
      cli.log("<b><span style='color:#f0f'>GET</span></b>  " + 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);
      };
      return new Promise((res, rej) => {
        xhr.onerror = () => {
          cli.log("<b><span style='color:#f00'>ERR</span></b>  Fatal error while retriving package.json.");
          rej()
        };
        xhr.onload = () => {
          var json = JSON.parse(xhr.responseText);
          cli.log("<b><span style='color:#0f0'>NAME</span></b> " + json.name);
          cli.log("<b><span style='color:#0f0'>MSG</span></b>  " + json.msg);
          for (let item of json.packages) {
            try {
              cli.log("<b><span style='color:#0f0'>OK</span></b>   " + item + "@" + source);
              config.pkglist.push(item + "@" + source);
            } catch (err) {
              cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
            }
          }
          res()
        };
        xhr.send();
      })
    }
    localStorage[".pkg93/config.json"] = JSON.stringify(config);
  },
  get: async function(pkg, cli) {
    cli = cli || {log: (i) => {$log(i);}};
    var config = pkg93.getConfig();
    cli.log("<b><span style='color:#f0f'>SRCH</span></b> " + pkg);
    var index = config.pkglist.findIndex(function(string) {
      return string.split("@")[0] == pkg;
    });
    if (index < 0) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  Not found.");
      return false;
    } else {
      cli.log("<b><span style='color:#0f0'>OK</span></b>   Found!");
      var pkgname = config.pkglist[index].split("@")[0];
      var pkgsource = config.pkglist[index].split("@")[1];
      cli.log("<b><span style='color:#f0f'>GET</span></b>  " + 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
        }
      };
      return new Promise((res, rej) => {
        xhr.onerror = () => {
          cli.log("<b><span style='color:#f00'>ERR</span></b>  Fatal error while retriving package.json.");
          rej();
        };
        xhr.onload = () => {
          try {
            cli.log("<b><span style='color:#0f0'>DONE</span></b> " + pkgsource + "/" + pkgname + "/package.json");
            var json = JSON.parse(xhr.responseText);
            localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json);
            cli.log("<b><span style='color:#f0f'>GET</span></b>  " + 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("<b><span style='color:#f00'>ERR</span></b>  Fatal error while retriving " + pkgsource + "/" + pkgname + "/" + json.inject);
              rej();
            };
            xhr2.onload = async () => {
              try {
                cli.log("<b><span style='color:#0f0'>DONE</span></b> " + 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("<b><span style='color:#0f0'>OK</span></b>   Injected package!");
                if (!config.installed.includes(pkgname)) {
                  config.installed.push(pkgname);
                }
                localStorage[".pkg93/config.json"] = JSON.stringify(config);
                res();
                // can i go home now
              } catch (err) {
                cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
                rej(err);
              }
            };
            xhr2.send();
          } catch (err) {
            cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
            rej(err);
          }
        };
        try {
          xhr.send();
        } catch (err) {
          cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
          rej(err);
        }
      })
    }
  },
  rm: function(pkg, cli) {
    cli = cli || {log: (i) => {$log(i);}};
    var config = pkg93.getConfig();
    var index = config.installed.indexOf(pkg);
    if (index < 0) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  Not found.");
      return false;
    } else if (localStorage[".pkg93/packages/" + pkg + ".rm.js"]) {
      eval(localStorage[".pkg93/packages/" + pkg + ".rm.js"]); // Typing eval makes me feel dirty.
      delete le._apps[config.installed[index]];
      delete localStorage[".pkg93/packages/" + pkg + ".rm.js"];
      delete localStorage[".pkg93/packages/" + pkg + ".js"];
      delete localStorage[".pkg93/packages/" + pkg + ".json"];
      config.installed.splice(index, 1);
      cli.log("<b><span style='color:#0f0'>OK</span></b>   Removed!");
    } else {
      try {
        if (le._apps[config.installed[index]] === null) {
          cli.log("<b><span style='color:#f00'>ERR</span></b>  Already removed.");
          return false;
        } else {
          delete le._apps[config.installed[index]];
          delete localStorage[".pkg93/packages/" + config.installed[index] + ".js"];
          delete localStorage[".pkg93/packages/" + config.installed[index] + ".json"];
          config.installed.splice(index, 1);
          cli.log("<b><span style='color:#0f0'>OK</span></b>   Removed!");
        }
        localStorage[".pkg93/config.json"] = JSON.stringify(config);
        return true;
      } catch (err) {
        cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
        return false;
      }
    }
  },
  pkgInfo: async function(pkg, onlineOnly) {
    var config = pkg93.getConfig();
    try {
      if (localStorage[".pkg93/packages/" + pkg + ".json"] && !onlineOnly) {
        return JSON.parse(localStorage[".pkg93/packages/" + pkg + ".json"]);
      } else {
        var index = config.pkglist.findIndex(function(string) {
          return string.split("@")[0] == pkg;
        });
        if (index < 0) {
          return false;
        } else {
          var pkgname = config.pkglist[index].split("@")[0];
          var pkgsource = config.pkglist[index].split("@")[1];
          var json = await (await (fetch(pkgsource + "/" + pkgname + "/package.json"))).json();
          localStorage[".pkg93/packages/" + pkgname + ".json"] = JSON.stringify(json); // save it for later
          return json;
        }
      }
    } catch (err) {
      console.error("[pkg93] " + err.stack);
      return err;
    }
  },
  shutUp: {
    log: ()=>{},
    arg: {arguments:[]}
  },
  version: "v2.0.0"
};

async function _pkg93execdonotcallplsusetheapi(cli) {
  pkg93.version = "v2.0.0";
  var 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", "peng"];
  var args = cli.arg.arguments;
  var help = `<b>pkg93 ${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">info</span> <span style="color:#77f">[pkg]</span>                Gets information on a package
<span style="color:#0f0">ls</span> <span style="color:#77f">[pkgs|installed|repos]</span> Lists 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> <span style="color:#00f">Info</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>

If you find my software useful, consider donating <a style="color: #00f;" href="http://codinggamerhd.com/donate.html">here</a>.
`;
  if (localStorage[".pkg93/config.json"] === undefined) {
    localStorage[".pkg93/config.json"] = "{\"repos\": [\"//codinggamerhd.com/main-repo\"], \"installed\": [], \"pkglist\": []}";
  }
  if (localStorage[".pkg93/packages/"] === undefined) {
    localStorage[".pkg93/packages/"] = "";
  }
  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 = pkg93.getConfig();
  if (args.length === 0) {
    cli.log(help);
  } else if (args[0] == "pull") {
    await pkg93.pull(cli);
  } else if (args[0] == "get") {
    if (args.length < 2) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  No package specified.");
    } else if (protected.includes(args[1])) {
      cli.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 {
      await pkg93.get(args[1], cli);
    }
  } else if (args[0] == "rm") {
    if (args.length < 2) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  No package specified.");
    } else if (protected.includes(args[1])) {
      cli.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 {
      pkg93.rm(args[1], cli);
    }
  } else if (args[0] == "add-repo") {
    if (args.length < 2) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  No repository specified.");
    } else {
      try {
        config.repos.push(args[1]); // well, that was easy
        localStorage[".pkg93/config.json"] = JSON.stringify(config);
        cli.log("<b><span style='color:#0f0'>OK</span></b>   Done!\n     Run \"pkg93 pull\" to update the package listing.");
      } catch (err) {
        cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
      }
    }
  } else if (args[0] == "rm-repo") {
    if (args.length < 2) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  No repository specified.");
    } else {
      try {
        config.repos.splice(parseInt(args[1]), 1);
        localStorage[".pkg93/config.json"] = JSON.stringify(config);
        cli.log("<b><span style='color:#0f0'>OK</span></b>   Done!\n     Run \"pkg93 pull\" to update the package listing.");
      } catch (err) {
        cli.log("<b><span style='color:#f00'>ERR</span></b>  " + err.message);
      }
    }
  } else if (args[0] == "ls") {
    if (args[1] == "pkgs") {
      cli.log(config.pkglist.join("\n"));
    } else if (args[1] == "installed") {
      cli.log(config.installed.join("\n"));
    } else if (args[1] == "repos") {
      var lerepos = "";
      for (let index in config.repos) {
        lerepos += "[" + index + "] " + config.repos[index] + "<br>";
      }
      cli.log(lerepos);
    } else {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  You must select either pkgs, installed, or repos.");
    }
  } else if (args[0] == "info") {
    if (args.length > 2) {
      cli.log("<b><span style='color:#f00'>ERR</span></b>  No package specified.");
    } else {
      var pkgInfo = await pkg93.pkgInfo(args[1]);
      if (!pkgInfo) {
        cli.log("<b><span style='color:#f00'>ERR</span></b>  Either the package doesn't exist, or an error occoured.");
      } else {
        var depends = pkgInfo.dependencies ? pkgInfo.dependencies.join(" , ") : "<i><span style='color:#444'>None!</span></i>";
        var description = pkgInfo.description ? pkgInfo.description : "<i><span style='color:#444'>None!</span></i>";
        console.log(pkgInfo);
        cli.log(`<b><u>${pkgInfo.name}</u></b>
Description: ${description}
Dependencies: ${depends}`);
      }
    }
  } else if (args[0] == "help") {
    cli.log(help);
  } else if (args[0] == "wtf") {
    // for teh lulz
    new Audio("/c/sys/sounds/QUACK.ogg").play();
    var wtf = ["mudkipz", "pkg93", "memes", "linux", "javascript", "git", "cpu", "windows93", "discord", "kirb", "apt93", "delays", /* those last 2 go well together */ "trash", "kernel panic", "bash", "package manager", "recusion"];
    cli.log("<b><span style='color:#0f0'>WTF?</span></b> " + wtf[Math.floor(Math.random() * wtf.length)] + " + " + wtf[Math.floor(Math.random() * wtf.length)] + " = " + wtf[Math.floor(Math.random() * wtf.length)]);
  } else {
    cli.log("<b><span style='color:#f00'>ERR</span></b>  Invalid command. Type \"pkg93\" without any arguments for help.");
  }
}

le._apps.pkg93 = {
  exec: function() { wrap.call(this, _pkg93execdonotcallplsusetheapi); },
  icon: "//cdn.rawgit.com/1024x2/pkg93/70039c02/pkg.png",
  terminal: true,
  hascli: true,
  categories: "Network;Utility;Settings;PackageManager"
};