Last active
April 3, 2020 06:32
-
-
Save GianSinghSarao/0456406d7ea6e1e084c3259b597e5e25 to your computer and use it in GitHub Desktop.
Wrapper for NodeJS scripts to make them portable on Windows. Allows distributing a single file which can be executed thanks to Microsoft's JScript. Proof of concept, this should probably not be used as-is, hence it's in a gist.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function (){ | |
var IsWscript = (function () { | |
var isWscript = true; | |
try { | |
WScript.Echo("Hello World!"); | |
} catch (e) { | |
isWscript = false; | |
} | |
return isWscript; | |
})(); | |
if (IsWscript) { | |
var Arguments = ""; | |
for (var i = 0; i < WScript.Arguments.length; i++) { | |
Arguments += WScript.Arguments.Item(i) + " "; | |
} | |
var runCommandNoWindow = function (strCmd) { | |
var res = null; | |
try { | |
var o = WScript.CreateObject("WScript.Shell"); | |
res = o.Run(strCmd, 0, true); | |
} catch (e) { | |
res = null; | |
} | |
return res; | |
}; | |
if (runCommandNoWindow("node --version") == null) { | |
var is64bit = (function () { | |
var sh = WScript.CreateObject("WScript.Shell"); | |
if (sh.ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%") != "x86" || sh.ExpandEnvironmentStrings("%PROCESSOR_ARCHITEW6432%") == "AMD64") { | |
return true; | |
} else { | |
return false; | |
} | |
})(); | |
var a = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); | |
a.open("GET", is64bit ? "https://nodejs.org/dist/latest/win-x64/node.exe" : "https://nodejs.org/dist/latest/win-x86/node.exe", false); | |
a.send(); | |
var b = new ActiveXObject("ADODB.Stream"); | |
b.Type = 1; | |
b.Open(); | |
b.Write(a.ResponseBody); | |
b.SaveToFile("node.exe"); | |
b.Close(); | |
} else { | |
var sh = WScript.CreateObject("WScript.Shell"); | |
sh.Run('cmd /k node "' + WScript.ScriptFullName + '" ' + Arguments, 10, false); | |
WScript.Quit(); | |
} | |
} | |
})(); | |
// Now the modern js code appears. | |
// This is still a work in progress. | |
// Modern Syntax like let and const still can't be used, but transpiling to es3 should work | |
// Hopefully there is a clever way to have modern syntax, (maybe alternate streams on NTFS?) | |
// Example NodeJS script below, downloads the latest 64bit version of node for windows | |
// Written to run in NodeJS | |
var https = require('https'), | |
fs = require('fs'); | |
https.get('https://nodejs.org/dist/latest/win-x64/node.exe', function (r) { | |
return r.pipe(fs.createWriteStream('node64.exe')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written to run in MicroSoft CScript/WScript | |
// Not fun, especially because it's obsolete, and the documentation is scarce. | |
// I only found WinHttp after trying to get MSXML.XMLHTTP.6.0 to work for hours. | |
// The following page was very useful, (even though its for VBScript): | |
// https://www.codeproject.com/tips/506439/downloading-files-with-vbscript | |
var a = new ActiveXObject("WinHttp.WinHttpRequest.5.1"); | |
a.open("GET", "https://nodejs.org/dist/latest/win-x64/node.exe", false); | |
a.send(); | |
var b = new ActiveXObject("ADODB.Stream"); | |
b.Type = 1; | |
b.Open(); | |
b.Write(a.ResponseBody); | |
b.SaveToFile("Node.exe"); | |
b.Close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written to run in NodeJS | |
var https = require('https'), | |
fs = require('fs'); | |
https.get('https://nodejs.org/dist/latest/win-x64/node.exe', function (r) { | |
return r.pipe(fs.createWriteStream('node64.exe')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finally got the display order right! If only I knew it was alphabetical not in order of file creation. I could have saved so much time.