diff --git a/.gitignore b/.gitignore index 9d1110aa..a62cbcce 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ package-lock.json mod_cache.json static/pictures/spritesheet.js static/pictures/spritesheet.png +static/external # weird pattern to ignore sharedPlugins except not ignore some core modules !sharedPlugins/ diff --git a/lib/npmPostinstall.js b/lib/npmPostinstall.js index 8fc1de66..2d73c004 100644 --- a/lib/npmPostinstall.js +++ b/lib/npmPostinstall.js @@ -1,35 +1,61 @@ var mkdirp = require("mkdirp"); var http = require('http'); +var https = require('https'); var fs = require('fs'); +function makeDirectory(path){ + return new Promise((resolve, reject) => { + mkdirp(path, function(err){ + if(err){ + console.error("Unable to mkdirp "+path); + console.error(err); + resolve(false); + } else { + console.log("mkdirp "+path); + resolve(true); + } + }); + }); +} +function download(url, file, callback){ + return new Promise((resolve, reject) => { + let pathArray = file.split("/"); + pathArray.pop(); // remove filename from the end + if(pathArray.length >= 1){ + // this might be an invalid path! + fs.stat(pathArray.join("/"), (err, stat) => { + if(err){ + makeDirectory(pathArray.join("/")).then(()=>{ + doDownload(url, file, callback); + }); + } else { + doDownload(url, file, callback); + } + }); + } else { + doDownload(url, file, callback); + } + function doDownload(url, file, callback){ + var writeStream = fs.createWriteStream(file); + let protocol = http; + if(url.split(":")[0] == "https") protocol = https; + var request = protocol.get(url, function(response) { + response.pipe(writeStream); + response.on("end", () => { + if(callback && typeof callback == "function") callback(file); + console.log("Downloaded "+file+" from "+url); + resolve(); + }); + }); + } + }); +} + var directories = [ "database/linvodb", "sharedMods", "instances", ]; - -directories.forEach(path => { - mkdirp("database/linvodb", function(err){ - if(err){ - console.error("Unable to mkdirp "+path); - console.error(err); - } else { - console.log("mkdirp "+path); - } - }); -}); - -function download(url, file, callback){ - var writeStream = fs.createWriteStream(file); - var request = http.get(url, function(response) { - response.pipe(writeStream); - response.on("end", () => { - if(callback && typeof callback == "function") callback(file); - console.log("Downloaded "+file+" from "+url); - }); - }); -} - let downloadList = [ { url: "http://fbpviewer.trakos.pl/images/spritesheet.json", @@ -42,10 +68,58 @@ let downloadList = [ url: "http://fbpviewer.trakos.pl/images/spritesheet.png", file: "static/pictures/spritesheet.png", }, + { + url: "https://code.jquery.com/jquery.js", + file: "static/external/jquery.js", + }, + { + url: "https://code.jquery.com/ui/1.12.1/jquery-ui.js", + file: "static/external/jquery-ui.js", + }, + { + url: "https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.js", + file: "static/external/canvasjs.js", + }, + { + url: "https://momentjs.com/downloads/moment.min.js", + file: "static/external/moment.min.js", + }, + { + url: "https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.min.js", + file: "static/external/mousetrap.min.js", + }, + { + url: "https://fonts.googleapis.com/css?family=Roboto", + file: "static/external/css/Roboto.css", + }, + { + url: "https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js", + file: "static/external/socket.io.js", + }, ] -downloadList.forEach(item => { - download(item.url, item.file, item.callback); -}); + +// initiate folder creations and downloads +processMkdirRecursive(processDownloadRecursive); +function processMkdirRecursive(callback){ + let item = directories.shift(); + makeDirectory(item).then(()=>{ + if(directories.length == 0){ + if(callback) callback(); + } else { + processMkdirRecursive(callback); + } + }); +} +function processDownloadRecursive(callback){ + let item = downloadList.shift(); + download(item.url, item.file, item.callback).then(()=>{ + if(downloadList.length == 0){ + if(callback) callback(); + } else { + processDownloadRecursive(callback); + } + }); +} // generate SSL certificates if they are missing if(!fs.existsSync("database/certificates") || !fs.existsSync("database/certificates/cert.crt") || !fs.existsSync("database/certificates/cert.key")){ diff --git a/static/head.html b/static/head.html index 8bfe6575..c503fe6b 100644 --- a/static/head.html +++ b/static/head.html @@ -1,9 +1,9 @@ Clusterio - - - - - + + + + + - \ No newline at end of file + \ No newline at end of file diff --git a/static/remoteMap.html b/static/remoteMap.html index 0a8f8841..fbaa2278 100644 --- a/static/remoteMap.html +++ b/static/remoteMap.html @@ -1,7 +1,7 @@ <%- include("head.html") %> - +