Fixed mod manager not exiting after downloading mods

This commit is contained in:
Daniel Vestøl committed 2018-06-16 15:41:06 +02:00
1 parent e4c276054b
commit 447766c752
3 files changed
+56 -13

No files matched your search

+16 -9
View File
@@ -158,7 +158,7 @@ if (!command || command == "help" || command == "--help") {
}); });
displayLines.forEach(line => console.log(line)); displayLines.forEach(line => console.log(line));
process.exit(1); process.exit(0);
} else if (command == "manage"){ } else if (command == "manage"){
// console.log("Launching mod manager"); // console.log("Launching mod manager");
//const fullUsage = 'node client.js manage [instance, "shared"] ["mods", "config"] ...'; //const fullUsage = 'node client.js manage [instance, "shared"] ["mods", "config"] ...';
@@ -175,20 +175,26 @@ if (!command || command == "help" || command == "--help") {
const action = process.argv[5] || ""; const action = process.argv[5] || "";
if(instance){ if(instance){
if(tool == "mods"){ if(tool == "mods"){
(async function(){try{
// allow managing mods // allow managing mods
if(action == "list"){ if(action == "list"){
modManager.listMods(instance); console.log(await modManager.listMods(instance));
} else if(action == "search"){ } else if(action == "search"){
modManager.findMods(process.argv[6]); console.log(await modManager.findMods(process.argv[6]));
} else if(action == "add" || action == "download"){ } else if(action == "add" || action == "download"){
modManager.addMod(process.argv[6], instance); await modManager.addMod(process.argv[6], instance);
} else if(action == "remove" || action == "rm" || action == "delete"){ } else if(action == "remove" || action == "rm" || action == "delete"){
modManager.removeMod(process.argv[6], instance); await modManager.removeMod(process.argv[6], instance);
} else if(action == "update"){ } else if(action == "update"){
modManager.updateAllMods(); await modManager.updateAllMods();
} else { } else {
usage(instance, tool); usage(instance, tool);
} }
process.exit(0);
}catch(e){
console.log("Got error from modManager:")
console.log(e);
}})();
} else if(tool == "config"){ } else if(tool == "config"){
// allow managing the config // allow managing the config
if(action == "list" || action == "show" || action == "display"){ if(action == "list" || action == "show" || action == "display"){
@@ -213,7 +219,7 @@ if (!command || command == "help" || command == "--help") {
console.log('Usage:'); console.log('Usage:');
usage(instance); usage(instance);
} }
// process.exit(1); // process.exit(0);
} else if (command == "delete") { } else if (command == "delete") {
if (!process.argv[3]) { if (!process.argv[3]) {
console.error("Usage: node client.js delete [instance]"); console.error("Usage: node client.js delete [instance]");
@@ -221,10 +227,10 @@ if (!command || command == "help" || command == "--help") {
} else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") { } else if (typeof process.argv[3] == "string" && fs.existsSync("./instances/" + process.argv[3]) && process.argv[3] != "/" && process.argv[3] != "") {
fileOps.deleteFolderRecursiveSync("./instances/" + process.argv[3]); fileOps.deleteFolderRecursiveSync("./instances/" + process.argv[3]);
console.log("Deleted instance " + process.argv[3]); console.log("Deleted instance " + process.argv[3]);
process.exit(1); process.exit(0);
} else { } else {
console.error("Instance not found: " + process.argv[3]); console.error("Instance not found: " + process.argv[3]);
process.exit(1); process.exit(0);
} }
} else if (command == "download") { } else if (command == "download") {
console.log("Downloading mods..."); console.log("Downloading mods...");
@@ -238,6 +244,7 @@ if (!command || command == "help" || command == "--help") {
https.get(url, function(response) { https.get(url, function(response) {
response.pipe(file); response.pipe(file);
console.log("Downloaded "+name); console.log("Downloaded "+name);
process.exit(0);
}); });
} }
} else if (command == "start" && instance === undefined) { } else if (command == "start" && instance === undefined) {
+38 -4
View File
@@ -11,6 +11,7 @@ const fs = require("fs");
const https = require('follow-redirects').https; const https = require('follow-redirects').https;
const factorioAPI = require("node-factorio-api"); const factorioAPI = require("node-factorio-api");
const sLog = require("single-line-log").stdout; const sLog = require("single-line-log").stdout;
const query = require('cli-interact').getYesNo;
const fileOps = require("_app/fileOps"); const fileOps = require("_app/fileOps");
const config = require("./../../config"); const config = require("./../../config");
@@ -35,6 +36,7 @@ modManager.listMods("shared");
// console.logs mods located in /sharedMods as a nice table // console.logs mods located in /sharedMods as a nice table
*/ */
module.exports.listMods = function(instance){ module.exports.listMods = function(instance){
return new Promise((resolve, reject) => {
if(instance && instances.includes(instance)){ if(instance && instances.includes(instance)){
factorioAPI.init(false, 'instances/'+instance+'/instanceMods', 'instances/'+instance+'/saves', config.factorio_version); factorioAPI.init(false, 'instances/'+instance+'/instanceMods', 'instances/'+instance+'/saves', config.factorio_version);
factorioAPI.authenticate({ factorioAPI.authenticate({
@@ -62,6 +64,7 @@ module.exports.listMods = function(instance){
}); });
}).catch(reason => { }).catch(reason => {
console.log("Failed to authenticate to factorio.com! Please check your connection and the username+token used for authentication in config.json"); console.log("Failed to authenticate to factorio.com! Please check your connection and the username+token used for authentication in config.json");
resolve();
}); });
} else { } else {
// list shared mods in /sharedMods/ // list shared mods in /sharedMods/
@@ -75,7 +78,10 @@ module.exports.listMods = function(instance){
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));
resolve();
}); });
}).catch(e => {
reject(new Error(e));
}); });
} }
@@ -85,6 +91,7 @@ module.exports.listMods = function(instance){
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()))
*/ */
});
} }
/** /**
Searches mods.factorio.com for mods matching a search term and prints Searches mods.factorio.com for mods matching a search term and prints
@@ -93,6 +100,7 @@ the results to console as a neat looking table
@param {string} searchTerm What to search for @param {string} searchTerm What to search for
*/ */
module.exports.findMods = function(searchTerm){ module.exports.findMods = function(searchTerm){
return new Promise((resolve, reject) => {
factorioAPI.init(false); factorioAPI.init(false);
factorioAPI.authenticate({ factorioAPI.authenticate({
username: config.username, username: config.username,
@@ -100,7 +108,7 @@ module.exports.findMods = function(searchTerm){
require_ownership: false require_ownership: false
}).then(token => { }).then(token => {
factorioAPI.searchMods(searchTerm).then(mods => { factorioAPI.searchMods(searchTerm).then(mods => {
console.log(asTable(mods.reverse().map(mod => { let result = asTable(mods.reverse().map(mod => {
return { return {
title:ellipse(mod.title, 50), title:ellipse(mod.title, 50),
name:ellipse(mod.name, 30), name:ellipse(mod.name, 30),
@@ -110,12 +118,19 @@ module.exports.findMods = function(searchTerm){
owner:mod.owner, owner:mod.owner,
//license: mod.license_name, //license: mod.license_name,
} }
}))) }));
}) resolve(result);
// console.log(result);
});
}).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.");
reject(new Error(e));
});
}); });
} }
module.exports.addMod = function(term, instance){ module.exports.addMod = function(term, instance){
return new Promise((resolve, reject) => {
if(instance == "shared"){ // modDirectory has to end in / if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/"; var modDirectory = "sharedMods/";
} else { } else {
@@ -134,6 +149,7 @@ module.exports.addMod = function(term, instance){
console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version); console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version);
factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{ factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{
console.log("Mod downloaded"); console.log("Mod downloaded");
resolve();
}); });
}); });
}); });
@@ -162,6 +178,7 @@ module.exports.addMod = function(term, instance){
}).on("end", x => { }).on("end", x => {
sLog.clear(); sLog.clear();
console.log(" Downloaded in "+moment.utc(moment.duration(Date.now()-downloadStartTime).as('milliseconds')).format('mm:ss')); // because sLog.clear() doesn't seem to do the job of ending the line properly console.log(" Downloaded in "+moment.utc(moment.duration(Date.now()-downloadStartTime).as('milliseconds')).format('mm:ss')); // because sLog.clear() doesn't seem to do the job of ending the line properly
resolve();
}); });
}); });
} }
@@ -174,16 +191,22 @@ module.exports.addMod = function(term, instance){
require_ownership: false require_ownership: false
}).then(() => { }).then(() => {
factorioAPI.searchMods(term).then(body => { factorioAPI.searchMods(term).then(body => {
console.log(body)
console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version); console.log("Im gonna download: "+body[0].name + " in version: " + body[0].latest_release.version);
factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{ factorioAPI.downloadMod( {name: body[0].name, version: body[0].latest_release.version}).then(()=>{
console.log("Mod downloaded"); console.log("Mod downloaded");
resolve();
}); });
}); });
}); });
} }
} else {
reject(new Error("Invalid search term"));
} }
});
} }
module.exports.removeMod = function removeMod(term, instance, force = false){ module.exports.removeMod = function removeMod(term, instance, force = false){
return new Promise((resolve, reject) => {
if(instance == "shared"){ // modDirectory has to end in / if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/"; var modDirectory = "sharedMods/";
} else { } else {
@@ -199,10 +222,21 @@ module.exports.removeMod = function removeMod(term, instance, force = false){
fs.unlinkSync(modDirectory+modsMatchingFilter[0]); fs.unlinkSync(modDirectory+modsMatchingFilter[0]);
console.log("Deleted."); console.log("Deleted.");
} else if(modsMatchingFilter.length > 1){ } else if(modsMatchingFilter.length > 1){
console.log("Found multiple mods, not doing anything"); console.log("Found multiple mods");
modsMatchingFilter.forEach(mod => console.log(mod)); modsMatchingFilter.forEach(mod => console.log(mod));
if(query("Delete all mods matching filter?")){
let modsToDelete = modsMatchingFilter.length;
for(let i = 0; i < modsToDelete; i++){
fs.unlinkSync(modDirectory+modsMatchingFilter[i]);
console.log("Deleted "+modsMatchingFilter[i]);
} }
} else console.log("Did not delete mods.")
} }
resolve();
} else {
reject(new Error("Invalid search term"));
}
});
} }
// 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases' // 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases'
+2
View File
@@ -27,6 +27,7 @@
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.18.2", "body-parser": "^1.18.2",
"cache-require-paths": "^0.3.0", "cache-require-paths": "^0.3.0",
"cli-interact": "^0.1.9",
"codecov": "^3.0.1", "codecov": "^3.0.1",
"compression": "^1.7.2", "compression": "^1.7.2",
"deepmerge": "^1.5.2", "deepmerge": "^1.5.2",
@@ -56,6 +57,7 @@
"request": "^2.83.0", "request": "^2.83.0",
"right-pad": "^1.0.1", "right-pad": "^1.0.1",
"rmdir-sync": "^1.0.1", "rmdir-sync": "^1.0.1",
"sanitizer": "^0.1.3",
"single-line-log": "^1.1.2", "single-line-log": "^1.1.2",
"socket.io": "^2.0.4", "socket.io": "^2.0.4",
"socket.io-client": "^2.0.4", "socket.io-client": "^2.0.4",