mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
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:
1 parent
5f33cf4d3b
commit
a783bd9651
3 files changed
+65
-61
No files matched your search
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-52
@@ -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");
|
Lists mods for an instance as a table. Warning, handles printing internally.
|
||||||
const instances = require("./../getInstances.js")(config).sync();
|
@param {Instance} instance - Instance object, see client.js
|
||||||
return {
|
|
||||||
/**
|
|
||||||
Lists mods for an instance as a table. Warning, handles printing internally.
|
|
||||||
@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
|
|
||||||
the results to console as a neat looking table
|
|
||||||
|
|
||||||
@param {string} searchTerm What to search for
|
/**
|
||||||
*/
|
Searches mods.factorio.com for mods matching a search term and prints
|
||||||
findMods: function(searchTerm){
|
the results to console as a neat looking table
|
||||||
|
|
||||||
|
@param {string} searchTerm What to search for
|
||||||
|
*/
|
||||||
|
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({
|
||||||
@@ -127,20 +124,21 @@ module.exports = function(config){
|
|||||||
// console.log(result);
|
// console.log(result);
|
||||||
});
|
});
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
if(e.contains("Insufficient information")) console.log("We are missing some parameters required for logging in! Make sure your factorio username and token are specified in config.json.");
|
if (e.contains("Insufficient information")) console.log("We are missing some parameters required for logging in! Make sure your factorio username and token are specified in config.json.");
|
||||||
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";
|
||||||
} else {
|
} else {
|
||||||
var modDirectory = instance.path("instanceMods");
|
var modDirectory = instance.path("instanceMods");
|
||||||
}
|
}
|
||||||
if(term && term.length > 0){
|
if (term && term.length > 0) {
|
||||||
if(term.includes("mods.factorio.com/mods/")){ // https://mods.factorio.com/mods/Danielv123/ChooChoo
|
if (term.includes("mods.factorio.com/mods/")) { // https://mods.factorio.com/mods/Danielv123/ChooChoo
|
||||||
// treat it as a link download
|
// treat it as a link download
|
||||||
factorioAPI.init(false, modDirectory, "", config.factorio_version);
|
factorioAPI.init(false, modDirectory, "", config.factorio_version);
|
||||||
factorioAPI.authenticate({
|
factorioAPI.authenticate({
|
||||||
@@ -156,14 +154,14 @@ module.exports = function(config){
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} else if(term.includes("github.com")){ // https://github.com/Danielv123/factorioClusterioMod
|
} else if (term.includes("github.com")) { // https://github.com/Danielv123/factorioClusterioMod
|
||||||
// treat it as a github releases download
|
// treat it as a github releases download
|
||||||
let apiURL = "https://api.github.com/repos/" + term.split("/")[3] + "/" + term.split("/")[4] + "/releases"; // https://api.github.com/repos/Danielv123/factorioClusterioMod/releases
|
let apiURL = "https://api.github.com/repos/" + term.split("/")[3] + "/" + term.split("/")[4] + "/releases"; // https://api.github.com/repos/Danielv123/factorioClusterioMod/releases
|
||||||
|
|
||||||
let res = syncRequest('GET', apiURL, {"headers":{"User-Agent":"factorioClusterio"}});
|
let res = syncRequest('GET', apiURL, {"headers":{"User-Agent":"factorioClusterio"}});
|
||||||
let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url;
|
let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url;
|
||||||
let name = JSON.parse(res.getBody())[0].assets[0].name;
|
let name = JSON.parse(res.getBody())[0].assets[0].name;
|
||||||
if(url) {
|
if (url) {
|
||||||
console.log("Downloading " + url);
|
console.log("Downloading " + url);
|
||||||
let file = fs.createWriteStream(path.join(modDirectory, name));
|
let file = fs.createWriteStream(path.join(modDirectory, name));
|
||||||
let bytesDownloaded = 0;
|
let bytesDownloaded = 0;
|
||||||
@@ -207,29 +205,30 @@ 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";
|
||||||
} else {
|
} else {
|
||||||
var modDirectory = instance.path("instanceMods");
|
var modDirectory = instance.path("instanceMods");
|
||||||
}
|
}
|
||||||
if(term && typeof term == "string" && term.length > 0){
|
if (term && typeof term == "string" && term.length > 0) {
|
||||||
let mods = fs.readdirSync(modDirectory);
|
let mods = fs.readdirSync(modDirectory);
|
||||||
let modsMatchingFilter = mods.filter(mod => mod.toLowerCase().includes(term.toLowerCase()));
|
let modsMatchingFilter = mods.filter(mod => mod.toLowerCase().includes(term.toLowerCase()));
|
||||||
if(modsMatchingFilter.length < 1){
|
if (modsMatchingFilter.length < 1) {
|
||||||
console.log("No mods found matching search");
|
console.log("No mods found matching search");
|
||||||
} else if(modsMatchingFilter.length == 1){
|
} else if (modsMatchingFilter.length == 1) {
|
||||||
console.log("Deleting "+modsMatchingFilter[0]);
|
console.log("Deleting "+modsMatchingFilter[0]);
|
||||||
fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[0]));
|
fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[0]));
|
||||||
console.log("Deleted.");
|
console.log("Deleted.");
|
||||||
} else if(modsMatchingFilter.length > 1){
|
} else if (modsMatchingFilter.length > 1) {
|
||||||
console.log("Found multiple mods");
|
console.log("Found multiple mods");
|
||||||
modsMatchingFilter.forEach(mod => console.log(mod));
|
modsMatchingFilter.forEach(mod => console.log(mod));
|
||||||
if(query("Delete all mods matching filter?")){
|
if (query("Delete all mods matching filter?")) {
|
||||||
let modsToDelete = modsMatchingFilter.length;
|
let modsToDelete = modsMatchingFilter.length;
|
||||||
for(let i = 0; i < modsToDelete; i++){
|
for (let i = 0; i < modsToDelete; i++) {
|
||||||
fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[i]));
|
fs.unlinkSync(path.join(modDirectory, modsMatchingFilter[i]));
|
||||||
console.log("Deleted "+modsMatchingFilter[i]);
|
console.log("Deleted "+modsMatchingFilter[i]);
|
||||||
}
|
}
|
||||||
@@ -240,23 +239,23 @@ 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'
|
||||||
/**
|
/**
|
||||||
Download mod from github repo by looking for the first binary download in
|
Download mod from github repo by looking for the first binary download in
|
||||||
the latest release, saving it to /sharedMods
|
the latest release, saving it to /sharedMods
|
||||||
|
|
||||||
@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"}});
|
||||||
let downloadUrl = JSON.parse(res.getBody())[0].assets[0].browser_download_url;
|
let downloadUrl = JSON.parse(res.getBody())[0].assets[0].browser_download_url;
|
||||||
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()))
|
||||||
if(downloadUrl) {
|
if (downloadUrl) {
|
||||||
console.log(downloadUrl);
|
console.log(downloadUrl);
|
||||||
let file = fs.createWriteStream("sharedMods/"+name);
|
let file = fs.createWriteStream("sharedMods/"+name);
|
||||||
https.get(downloadUrl, function(response) {
|
https.get(downloadUrl, function(response) {
|
||||||
@@ -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,
|
||||||
|
}
|
||||||
@@ -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) => {
|
||||||
|
|||||||
Reference in new issue
Block a user