2023-07-31 15:48:52 +02:00
#!/usr/bin/env node
var version = process . argv [ 2 ] ;
var https = require ( "https" ) ;
var os = require ( "os" ) ;
var fs = require ( "fs" ) ;
var zip = require ( "zip" ) ;
var isSVRJSinstalled = fs . existsSync ( "svr.js" ) ;
if ( ! version ) {
console . log ( "A utility to create and update SVR.JS" ) ;
console . log ( "Usage:" ) ;
console . log ( "create-svrjs-server <version>" ) ;
console . log ( " version - SVR.JS version you want to download" ) ;
console . log ( " 'latest' -> latest SVR.JS version" ) ;
console . log ( " 'lts' -> latest LTS SVR.JS version" ) ;
2023-08-16 08:21:43 +02:00
console . log ( " '3.6.4' -> SVR.JS 3.6.4" ) ;
2023-07-31 15:48:52 +02:00
} else if ( version == "latest" || version == "lts" ) {
https . get ( {
2024-09-21 06:51:30 +02:00
hostname : "downloads.svrjs.org" ,
2023-07-31 15:48:52 +02:00
port : 443 ,
2024-09-21 06:51:30 +02:00
path : version == "lts" ? "/latest-lts.svrjs" : "/latest.svrjs" ,
2023-07-31 15:48:52 +02:00
method : "GET" ,
headers : {
"User-Agent" : "create-svrjs-server"
}
} , function ( res ) {
if ( res . statusCode != 200 ) {
console . log ( "Server returns " + res . statusCode + " HTTP code" ) ;
return ;
}
var data = "" ;
res . on ( "data" , function ( chunk ) {
data += chunk ;
} ) ;
res . on ( "end" , function ( ) {
2024-09-21 06:51:30 +02:00
var dlver = data . trim ( ) ;
2023-07-31 15:48:52 +02:00
if ( ! dlver ) {
2024-09-21 06:51:30 +02:00
console . log ( "Can't obtain the latest version from downloads server" ) ;
2023-07-31 15:48:52 +02:00
} else {
2024-09-21 06:51:30 +02:00
console . log ( "Selected SVR.JS " + dlver ) ;
downloadSVRJS ( dlver ) ;
2023-07-31 15:48:52 +02:00
}
} ) ;
} ) . on ( "error" , function ( ) {
2024-03-09 17:01:21 +01:00
console . log ( "Can't connect to the SVR.JS download server!" ) ;
2023-07-31 15:48:52 +02:00
} ) ;
} else {
downloadSVRJS ( version ) ;
}
2023-08-16 08:21:43 +02:00
2023-07-31 23:04:36 +02:00
function downloadSVRJS ( oversion ) {
var version = oversion . toLowerCase ( ) . replace ( /[^0-9a-z.]/g , "." ) ;
2023-11-29 02:28:10 +01:00
var path = "/svr.js." + version + ".zip" ;
if ( version . indexOf ( "beta" ) != - 1 ) path = "/beta/svr.js." + version + ".zip" ;
if ( version . indexOf ( "nightly" ) != - 1 ) path = "/nightly/svr.js." + version + ".zip" ;
2023-07-31 15:48:52 +02:00
https . get ( {
2023-11-29 02:28:10 +01:00
hostname : "downloads.svrjs.org" ,
2023-07-31 15:48:52 +02:00
port : 443 ,
2023-07-31 23:04:36 +02:00
path : path ,
2023-07-31 15:48:52 +02:00
method : "GET" ,
headers : {
"User-Agent" : "create-svrjs-server"
}
} , function ( res ) {
if ( res . statusCode != 200 ) {
2023-07-31 23:04:36 +02:00
console . log ( "Server returns " + res . statusCode + " HTTP code while trying to download SVR.JS " + oversion ) ;
2023-11-29 02:28:10 +01:00
console . log ( "Make sure you're using the latest version of the create-svrjs-server utility." ) ;
2023-07-31 15:48:52 +02:00
return ;
}
var zipFile = fs . createWriteStream ( "svrjs.zip" ) ;
res . on ( "end" , function ( ) {
console . log ( "Downloaded .zip file" ) ;
fs . readFile ( "svrjs.zip" , function ( err , data ) {
if ( err ) {
console . log ( "Can't read downloaded file!" ) ;
return ;
} else {
var reader = zip . Reader ( data ) ;
var allFiles = reader . toObject ( ) ;
var allFileNames = Object . keys ( allFiles ) ;
for ( var i = 0 ; i < allFileNames . length ; i ++ ) {
var paths = allFileNames [ i ] . split ( "/" ) ;
2023-08-16 08:21:43 +02:00
if ( ! isSVRJSinstalled || allFileNames [ i ] . match ( /^(?:[^\/.]+\.compressed|(?:log(?:viewer|highlight)|svr(?:passwd|_new)?)\.js|node_modules(?:\/|$))/ ) ) {
2023-07-31 15:48:52 +02:00
for ( var j = 0 ; j < paths . length - 1 ; j ++ ) {
var dirname = JSON . parse ( JSON . stringify ( paths ) ) . splice ( 0 , j + 1 ) . join ( "/" ) ;
if ( ! fs . existsSync ( dirname ) ) {
fs . mkdirSync ( dirname ) ;
}
}
fs . writeFileSync ( allFileNames [ i ] , allFiles [ allFileNames [ i ] ] ) ;
}
}
fs . unlinkSync ( "svrjs.zip" ) ;
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" ) ;
}
} ) ;
} ) ;
res . pipe ( zipFile ) ;
} ) . on ( "error" , function ( ) {
2023-11-29 02:28:10 +01:00
console . log ( "Can't connect to the SVR.JS download server!" ) ;
2023-07-31 15:48:52 +02:00
} ) ;
}