-
-
Save satyr/964240 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Coco compiler wrapper for WSH | |
// (must be placed in the same directory as coco.js) | |
// Usage: cscript cowsh.js [--watch] file | |
// inspired by http://kennyj-jp.blogspot.com/2011/01/coffeescriptwindows.html | |
var fs = WScript.CreateObject("Scripting.FileSystemObject"); | |
var scriptPath = WScript.ScriptFullName.slice( | |
0, -WScript.ScriptName.length); | |
var Coco = new new Function( | |
fs.OpenTextFile(scriptPath + "coco.js", 1).ReadAll() | |
)().Coco; | |
function compileFile(file) { | |
var fh = fs.OpenTextFile(file, 1); | |
var src = fh.ReadAll(); | |
fh.Close(); | |
var output = file.replace(/\.co$/i, '') + '.js'; | |
WScript.Echo('Compiling ' + file + ' to ' + output); | |
try { | |
var compiled = Coco.compile(src); | |
} catch(e) { | |
WScript.Echo(e.message); | |
return; | |
} | |
var fh = fs.OpenTextFile(output, 2, true); | |
fh.Write(compiled); | |
fh.Close(); | |
} | |
function watchFile(file) { | |
var sh = new ActiveXObject("WScript.Shell"); | |
var fullPath = getFullPath(file, sh.CurrentDirectory); | |
var drive = getDrive(fullPath); | |
var basename = fullPath.replace(/^.*\\/, ''); | |
var dir = fullPath.slice(2).slice(0, -basename.length); | |
var computer = '.'; | |
var wmi = GetObject("winmgmts:\\\\" + computer + "\\root\\CIMV2"); | |
WScript.Echo('Watching ' + drive + dir + ' for file ' + basename); | |
var wql = [ | |
"SELECT * FROM __InstanceModificationEvent WITHIN 2 WHERE ", | |
"Targetinstance ISA 'CIM_DataFile' AND ", | |
"TargetInstance.Drive = '" + drive + "' AND ", | |
"TargetInstance.Path = '" + dir.split('\\').join('\\\\') + "'" | |
].join(''); | |
var eventSrc = wmi.ExecNotificationQuery(wql); | |
while (true) { | |
var eventObj = eventSrc.NextEvent(); | |
var objItem = eventObj.TargetInstance; | |
if (objItem.Name === fullPath.toLowerCase()) { | |
compileFile(file); | |
} | |
} | |
} | |
function getFullPath(file, base) { | |
if (file.indexOf('\\') === 0) { | |
file = getDrive(base) + file; | |
} else if (!getDrive(file)) { | |
file = base + '\\' + file; | |
} | |
return file | |
.replace(/\\(\.)?\\/g, '\\') // replace \.\ and \\ to \ | |
.replace(/\\[^\\]*\\\.\.\\/g, '\\') // replace \foo\..\ to \ | |
.replace(/:\\(..\\)*/, ':\\'); // replace :\..\ to :\ | |
} | |
function getDrive(file) { | |
var m; | |
if (m = file.match(/^([a-zA-Z]:)/)) { | |
return m[1].toUpperCase(); | |
} | |
return null; | |
} | |
function main() { | |
var usage = 'Usage: cscript cowsh.js [--watch] file'; | |
var scriptPath = WScript.ScriptFullName.slice( | |
0, -WScript.ScriptName.length); | |
var args = WScript.Arguments; | |
var len = args.length; | |
if (len === 0) { | |
WScript.Echo(usage); | |
WScript.Quit(1); | |
} else { | |
var watch = false; | |
var i = 0; | |
if (/^-(?:w|-watch)$/.test(args.Item(i))) { | |
watch = true; | |
if (++i == len) { | |
WScript.Echo(usage); | |
WScript.Quit(1); | |
} | |
} | |
var file = args.Item(i).replace(/\//g, '\\'); | |
compileFile(file); | |
if (watch) { | |
watchFile(file); | |
} | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment