Fixed modManager add and remove functions

This commit is contained in:
Danielv123 committed 2018-02-22 19:13:47 +01:00
1 parent 64a29c004c
commit 29bffa472b
1 file changed
+80 -3
+80 -3
View File
@@ -39,7 +39,7 @@ module.exports.listMods = function(instance){
token: config.token, token: config.token,
require_ownership: false require_ownership: false
}).then(token => { }).then(token => {
console.log("Currently installed:"); console.log("* Currently installed:");
factorioAPI.readModZips().then(list => { factorioAPI.readModZips().then(list => {
console.log(asTable(list.map(mod => { console.log(asTable(list.map(mod => {
return { return {
@@ -54,7 +54,7 @@ module.exports.listMods = function(instance){
// Had issues earlier due to bug in node-factorio-api, see https://github.com/Danacus/node-factorio-api/issues/2 // Had issues earlier due to bug in node-factorio-api, see https://github.com/Danacus/node-factorio-api/issues/2
// bug is resolved, thanks to @Danacus and @brotherpot // bug is resolved, thanks to @Danacus and @brotherpot
factorioAPI.getModsFromSaves().then(list => { factorioAPI.getModsFromSaves().then(list => {
console.log("Mods used in save:"); console.log("* Mods used in save:");
console.log(asTable(list[0].mods)) console.log(asTable(list[0].mods))
}); });
}).catch(reason => { }).catch(reason => {
@@ -68,7 +68,7 @@ module.exports.listMods = function(instance){
token: config.token, token: config.token,
require_ownership: false require_ownership: false
}).then(() => { }).then(() => {
console.log("Mods shared by all instances:") console.log("* Mods shared by all instances:")
factorioAPI.getInstalledMods().then(mods => { factorioAPI.getInstalledMods().then(mods => {
console.log(asTable(mods)); console.log(asTable(mods));
}); });
@@ -110,6 +110,83 @@ module.exports.findMods = function(searchTerm){
}); });
} }
module.exports.addMod = function(term, instance){
if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/";
} else {
var modDirectory = "instances/"+instance+"/instanceMods/";
}
if(term && term.length > 0){
if(term.includes("mods.factorio.com/mods/")){ // https://mods.factorio.com/mods/Danielv123/ChooChoo
// treat it as a link download
factorioAPI.init(false, modDirectory, "", "0.16.15");
factorioAPI.authenticate({
username: config.username,
token: config.token,
require_ownership: false
}).then(() => {
factorioAPI.searchMods(term.split("/")[term.split("/").length-1]).then(body => {
console.log("Im gonna download: "+body[0].name);
factorioAPI.downloadMod(body[0]).then(()=>{
console.log("Mod downloaded");
});
});
});
} else if(term.includes("github.com")){ // https://github.com/Danielv123/factorioClusterioMod
// 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 res = syncRequest('GET', apiURL, {"headers":{"User-Agent":"Fuck you for requiring user agents!"}});
let url = JSON.parse(res.getBody())[0].assets[0].browser_download_url;
let name = JSON.parse(res.getBody())[0].assets[0].name;
if(url) {
console.log("Downloading " + url);
let file = fs.createWriteStream(modDirectory + name);
https.get(url, function(response) {
response.pipe(file);
console.log("Downloaded "+name);
});
}
} else {
// treat it as a search and download the top result
factorioAPI.init(false, modDirectory, "", "0.16.15");
factorioAPI.authenticate({
username: config.username,
token: config.token,
require_ownership: false
}).then(() => {
factorioAPI.searchMods(term).then(body => {
console.log("Im gonna download: "+body[0].name);
factorioAPI.downloadMod(body[0]).then(()=>{
console.log("Mod downloaded");
});
});
});
}
}
}
module.exports.removeMod = function removeMod(term, instance, force = false){
if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/";
} else {
var modDirectory = "instances/"+instance+"/instanceMods/";
}
if(term && typeof term == "string" && term.length > 0){
let mods = fs.readdirSync(modDirectory);
let modsMatchingFilter = mods.filter(mod => mod.toLowerCase().includes(term.toLowerCase()));
if(modsMatchingFilter.length < 1){
console.log("No mods found matching search");
} else if(modsMatchingFilter.length == 1){
console.log("Deleting "+modsMatchingFilter[0]);
fs.unlinkSync(modDirectory+modsMatchingFilter[0]);
console.log("Deleted.");
} else if(modsMatchingFilter.length > 1){
console.log("Found multiple mods, not doing anything");
modsMatchingFilter.forEach(mod => console.log(mod));
}
}
}
// '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