Created
April 10, 2011 07:58
-
-
Save edvakf/912130 to your computer and use it in GitHub Desktop.
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
// Coffee Script compiler wrapper for WSH JScript (must be placed in the same directory as coffee-script.js) | |
// Usage: cscript coffee-maker.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); | |
eval(fs.OpenTextFile(scriptPath + "coffee-script.js", 1).ReadAll()); | |
function compileFile(file) { | |
var fh = fs.OpenTextFile(file, 1); | |
var src = fh.ReadAll(); | |
fh.Close(); | |
WScript.Echo('Compiling ' + file + ', output written to ' + output); | |
try { | |
var compiled = CoffeeScript.compile(src); | |
} catch(e) { | |
WScript.Echo("Error : " + e.message); | |
return; | |
} | |
var output = file.replace(/\.coffee$/, '') + '.js'; | |
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 coffee-maker.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 (args.Item(i) === '--watch') { | |
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