3 var version = process.argv[2];
4 var https = require("https");
5 var os = require("os");
6 var fs = require("fs");
7 var zip = require("zip");
9 var isSVRJSinstalled = fs.existsSync("svr.js");
12 console.log("A utility to create and update SVR.JS");
13 console.log("Usage:");
14 console.log("create-svrjs-server <version>");
15 console.log(" version - SVR.JS version you want to download");
16 console.log(" 'latest' -> latest SVR.JS version");
17 console.log(" 'lts' -> latest LTS SVR.JS version");
18 console.log(" '3.6.4' -> SVR.JS 3.6.4");
19 } else if(version == "latest" || version == "lts") {
21 hostname: "svrjs.org",
26 "User-Agent": "create-svrjs-server"
29 if(res.statusCode != 200) {
30 console.log("Server returns " + res.statusCode + " HTTP code");
34 res.on("data", function(chunk) {
37 res.on("end", function() {
38 var regex = />Download SVR\.JS ([^ <]+)<\/a>/;
39 if(version == "lts") {
40 regex = />Download SVR\.JS ([^ <]+) LTS<\/a>/;
42 var dlver = data.match(regex);
44 console.log("Can't obtain latest version from main page");
46 console.log("Selected SVR.JS " + dlver[1]);
47 downloadSVRJS(dlver[1]);
50 }).on("error", function() {
51 console.log("Can't connect to SVR.JS download server!");
54 downloadSVRJS(version);
57 function downloadSVRJS(oversion) {
58 var version = oversion.toLowerCase().replace(/[^0-9a-z.]/g,".");
59 var path = "/dl/svr.js." + version + ".zip";
60 if(version.indexOf("beta") != -1) path = "/dl/beta/svr.js." + version + ".zip";
61 if(version.indexOf("nightly") != -1) path = "/dl/nightly/svr.js." + version + ".zip";
63 hostname: "svrjs.org",
68 "User-Agent": "create-svrjs-server"
71 if(res.statusCode != 200) {
72 console.log("Server returns " + res.statusCode + " HTTP code while trying to download SVR.JS " + oversion);
75 var zipFile = fs.createWriteStream("svrjs.zip");
76 res.on("end", function() {
77 console.log("Downloaded .zip file");
78 fs.readFile("svrjs.zip", function(err,data) {
80 console.log("Can't read downloaded file!");
83 var reader = zip.Reader(data);
84 var allFiles = reader.toObject();
85 var allFileNames = Object.keys(allFiles);
86 for(var i=0;i<allFileNames.length;i++) {
87 var paths = allFileNames[i].split("/");
88 if(!isSVRJSinstalled || allFileNames[i].match(/^(?:[^\/.]+\.compressed|(?:log(?:viewer|highlight)|svr(?:passwd|_new)?)\.js|node_modules(?:\/|$))/)) {
89 for(var j=0;j<paths.length-1;j++) {
90 var dirname = JSON.parse(JSON.stringify(paths)).splice(0,j+1).join("/");
91 if(!fs.existsSync(dirname)) {
92 fs.mkdirSync(dirname);
95 fs.writeFileSync(allFileNames[i], allFiles[allFileNames[i]]);
98 fs.unlinkSync("svrjs.zip");
99 console.log("SVR.JS " + (isSVRJSinstalled ? "updated" : "installed") + "! To start SVR.JS, type \"" + (process.argv0 ? process.argv0 : "node") + " svr.js\" For more information refer to SVR.JS documentation at https://svrjs.org/docs");
104 }).on("error", function() {
105 console.log("Can't connect to SVR.JS download server!");