Refactor out config injection in modManager

Change config in modManager to be passed to each individual functions
instead of requiring it be present at load time.
This commit is contained in:
Hornwitser committed 2019-09-16 16:04:08 +02:00
1 parent 5f33cf4d3b
commit a783bd9651
3 files changed
+32 -28

No files matched your search

+5 -7
View File
@@ -21,6 +21,7 @@ const objectOps = require("lib/objectOps.js");
const fileOps = require("lib/fileOps"); const fileOps = require("lib/fileOps");
const stringUtils = require("lib/stringUtils.js"); const stringUtils = require("lib/stringUtils.js");
const pluginManager = require("lib/manager/pluginManager"); const pluginManager = require("lib/manager/pluginManager");
const modManager = require("lib/manager/modManager");
const hashFile = require('lib/hash').hashFile; const hashFile = require('lib/hash').hashFile;
// Uhm... // Uhm...
@@ -245,18 +246,15 @@ async function manage(config, instance) {
if (instance !== undefined) { if (instance !== undefined) {
if(tool == "mods"){ if(tool == "mods"){
(async function(){try{ (async function(){try{
// do require down here to reduce master load time
const modManager = require("lib/manager/modManager.js")(config);
// allow managing mods // allow managing mods
if(action == "list"){ if(action == "list"){
console.log(await modManager.listMods(instance)); console.log(await modManager.listMods(config, instance));
} else if(action == "search"){ } else if(action == "search"){
console.log(await modManager.findMods(process.argv[6])); console.log(await modManager.findMods(config, process.argv[6]));
} else if(action == "add" || action == "download"){ } else if(action == "add" || action == "download"){
await modManager.addMod(process.argv[6], instance); await modManager.addMod(config, process.argv[6], instance);
} else if(action == "remove" || action == "rm" || action == "delete"){ } else if(action == "remove" || action == "rm" || action == "delete"){
await modManager.removeMod(process.argv[6], instance); await modManager.removeMod(config, process.argv[6], instance);
} else { } else {
usage(instance, tool); usage(instance, tool);
} }
+25 -19
View File
@@ -14,7 +14,6 @@ const query = require('cli-interact').getYesNo;
const path = require("path"); const path = require("path");
const fileOps = require("lib/fileOps"); const fileOps = require("lib/fileOps");
// const config = require("./../../config");
function ellipse(str, max){ function ellipse(str, max){
@@ -24,23 +23,20 @@ function ellipse(str, max){
function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}; function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]};
module.exports = function(config){
if(!config) config = require("./../../config");
const instances = require("./../getInstances.js")(config).sync();
return {
/** /**
Lists mods for an instance as a table. Warning, handles printing internally. Lists mods for an instance as a table. Warning, handles printing internally.
@param {Instance} instance - Instance object, see client.js @param {Instance} instance - Instance object, see client.js
@example @example
var modManager = require("modManager")(config); var modManager = require("modManager");
modManager.listMods(instance); modManager.listMods(config, instance);
// console.logs table of mods installed on instance // console.logs table of mods installed on instance
// if instance.name is shared: // if instance.name is shared:
// console.logs mods located in /sharedMods as a nice table // console.logs mods located in /sharedMods as a nice table
*/ */
listMods: function(instance){ async function listMods(config, instance) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const instances = require("./../getInstances.js")(config).sync();
if (instances.includes(instance.name)) { if (instances.includes(instance.name)) {
factorioAPI.init(false, instance.path("instanceMods"), instance.path("saves"), config.factorio_version); factorioAPI.init(false, instance.path("instanceMods"), instance.path("saves"), config.factorio_version);
factorioAPI.authenticate({ factorioAPI.authenticate({
@@ -96,14 +92,15 @@ module.exports = function(config){
console.log(JSON.parse(res.getBody())) console.log(JSON.parse(res.getBody()))
*/ */
}); });
}, }
/** /**
Searches mods.factorio.com for mods matching a search term and prints Searches mods.factorio.com for mods matching a search term and prints
the results to console as a neat looking table the results to console as a neat looking table
@param {string} searchTerm What to search for @param {string} searchTerm What to search for
*/ */
findMods: function(searchTerm){ async function findMods(config, searchTerm) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
factorioAPI.init(false); factorioAPI.init(false);
factorioAPI.authenticate({ factorioAPI.authenticate({
@@ -131,8 +128,9 @@ module.exports = function(config){
reject(new Error(e)); reject(new Error(e));
}); });
}); });
}, }
addMod: function(term, instance){
async function addMod(config, term, instance) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (instance.name == "shared") { if (instance.name == "shared") {
var modDirectory = "sharedMods"; var modDirectory = "sharedMods";
@@ -207,8 +205,9 @@ module.exports = function(config){
reject(new Error("Invalid search term")); reject(new Error("Invalid search term"));
} }
}); });
}, }
removeMod: function removeMod(term, instance, force = false){
async function removeMod(config, term, instance, force = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (instance.name == "shared") { if (instance.name == "shared") {
var modDirectory = "sharedMods"; var modDirectory = "sharedMods";
@@ -240,7 +239,7 @@ module.exports = function(config){
reject(new Error("Invalid search term")); reject(new Error("Invalid search term"));
} }
}); });
}, }
// 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases' // 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases'
/** /**
@@ -249,7 +248,7 @@ module.exports = function(config){
@param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases @param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases
*/ */
downloadFromGitURL: function(url){ function downloadFromGitURL(url) {
console.log("Downloading mod from "+url); console.log("Downloading mod from "+url);
// get JSON data about releases // get JSON data about releases
let res = syncRequest('GET', url, {"headers":{"User-Agent":"factorioClusterio"}}); let res = syncRequest('GET', url, {"headers":{"User-Agent":"factorioClusterio"}});
@@ -264,11 +263,10 @@ module.exports = function(config){
console.log("Downloaded "+name); console.log("Downloaded "+name);
}); });
} }
},
}
} }
/* /*
module.exports.findUpdates = function(){ function findUpdates() {
console.log("Downloading mods..."); console.log("Downloading mods...");
// get JSON data about releases // get JSON data about releases
let res = syncRequest('GET', 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases', {"headers":{"User-Agent":"factorioClusterio"}}); let res = syncRequest('GET', 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases', {"headers":{"User-Agent":"factorioClusterio"}});
@@ -276,3 +274,11 @@ module.exports.findUpdates = function(){
let name = JSON.parse(res.getBody())[0].assets[0].name; let name = JSON.parse(res.getBody())[0].assets[0].name;
console.log(JSON.parse(res.getBody())) console.log(JSON.parse(res.getBody()))
}*/ }*/
module.exports = {
listMods,
findMods,
addMod,
removeMod,
downloadFromGitURL,
}
+2 -2
View File
@@ -10,7 +10,7 @@ module.exports = class remoteCommands {
this.config = mergedConfig; this.config = mergedConfig;
this.socket = extras.socket; this.socket = extras.socket;
const pluginManager = require("lib/manager/pluginManager.js"); const pluginManager = require("lib/manager/pluginManager.js");
// const modManager = require("lib/manager/modManager.js")(this.config); // const modManager = require("lib/manager/modManager");
this.socket.on("hello", () => { this.socket.on("hello", () => {
this.socket.emit("registerServerManager"); this.socket.emit("registerServerManager");
}); });
@@ -19,7 +19,7 @@ module.exports = class remoteCommands {
}); });
this.socket.on("serverManagerGetMods", async (data, callback) => { this.socket.on("serverManagerGetMods", async (data, callback) => {
// TODO: Implement this function separately from listMods (which console.logs it) // TODO: Implement this function separately from listMods (which console.logs it)
// let mods = modManager.getMods() // let mods = modManager.getMods(this.config)
// callback(mods); // callback(mods);
}); });
this.socket.on("serverManagerGetPlugins", async (data, callback) => { this.socket.on("serverManagerGetPlugins", async (data, callback) => {