From 8abeb26dcef9046358a160389be5784267ef2564 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Vest=C3=B8l?= Date: Wed, 25 Jul 2018 01:14:43 +0200 Subject: [PATCH] Commandline tools to install/uninstall plugins from github --- client.js | 22 +++++++++++- lib/manager/pluginManager.js | 67 ++++++++++++++++++++++++++++++++++++ package.json | 5 +-- 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 lib/manager/pluginManager.js diff --git a/client.js b/client.js index 079e84e5..97df1cde 100644 --- a/client.js +++ b/client.js @@ -118,6 +118,8 @@ if (!command || command == "help" || command == "--help") { console.error("node client.js delete [instance]"); console.error("To download the latest version of the Clusterio lua mod, do"); console.error("node client.js manage shared mods download clusterio"); + console.error("To download a clusterio plugin, do"); + console.error("node client.js manage shared plugins install https://github.com/Danielv123/playerManager"); console.error("For more management options, do"); console.error("node client.js manage"); process.exit(1); @@ -173,8 +175,10 @@ if (!command || command == "help" || command == "--help") { console.log('node client.js manage '+instance+' '+tool+' ["list", "search", "add", "remove", "update"]'); } else if(tool && tool == "config") { console.log('node client.js manage '+instance+' '+tool+' ["list", "edit"]'); + } else if(tool && tool == "plugins") { + console.log(`node client.js manage ${instance} ${tool} ["list", "add", "remove"]`); } else { - console.log('node client.js manage '+(instance || '[instance, "shared"]') +' '+ (tool || '["mods", "config"]') + ' ...'); + console.log('node client.js manage '+(instance || '[instance, "shared"]') +' '+ (tool || '["mods", "config", "plugins"]') + ' ...'); } } const tool = process.argv[4] || ""; @@ -221,6 +225,22 @@ if (!command || command == "help" || command == "--help") { } else { usage(instance, tool); } + } else if(tool == "plugins"){ + (async function(){try{ + const pluginManager = require("./lib/manager/pluginManager.js")(config); + if(action == "list"){ + console.log(await pluginManager.listPlugins()) + } else if(action == "add" || action == "install" || action == "download"){ + let status = await pluginManager.addPlugin(process.argv[6]); + } else if(action == "remove" || action == "uninstall" || action == "delete"){ + let status = await pluginManager.removePlugin(process.argv[6]); + if(status && status.msg) console.log(status.msg); + } + process.exit(0); + }catch(e){ + console.error(e); + process.exit(1); + }})(); } else { usage(instance); } diff --git a/lib/manager/pluginManager.js b/lib/manager/pluginManager.js new file mode 100644 index 00000000..3eff4035 --- /dev/null +++ b/lib/manager/pluginManager.js @@ -0,0 +1,67 @@ +const fs = require("fs-extra"); +const path = require("path"); +const git = require("simple-git")("./sharedPlugins"); + + +module.exports = function(config){ + if(!config) config = require("./../../config"); + + return { + listPlugins: async function(){ + let plugins = await this.getPlugins(); + plugins.forEach(plug => { + console.log(`${plug.name}`.padEnd(22).substr(0,22)+` - ${plug.description}`.substr(0,80-22)); + }); + }, + getPlugins: async function(){ + let directories = await fs.readdir("./sharedPlugins"); + let returnArray = []; + for(let i in directories){ + let dir = directories[i]; + let pluginContents = await fs.readdir(path.join("./sharedPlugins", dir)); + // console.log(pluginContents) + if(pluginContents.includes("config.js") || pluginContents.includes("config.json")){ + try{ + let pluginConfig = require(path.join("./../../sharedPlugins", dir, "config")); + pluginConfig.pluginPath = path.join("./sharedPlugins", dir); + returnArray.push(pluginConfig); + }catch(e){ + console.log(e) + } + } + } + return returnArray; + }, + addPlugin: function(path){ + return new Promise((resolve, reject) => { + if(path.includes("git")){ // we are decently sure this is a git repo + git.clone(path, data => resolve(data)) + } + }); + }, + removePlugin: async function(name){ + let plugins = await this.getPlugins(); + for(let i in plugins){ + let plug = plugins[i]; + if(plug.name.includes(name)){ + // console.log(`Removing plugin ${plug.name}`); + await fs.remove(plug.pluginPath); + return { + ok:true, + msg:`Deleted plugin ${plug.name}` + } + } + } + return { + ok:false, + msg:`Plugin matching "${name}" not found`, + } + }, + enablePlugin: function(){ + + }, + disablePlugin: function(){ + + } + } +} diff --git a/package.json b/package.json index 82f22118..9e1bc980 100644 --- a/package.json +++ b/package.json @@ -61,12 +61,13 @@ "right-pad": "^1.0.1", "rmdir-sync": "^1.0.1", "sanitizer": "^0.1.3", + "simple-git": "^1.96.0", "single-line-log": "^1.1.2", + "snyk": "^1.88.0", "socket.io": "^2.0.4", "socket.io-client": "^2.0.4", "sync-request": "^4.0.1", - "underscore": "^1.8.3", - "snyk": "^1.88.0" + "underscore": "^1.8.3" }, "devDependencies": { "documentation": "^5.4.0",