Fixed modManager to work in 0.16

This commit is contained in:
Danielv123 committed 2018-02-18 19:50:35 +01:00
1 parent 9e63086d6d
commit 64a29c004c
2 files changed
+22 -105

No files matched your search

+12 -95
View File
@@ -15,6 +15,11 @@ const fileOps = require("./../fileOps.js");
const config = require("./../../config");
const instances = require("./../getInstances.js").sync();
function ellipse(str, max){
return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str;
}
/**
Lists mods for an instance as a table. Warning, handles printing internally.
@param {string} instance - Name of the instance or "shared"
@@ -29,7 +34,6 @@ modManager.listMods("shared");
module.exports.listMods = function(instance){
if(instance && instances.includes(instance)){
factorioAPI.init(false, 'instances/'+instance+'/instanceMods', 'instances/'+instance+'/saves', '0.16.15');
console.log("Authenticating to factorio.com...")
factorioAPI.authenticate({
username: config.username,
token: config.token,
@@ -47,20 +51,18 @@ module.exports.listMods = function(instance){
}
})));
});
// Deactivated 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
/*factorioAPI.getModsFromSaves().then(list => {
factorioAPI.getModsFromSaves().then(list => {
console.log("Mods used in save:");
console.log(JSON.stringify(list))
console.log(asTable(list[0].mods))
});*/
});
}).catch(reason => {
console.log("Failed to authenticate to factorio.com! Please check your connection and the username+token used for authentication in config.json");
});
} else {
// list shared mods in /sharedMods/
factorioAPI.init(false, 'sharedMods/', '', '0.16.15');
console.log("Authenticating to factorio.com...")
factorioAPI.authenticate({
username: config.username,
token: config.token,
@@ -80,87 +82,6 @@ module.exports.listMods = function(instance){
console.log(JSON.parse(res.getBody()))
*/
}
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(
{q:term.split("/")[term.split("/").length-1], order: 'top', page_size:1}
).then(body => {
console.log("Im gonna download: "+body.results[0].name);
factorioAPI.downloadMod(body.results[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(
{q:term, order: 'top', page_size:1}
).then(body => {
console.log("Im gonna download: "+body.results[0].name);
factorioAPI.downloadMod(body.results[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.includes(term));
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));
}
}
}
/**
Searches mods.factorio.com for mods matching a search term and prints
the results to console as a neat looking table
@@ -169,20 +90,16 @@ the results to console as a neat looking table
*/
module.exports.findMods = function(searchTerm){
factorioAPI.init(false);
console.log("Authenticating to factorio.com...")
factorioAPI.authenticate({
username: config.username,
token: config.token,
require_ownership: false
}).then(token => {
factorioAPI.searchMods(
{q: searchTerm, order: 'top', page_size: 20}
).then(body => {
let mods = body.results;
console.log(asTable(mods.map(mod => {
factorioAPI.searchMods(searchTerm).then(mods => {
console.log(asTable(mods.reverse().map(mod => {
return {
title:mod.title,
name:mod.name,
title:ellipse(mod.title, 50),
name:ellipse(mod.name, 30),
downloads:mod.downloads_count,
updated: moment(mod.updated_at).format("DD.MM.YY"),
owner:mod.owner,
+10 -10
View File
@@ -26,22 +26,22 @@
"ejs": "^2.5.7",
"express": "^4.16.2",
"express-fileupload": "0.0.6",
"follow-redirects": "^1.2.5",
"follow-redirects": "^1.4.1",
"fs-extra": "^1.0.0",
"getmac": "^1.2.1",
"getmac": "^1.4.0",
"hash-files": "^1.1.1",
"html5-validator": "^1.0.6",
"ini": "^1.3.4",
"html5-validator": "^1.0.7",
"ini": "^1.3.5",
"leveldown": "^1.9.0",
"linvodb3": "^3.25.0",
"linvodb3": "^3.25.1",
"luamin": "^1.0.4",
"mkdirp": "^0.5.1",
"mkdirp-promise": "^5.0.1",
"moment": "^2.19.1",
"moment": "^2.20.1",
"ncp": "^2.0.0",
"nedb": "^1.8.0",
"needle": "^1.1.0",
"node-factorio-api": "^0.1.28",
"node-factorio-api": "^0.2.2",
"node-forge": "^0.7.1",
"request": "^2.83.0",
"right-pad": "^1.0.1",
@@ -53,12 +53,12 @@
"underscore": "^1.8.3"
},
"devDependencies": {
"documentation": "^5.3.3",
"documentation": "^5.4.0",
"istanbul": "^0.4.5",
"mocha": "^3.5.3",
"mocha-sinon": "^2.0.0",
"mocha.parallel": "^0.15.3",
"nock": "^9.0.27",
"mocha.parallel": "^0.15.5",
"nock": "^9.1.6",
"sinon": "^2.4.1",
"supertest": "^3.0.0"
}