2010-02-11 01:57:33 -05:00
( function ( ) {
2010-02-24 18:56:32 -05:00
var BANNER , SWITCHES , coffee , compile _options , compile _script , compile _scripts , compile _stdio , fs , lint , option _parser , options , optparse , parse _options , path , print _tokens , sources , usage , version , watch _scripts , write _js ;
2010-02-15 19:08:14 -05:00
fs = require ( 'fs' ) ;
2010-02-12 22:59:21 -05:00
path = require ( 'path' ) ;
2010-02-11 01:57:33 -05:00
coffee = require ( 'coffee-script' ) ;
2010-02-14 15:16:33 -05:00
optparse = require ( 'optparse' ) ;
2010-02-11 01:57:33 -05:00
BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee" ;
2010-02-24 18:56:32 -05:00
SWITCHES = [ [ '-i' , '--interactive' , 'run an interactive CoffeeScript REPL' ] , [ '-r' , '--run' , 'compile and run a CoffeeScript' ] , [ '-o' , '--output [DIR]' , 'set the directory for compiled JavaScript' ] , [ '-w' , '--watch' , 'watch scripts for changes, and recompile' ] , [ '-p' , '--print' , 'print the compiled JavaScript to stdout' ] , [ '-l' , '--lint' , 'pipe the compiled JavaScript through JSLint' ] , [ '-s' , '--stdio' , 'listen for and compile scripts over stdio' ] , [ '-e' , '--eval' , 'compile a string from the command line' ] , [ '-n' , '--no-wrap' , 'compile without the top-level function wrapper' ] , [ '-t' , '--tokens' , 'print the tokens that the lexer produces' ] , [ '-tr' , '--tree' , 'print the parse tree that Jison produces' ] , [ '-v' , '--version' , 'display CoffeeScript version' ] , [ '-h' , '--help' , 'display this help message' ] ] ;
2010-02-13 10:16:28 -05:00
options = { } ;
sources = [ ] ;
option _parser = null ;
2010-02-11 01:57:33 -05:00
// The CommandLine handles all of the functionality of the `coffee` utility.
exports . run = function run ( ) {
2010-02-16 01:04:48 -05:00
var flags , separator ;
2010-02-13 10:16:28 -05:00
parse _options ( ) ;
2010-02-24 17:57:58 -05:00
if ( options . help ) {
return usage ( ) ;
}
if ( options . version ) {
return version ( ) ;
}
2010-02-13 10:16:28 -05:00
if ( options . interactive ) {
2010-02-17 00:50:08 -05:00
return require ( 'repl' ) ;
2010-02-13 10:07:59 -05:00
}
2010-02-24 18:18:29 -05:00
if ( options . stdio ) {
return compile _stdio ( ) ;
}
2010-02-13 23:27:13 -05:00
if ( options . eval ) {
2010-02-24 17:57:58 -05:00
return compile _script ( 'unknown' , sources [ 0 ] ) ;
2010-02-13 23:27:13 -05:00
}
2010-02-13 10:16:28 -05:00
if ( ! ( sources . length ) ) {
2010-02-24 17:57:58 -05:00
return usage ( ) ;
2010-02-13 10:16:28 -05:00
}
2010-02-16 01:04:48 -05:00
separator = sources . indexOf ( '--' ) ;
flags = [ ] ;
if ( separator >= 0 ) {
flags = sources . slice ( ( separator + 1 ) , sources . length ) ;
sources = sources . slice ( 0 , separator ) ;
}
process . ARGV = flags ;
2010-02-16 23:59:32 -05:00
if ( options . watch ) {
watch _scripts ( ) ;
}
2010-02-13 10:16:28 -05:00
compile _scripts ( ) ;
2010-02-11 01:57:33 -05:00
return this ;
} ;
// The "--help" usage message.
2010-02-13 10:16:28 -05:00
usage = function usage ( ) {
2010-02-14 15:16:33 -05:00
puts ( '\n' + option _parser . help ( ) + '\n' ) ;
2010-02-11 01:57:33 -05:00
return process . exit ( 0 ) ;
} ;
// The "--version" message.
2010-02-13 10:16:28 -05:00
version = function version ( ) {
2010-02-11 01:57:33 -05:00
puts ( "CoffeeScript version " + coffee . VERSION ) ;
return process . exit ( 0 ) ;
} ;
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
// or JSLint results.
2010-02-13 10:16:28 -05:00
compile _scripts = function compile _scripts ( ) {
2010-02-21 19:59:27 -05:00
var _a , _b , _c , compile , source ;
compile = function compile ( source ) {
return fs . readFile ( source , function ( err , code ) {
return compile _script ( source , code ) ;
} ) ;
} ;
_a = [ ] ; _b = sources ;
for ( _c = 0 ; _c < _b . length ; _c ++ ) {
source = _b [ _c ] ;
_a . push ( compile ( source ) ) ;
2010-02-11 01:57:33 -05:00
}
2010-02-21 19:59:27 -05:00
return _a ;
2010-02-16 23:59:32 -05:00
} ;
// Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method.
compile _script = function compile _script ( source , code ) {
2010-02-24 18:56:32 -05:00
var js , o ;
o = options ;
2010-02-16 23:59:32 -05:00
try {
2010-02-24 18:56:32 -05:00
if ( o . tokens ) {
return print _tokens ( coffee . tokenize ( code ) ) ;
} else if ( o . tree ) {
2010-02-16 23:59:32 -05:00
return puts ( coffee . tree ( code ) . toString ( ) ) ;
2010-02-12 23:09:57 -05:00
} else {
2010-02-24 18:18:29 -05:00
js = coffee . compile ( code , compile _options ( ) ) ;
2010-02-24 18:56:32 -05:00
if ( o . run ) {
2010-02-16 23:59:32 -05:00
return eval ( js ) ;
2010-02-24 18:56:32 -05:00
} else if ( o . lint ) {
2010-02-16 23:59:32 -05:00
return lint ( js ) ;
2010-02-24 18:56:32 -05:00
} else if ( o . print || o . eval ) {
2010-02-21 22:41:19 -05:00
return puts ( js ) ;
2010-02-13 09:39:25 -05:00
} else {
2010-02-19 18:29:24 -05:00
return write _js ( source , js ) ;
2010-02-13 09:39:25 -05:00
}
2010-02-12 23:09:57 -05:00
}
2010-02-16 23:59:32 -05:00
} catch ( err ) {
2010-02-24 18:56:32 -05:00
if ( o . watch ) {
2010-02-16 23:59:32 -05:00
return puts ( err . message ) ;
} else {
throw err ;
}
}
} ;
2010-02-24 18:18:29 -05:00
// Listen for and compile scripts over stdio.
compile _stdio = function compile _stdio ( ) {
var code ;
code = '' ;
process . stdio . open ( ) ;
process . stdio . addListener ( 'data' , function ( string ) {
if ( string ) {
return code += string ;
}
} ) ;
return process . stdio . addListener ( 'close' , function ( ) {
return process . stdio . write ( coffee . compile ( code , compile _options ( ) ) ) ;
} ) ;
} ;
2010-02-16 23:59:32 -05:00
// Watch a list of source CoffeeScript files, recompiling them every time the
// files are updated.
watch _scripts = function watch _scripts ( ) {
2010-02-21 19:59:27 -05:00
var _a , _b , _c , source , watch ;
watch = function watch ( source ) {
return process . watchFile ( source , {
2010-02-16 23:59:32 -05:00
persistent : true ,
interval : 500
} , function ( curr , prev ) {
if ( curr . mtime . getTime ( ) === prev . mtime . getTime ( ) ) {
return null ;
}
2010-02-21 14:06:01 -05:00
return fs . readFile ( source , function ( err , code ) {
2010-02-16 23:59:32 -05:00
return compile _script ( source , code ) ;
} ) ;
2010-02-21 19:59:27 -05:00
} ) ;
} ;
_a = [ ] ; _b = sources ;
for ( _c = 0 ; _c < _b . length ; _c ++ ) {
source = _b [ _c ] ;
_a . push ( watch ( source ) ) ;
2010-02-16 23:59:32 -05:00
}
return _a ;
2010-02-11 01:57:33 -05:00
} ;
2010-02-12 22:59:21 -05:00
// Write out a JavaScript source file with the compiled code.
2010-02-13 10:16:28 -05:00
write _js = function write _js ( source , js ) {
2010-02-12 22:59:21 -05:00
var dir , filename , js _path ;
filename = path . basename ( source , path . extname ( source ) ) + '.js' ;
2010-02-13 10:16:28 -05:00
dir = options . output || path . dirname ( source ) ;
2010-02-12 22:59:21 -05:00
js _path = path . join ( dir , filename ) ;
2010-02-19 18:27:50 -05:00
return fs . writeFile ( js _path , js ) ;
2010-02-12 22:59:21 -05:00
} ;
2010-02-12 23:09:57 -05:00
// Pipe compiled JS through JSLint (requires a working 'jsl' command).
2010-02-13 10:16:28 -05:00
lint = function lint ( js ) {
2010-02-12 23:09:57 -05:00
var jsl ;
jsl = process . createChildProcess ( 'jsl' , [ '-nologo' , '-stdin' ] ) ;
jsl . addListener ( 'output' , function ( result ) {
if ( result ) {
return puts ( result . replace ( /\n/g , '' ) ) ;
}
} ) ;
2010-02-12 23:10:51 -05:00
jsl . addListener ( 'error' , function ( result ) {
2010-02-12 23:09:57 -05:00
if ( result ) {
return puts ( result ) ;
}
} ) ;
jsl . write ( js ) ;
return jsl . close ( ) ;
} ;
2010-02-24 18:56:32 -05:00
// Pretty-print a token stream.
print _tokens = function print _tokens ( tokens ) {
var _a , _b , _c , strings , token ;
strings = ( function ( ) {
_a = [ ] ; _b = tokens ;
for ( _c = 0 ; _c < _b . length ; _c ++ ) {
token = _b [ _c ] ;
_a . push ( '[' + token [ 0 ] + ' ' + token [ 1 ] . toString ( ) . replace ( /\n/ , '\\n' ) + ']' ) ;
}
return _a ;
} ) . call ( this ) ;
return puts ( strings . join ( ' ' ) ) ;
} ;
2010-02-11 01:57:33 -05:00
// Use OptionParser for all the options.
2010-02-13 10:16:28 -05:00
parse _options = function parse _options ( ) {
2010-02-24 17:57:58 -05:00
option _parser = new optparse . OptionParser ( SWITCHES , BANNER ) ;
options = option _parser . parse ( process . ARGV ) ;
return sources = options . arguments . slice ( 2 , options . arguments . length ) ;
2010-02-11 01:57:33 -05:00
} ;
2010-02-24 18:18:29 -05:00
// The options to pass to the CoffeeScript compiler.
compile _options = function compile _options ( ) {
return options [ 'no-wrap' ] ? {
no _wrap : true
} : { } ;
} ;
2010-02-24 18:56:32 -05:00
} ) ( ) ;