coffee.wsf 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <job>
  2. <!-- https://github.com/jashkenas/coffee-script/raw/master/extras/coffee-script.js -->
  3. <script src="coffee-script.js" language="JScript" />
  4. <script language="JScript">
  5. (function() {
  6. var args = [];
  7. for (var i = 0; i < WScript.Arguments.Length; i++) {
  8. args.push(WScript.Arguments.Item(i));
  9. }
  10. // FileSystemObject: http://msdn.microsoft.com/en-us/library/bkx696eh.aspx
  11. var fso = new ActiveXObject("Scripting.FileSystemObject");
  12. var isfolder = (args[0] && fso.folderExists(args[0]));
  13. if (isfolder) {
  14. f = fso.getFolder(args[0]);
  15. e = new Enumerator(f.files);
  16. for (; !e.atEnd(); e.moveNext()) {
  17. if (e.item().path.toLowerCase().lastIndexOf('.coffee') != -1) {
  18. convert(e.item(), args[1]);
  19. }
  20. }
  21. }
  22. else {
  23. convert(args[0], args[1])
  24. }
  25. })();
  26. function convert(input, output) {
  27. var fso = new ActiveXObject("Scripting.FileSystemObject");
  28. if (output) {
  29. // if output specifies a folder name, output filename is same as input filename with .coffee extension
  30. if (fso.folderExists(output)) {
  31. output = output + '\\' + fso.getFile(input).name.replace('\.coffee', '.js')
  32. }
  33. }
  34. var coffee;
  35. if (!input) {
  36. // Read all input data from STDIN
  37. var chunks = [];
  38. while (!WScript.StdIn.AtEndOfStream)
  39. chunks.push(WScript.StdIn.ReadAll());
  40. coffee = chunks.join('');
  41. }
  42. else {
  43. coffee = readUtf8(input);
  44. }
  45. try {
  46. if(!Object.create)
  47. Object.create = function(proto)
  48. {
  49. function f(){}
  50. f.prototype = proto;
  51. return new f;
  52. }
  53. var js = CoffeeScript.compile(coffee, {filename: "temp.coffee"});
  54. if (!output) {
  55. WScript.StdOut.Write(js);
  56. }
  57. else {
  58. writeUtf8(output, js);
  59. }
  60. }
  61. catch (err) {
  62. WScript.StdErr.WriteLine(err.message);
  63. WScript.Quit(1);
  64. }
  65. }
  66. function readUtf8(filename) {
  67. var stream = new ActiveXObject("ADODB.Stream");
  68. stream.Open();
  69. stream.Type = 2; // Text
  70. stream.Charset = 'utf-8';
  71. stream.LoadFromFile(filename);
  72. var text = stream.ReadText();
  73. stream.Close();
  74. return text;
  75. }
  76. function writeUtf8(filename, text) {
  77. var stream = new ActiveXObject("ADODB.Stream");
  78. stream.Type = 2; // Text
  79. stream.Charset = "utf-8";
  80. stream.Open();
  81. stream.WriteText(text);
  82. stream.Position = 0;
  83. stream.Type = 1; // Binary
  84. stream.Position = 3;
  85. var binary = stream.Read();
  86. stream.Close();
  87. stream.Open();
  88. stream.Write(binary);
  89. stream.SaveToFile(filename, 2);
  90. stream.Close();
  91. }
  92. </script>
  93. </job>