coffee.wsf 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. var js = CoffeeScript.compile(coffee);
  47. if (!output) {
  48. WScript.StdOut.Write(js);
  49. }
  50. else {
  51. writeUtf8(output, js);
  52. }
  53. }
  54. catch (err) {
  55. WScript.StdErr.WriteLine(err.message);
  56. WScript.Quit(1);
  57. }
  58. }
  59. function readUtf8(filename) {
  60. var stream = new ActiveXObject("ADODB.Stream");
  61. stream.Open();
  62. stream.Type = 2; // Text
  63. stream.Charset = 'utf-8';
  64. stream.LoadFromFile(filename);
  65. var text = stream.ReadText();
  66. stream.Close();
  67. return text;
  68. }
  69. function writeUtf8(filename, text) {
  70. var stream = new ActiveXObject("ADODB.Stream");
  71. stream.Open();
  72. stream.Type = 2; // Text
  73. stream.Charset = 'utf-8';
  74. stream.WriteText(text);
  75. stream.SaveToFile(filename, 2);
  76. stream.Close();
  77. }
  78. </script>
  79. </job>