Add support for SVR.JS utility updates.
[create-svrjs-server.git] / downloader.js
blob04834c146ac893d23404b3f50866fc3932344060
1 #!/usr/bin/env node
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");
11 if(!version) {
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") {
20   https.get({
21     hostname: "svrjs.org",
22     port: 443,
23     path: "/",
24     method: "GET",
25     headers: {
26       "User-Agent": "create-svrjs-server"
27     }
28   }, function(res) {
29     if(res.statusCode != 200) {
30       console.log("Server returns " + res.statusCode + " HTTP code");
31       return;
32     }
33     var data = "";
34     res.on("data", function(chunk) {
35         data += chunk;
36     });
37     res.on("end", function() {
38         var regex = />Download SVR\.JS ([^ <]+)<\/a>/;
39         if(version == "lts") {
40            regex = />Download SVR\.JS ([^ <]+) LTS<\/a>/;
41         }
42         var dlver = data.match(regex);
43         if(!dlver) {
44           console.log("Can't obtain latest version from main page");
45         } else {
46           console.log("Selected SVR.JS " + dlver[1]);
47           downloadSVRJS(dlver[1]);
48         }
49     });
50   }).on("error", function() {
51     console.log("Can't connect to SVR.JS download server!");  
52   });
53 } else {
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";
62    https.get({
63     hostname: "svrjs.org",
64     port: 443,
65     path: path,
66     method: "GET",
67     headers: {
68       "User-Agent": "create-svrjs-server"
69     }
70   }, function(res) {
71     if(res.statusCode != 200) {
72       console.log("Server returns " + res.statusCode + " HTTP code while trying to download SVR.JS " + oversion);
73       return;
74     }
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) {
79         if(err) {
80           console.log("Can't read downloaded file!");
81           return;
82         } else {
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);
93                 }
94               }
95               fs.writeFileSync(allFileNames[i], allFiles[allFileNames[i]]);
96             }
97           }
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");
100         }
101       });
102     });
103     res.pipe(zipFile);
104      }).on("error", function() {
105     console.log("Can't connect to SVR.JS download server!");  
106   });