I have waged and long and bloody battle to find a way to work with CoffeeScript on Windows – and now I finally have!

The first step is to get CoffeeScript from github. You can either pull the source with git or download an archive after some hardcore archiving action.

The CoffeeScript compiler is written in CoffeeScript and provided in compiled JavaScript form. Therefore, we need a server-side JavaScript environment to execute the compiler and transform our CoffeeScript scripts into JavaScript that a browser can execute. Somehow, in my struggles I missed JSDB, a minimal server-side JavaScript environment. JSDB comes as a zip archive. To access the shell simply unzip and run jsdb.exe. From the shell you can execute any JavaScript that does not rely upon browser objects.

image

The basic steps to compile coffeescript are:

  1. Launch jsdb
  2. Load the CoffeeScript compiler
  3. Pass some CoffeeScript to the CoffeeScript.compile function.

Here is an example:

image

It would be nice to be able to pass a directory to the compiler and have it compile all of the CoffeeScript files to JavaScript. I created the following scripts to do that, based on the work of TBD. Both files should be placed in the CoffeeScript bin directory. When complete a directory can be compiled by calling Coffee.bat and passing the path to jsdb.exe and the directory to be compiled as follows:

coffee.bat “..\jsdb\jsdb.exe” “d:\scripts”

Coffee.bat

@echo off

if "%~1" == "" goto usage
if "%~2" == "" goto usage

%~1 -nojit jsdb.js %~1 %2
goto end

:usage
echo coffee.bat [path_to_jsdb.exe] [directory_containing_coffee_files]
goto end

:end

jsdb.js

load("..\\extras\\coffee-script.js");
writeln(system.arguments[1]);
system.setcwd(system.arguments[1]);
var files = system.files('*.coffee');

function getSource(filename) {
  var sourceStream = new Stream(filename);
  return sourceStream.read(1000000);
}

function getJavaScript(coffee) {
  return CoffeeScript.compile(coffee);
};

function writeJavaScript(javaScript, filename) {
  var javaScriptStream = new Stream(filename.split('.')[0] + ".js",'wt');
  javaScriptStream.write(javaScript);
}

for (var i = 0; i < files.length; i++) {
  var coffee = getSource(files[i]);
  var javaScript = getJavaScript(coffee);
  writeln(javaScript);  
  writeJavaScript(javaScript, files[i]);
}

Here is the result of compiling the examples from the CoffeeScript home page. The output is saved to cs_examples.js:

image