From 8e5a02fbe012c9fa5fa34961857f73936c6491a9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 20 Apr 2018 18:19:57 +0100 Subject: add descriptions and api docs --- README.md | 70 ++++++++++++++++++++++++++++++++++ pkg93.js | 126 ++++++++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 151 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index b95514f..c4a50f0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # pkg93 A package manager for Windows 93! +## Table of Contents +- [Installation](#installation) +- [Adding my package to the main repoistory](#adding-my-package-to-the-main-repoistory) +- [Making a Repository](#making-a-repository) +- [Making a Package](#making-a-package) +- [API](#api) + ## Installation Install the userscript in this repository named "inject.user.js". @@ -48,6 +55,7 @@ Firstly, you want to make a new folder called the name of the package. Then, you want to make a file called package.json in the folder. In it, there should be 4 keys. - `name`: **Must be the same as the folder name and command name!** (unless you've provided a uninstaller) +- `description` A description of your package. - `inject`: It should be the name of the injection script. - `uninstall`: Optional, It should be the name of the uninstaller script, if it doesn't exist pkg93 will simply delete the command for you. - `dependencies`: Optional, Packages this package depends on. These will be automatically installed. @@ -55,6 +63,7 @@ Here's a example: ```json { "name": "examplepkg", + "description": "my kewl pakeg!!11", "inject": "installer.js", "uninstall": "optionaluninstaller.js", "dependencies": [ @@ -70,3 +79,64 @@ examplepkg ├── installer.js └── optionaluninstaller.js ``` + +## API +### `pkg93.getConfig()` +Gets the configuration, or returns `false` if something went wrong. +Example: +```js +var config = pkg93.getConfig(); +if (config == false) { + alert("Something went wrong..."); +} else { + alert("You have " + config.pkglist.length + " packages available!"); +} +``` + +#### Configuration Format: +The configuration is a object with the following keys: +- `repos` All added repos. +- `installed` All installed packages. +- `pkglist` - All available packages. + +### `pkg93.pull()` +Refreshes the list of packages available. +Example: +```js +alert("You previously had " + config.pkglist.length + " packages available."); +pkg93.pull(); +alert("Now you have " + config.pkglist.length + " packages available!"); +``` + +### `pkg93.get(package)` +Tries to install `package`, then returns `true` if the package was installed or `false` if the package couldn't be installed. +```js +succeded = pkg93.get("wget93"); +if (succeded) { + alert("Installed wget93!"); +} else { + alert("Something went wrong..."); +} +``` + +### `pkg93.rm(package)` +Tries to remove `package`, then returns `true` if the package was removed or `false` if the package couldn't be removed. +```js +succeded = pkg93.rm("wget93"); +if (succeded) { + alert("Removed wget93!"); +} else { + alert("Something went wrong..."); +} +``` + +### `pkg93.pkginfo(package)` +Returns the package.json of `package` or `false` if it failed. +```js +package = pkg93.pkgInfo("wget93"); +if (package == false) { + alert("Something went wrong..."); +} else { + alert("wget93's description is: " + package.description); +} +``` diff --git a/pkg93.js b/pkg93.js index 3152c43..44058f6 100644 --- a/pkg93.js +++ b/pkg93.js @@ -1,19 +1,3 @@ -/*************************** - * 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 * - ***************************/ - console.group("[pkg93]"); console.log("[pkg93] Injecting packages..."); try { @@ -40,8 +24,8 @@ var pkg93 = { } }, pull: function() { - var config = pkg93.getConfig(); - var request = new XMLHttpRequest(); + var config = pkg93.getConfig(), + request = new XMLHttpRequest(); $log("WARN Windows93 may lag while getting packages.\n This is a normal thing."); config.pkglist = []; config.repos.forEach(function (source) { @@ -68,8 +52,8 @@ var pkg93 = { localStorage[".pkg93/config.json"] = JSON.stringify(config); }, get: function(pkg) { - var config = pkg93.getConfig(); - var request = new XMLHttpRequest(); + var config = pkg93.getConfig(), + request = new XMLHttpRequest(); $log("SRCH " + pkg); var index = config.pkglist.findIndex(function(string) { return string.split("@")[0] == pkg; @@ -119,9 +103,9 @@ var pkg93 = { } }, rm: function(pkg) { - var config = pkg93.getConfig(); - var request = new XMLHttpRequest(); - var index = config.installed.indexOf(pkg); + var config = pkg93.getConfig(), + request = new XMLHttpRequest(), + index = config.installed.indexOf(pkg); if (index < 0) { $log("ERR Not found."); return false; @@ -152,6 +136,33 @@ var pkg93 = { return false; } } + }, + pkgInfo: function (pkg, onlineOnly) { + var config = pkg93.getConfig(), + request = new XMLHttpRequest(); + 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]; + request.open('GET', pkgsource + "/" + pkgname + "/package.json", false); + request.send(null); + var json = JSON.parse(request.responseText); + localStorage[".pkg93/packages/" + pkgname + ".json"] = request.responseText; // save it for later + return json; + } + } + } catch (err) { + console.error("[pkg93] " + err.stack); + return false; + } } } @@ -164,19 +175,21 @@ le._apps.pkg93 = { Usage: pkg93 [command] List of Commands -pull Updates package listing -get [package] Installs a package -rm [package] Uninstalls a package -add-repo [url] Adds a repository -rm-repo [id] Removes a repository -ls [pkgs|installed|repos] Lists all packages, installed +pull Updates package listing +get [package] Installs a package +rm [package] Uninstalls a package +add-repo [url] Adds a repository +rm-repo [id] Removes a repository +info [pkg] Gets information on a package +ls [pkgs|installed|repos] Lists packages, installed packages or repositories. Color meanings -Executing OK Error Warning +Executing OK Error Warning Info Examples -pkg93 get gud -pkg93 rm kebab`; +pkg93 get gud +pkg93 rm kebab +`; if (localStorage[".pkg93/config.json"] === undefined) { localStorage[".pkg93/config.json"] = '{"repos": ["http://codinggamerhd.com/main-repo"], "installed": [], "pkglist": []}'; } @@ -206,20 +219,28 @@ pkg93 rm kebab`; pkg93.rm(args[1]); } } else if (args[0] == "add-repo") { - try { - config.repos.push(args[1]); // well, that was easy - localStorage[".pkg93/config.json"] = JSON.stringify(config); - $log("OK Done!\n Run \"pkg93 pull\" to update the package listing."); - } catch (err) { - $log("ERR " + err.message); + if (args.length < 2) { + $log("ERR No repository specified."); + } else { + try { + config.repos.push(args[1]); // well, that was easy + localStorage[".pkg93/config.json"] = JSON.stringify(config); + $log("OK Done!\n Run \"pkg93 pull\" to update the package listing."); + } catch (err) { + $log("ERR " + err.message); + } } } else if (args[0] == "rm-repo") { - try { - config.repos.splice(parseInt(args[1]), 1); - localStorage[".pkg93/config.json"] = JSON.stringify(config); - $log("OK Done!\n Run \"pkg93 pull\" to update the package listing."); - } catch (err) { - $log("ERR " + err.message); + if (args.length < 2) { + $log("ERR No repository specified."); + } else { + try { + config.repos.splice(parseInt(args[1]), 1); + localStorage[".pkg93/config.json"] = JSON.stringify(config); + $log("OK Done!\n Run \"pkg93 pull\" to update the package listing."); + } catch (err) { + $log("ERR " + err.message); + } } } else if (args[0] == "ls") { if (args[1] == "pkgs") { @@ -235,6 +256,21 @@ pkg93 rm kebab`; } else { $log("ERR You must select either pkgs, installed, or repos."); } + } else if (args[0] == "info") { + if (args.length > 2) { + $log("ERR No package specified."); + } else { + var pkgInfo = JSON.parse(pkg93.pkgInfo(args[1])); + if (!pkgInfo) { + $log("ERR Either the package doesn't exist, or an error occoured.") + } else { + depends = pkgInfo.dependencies ? pkgInfo.dependencies.join(" , ") : "None!"; + console.log(pkgInfo); + $log(`${pkgInfo.name} +Description: ${pkgInfo.description} +Dependencies: ${depends}`); + } + } } else if (args[0] == "help") { $log(help); } else if (args[0] == "wtf") { @@ -249,5 +285,5 @@ pkg93 rm kebab`; icon: "//cdn.rawgit.com/1024x2/pkg93/70039c02/pkg.png", terminal: true, hascli: true, - categories: "Network;" + categories: "Network;Utility;Settings;PackageManager" }; -- cgit v1.2.3