先日の 記事 のthincaさんフォーク版、僕々仕様のWindows上でのCoffeeScriptコンパイラに --runcmd オプションを加えた。

yukimemi/coffee-script-on-jscript

> coffee
Usage: coffee [options] path/to/script.coffee

  -b, --bare         compile without the top-level function wrapper
  -c, --compile      compile to JavaScript and save as .js files
      --encoding     character encoding used by source files
  -e, --eval         compile a string from the command line
  -h, --help         display this help message
  -j, --join         concatenate the scripts before compiling
  -n, --nodes        print out the parse tree that the parser produces
  -o, --output       set the directory for compiled JavaScript
  -p, --print        print the compiled JavaScript to stdout
  -s, --stdio        listen for and compile scripts over stdio
  -t, --tokens       print the tokens that the lexer produces
  -v, --version      display CoffeeScript version
  -w, --watch        watch scripts for changes, and recompile
  -r, --runcmd       run the compiled scripts as JScript

最後の行のやつ。

オプションで -r もしくは、 --runcmd を指定すると、自動でコンパイルされて実行形式 (.cmd) になったファイルが実行される。

  • hello.coffee
do (name = "CoffeeScript") ->
  WScript.Echo "はろー, \#{name} !"

上記の hello.coffee を、文字コード: UTF-8 改行コード: LF で作成し、次のようにコンパイル。

> coffee -r hello.coffee
はろー, CoffeeScript !

ExitCode = [0]

自動でコンパイルしてJScriptとして実行される。(あと一応戻り値もEchoするようにしておいた。)

--watch オプションと一緒に使うとかなり便利。

> coffee -rw hello.coffee
はろー, CoffeeScript !

ExitCode = [0]


へろー, CoffeeScript !

ExitCode = [0]

保存する度に自動でコンパイル、実行まで行ってくれる。

便利なのはいいんだけど、今どきJScriptとか、時代に逆行してるなぁ・・・。


参考

Exec メソッド