Made progress bar for github mod downloads

This commit is contained in:
Danielv123 committed 2018-02-22 21:00:35 +01:00
1 parent 29bffa472b
commit 1fcd91342f
2 files changed
+13 -1

No files matched your search

+12 -1
View File
@@ -10,6 +10,7 @@ const syncRequest = require("sync-request");
const fs = require("fs");
const https = require('follow-redirects').https;
const factorioAPI = require("node-factorio-api");
const sLog = require("single-line-log").stdout;
const fileOps = require("./../fileOps.js");
const config = require("./../../config");
@@ -19,6 +20,8 @@ const instances = require("./../getInstances.js").sync();
function ellipse(str, max){
return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str;
}
// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
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]};
/**
Lists mods for an instance as a table. Warning, handles printing internally.
@@ -68,6 +71,7 @@ module.exports.listMods = function(instance){
token: config.token,
require_ownership: false
}).then(() => {
console.log("\r\n")
console.log("* Mods shared by all instances:")
factorioAPI.getInstalledMods().then(mods => {
console.log(asTable(mods));
@@ -142,9 +146,16 @@ module.exports.addMod = function(term, instance){
if(url) {
console.log("Downloading " + url);
let file = fs.createWriteStream(modDirectory + name);
let bytesDownloaded = 0;
https.get(url, function(response) {
response.pipe(file);
console.log("Downloaded "+name);
response.on("data", chunk => {
bytesDownloaded += chunk.length;
sLog("Downloading "+name+": "+formatBytes(bytesDownloaded));
}).on("end", x => {
sLog.clear();
console.log(""); // because sLog.clear() doesn't seem to do the job of ending the line properly
});
});
}
} else {
+1
View File
@@ -47,6 +47,7 @@
"right-pad": "^1.0.1",
"rmdir-sync": "^1.0.1",
"simple-rcon": "^0.3.1",
"single-line-log": "^1.1.2",
"socket.io": "^2.0.4",
"socket.io-client": "^2.0.4",
"sync-request": "^4.0.1",