2010-02-11 01:57:33 -05:00
( function ( ) {
2010-02-24 22:30:22 -05:00
var BANNER , CoffeeScript , SWITCHES , 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-03-06 23:48:06 -05:00
// The `coffee` utility. Handles command-line compilation of CoffeeScript
// into various forms: saved into `.js` files or printed to stdout, piped to
2010-03-07 00:28:58 -05:00
// [JSLint](http://javascriptlint.com/) or recompiled every time the source is
// saved, printed as a token stream or as the syntax tree, or launch an
// interactive REPL.
2010-03-06 23:48:06 -05:00
// External dependencies.
2010-02-15 19:08:14 -05:00
fs = require ( 'fs' ) ;
2010-02-12 22:59:21 -05:00
path = require ( 'path' ) ;
2010-03-15 23:39:46 -04:00
optparse = require ( './optparse' ) ;
CoffeeScript = require ( './coffee-script' ) ;
2010-03-07 00:28:58 -05:00
// The help banner that is printed when `coffee` is called without arguments.
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-03-07 00:28:58 -05:00
// The list of all the valid option flags that `coffee` knows how to handle.
2010-02-27 00:38:04 -05:00
SWITCHES = [ [ '-c' , '--compile' , 'compile to JavaScript and save as .js files' ] , [ '-i' , '--interactive' , 'run an interactive CoffeeScript REPL' ] , [ '-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' ] , [ '--no-wrap' , 'compile without the top-level function wrapper' ] , [ '-t' , '--tokens' , 'print the tokens that the lexer produces' ] , [ '-n' , '--nodes' , 'print the parse tree that Jison produces' ] , [ '-v' , '--version' , 'display CoffeeScript version' ] , [ '-h' , '--help' , 'display this help message' ] ] ;
2010-03-07 00:28:58 -05:00
// Top-level objects shared by all the functions.
2010-02-13 10:16:28 -05:00
options = { } ;
sources = [ ] ;
option _parser = null ;
2010-03-07 00:28:58 -05:00
// Run `coffee` by parsing passed options and determining what action to take.
// Many flags cause us to divert before compiling anything. Flags passed after
2010-03-07 13:41:15 -05:00
// `--` will be passed verbatim to your script as arguments in `process.argv`
2010-02-11 01:57:33 -05:00
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-03-15 23:39:46 -04: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-03-07 22:17:45 -05:00
return compile _script ( 'console' , 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 ) ;
}
2010-03-06 20:09:08 -05:00
process . ARGV = ( process . argv = flags ) ;
2010-02-16 23:59:32 -05:00
if ( options . watch ) {
watch _scripts ( ) ;
}
2010-03-07 00:28:58 -05:00
return compile _scripts ( ) ;
2010-02-11 01:57:33 -05:00
} ;
2010-03-07 00:28:58 -05:00
// Asynchronously read in each CoffeeScript in a list of source files and
// compile them.
2010-02-13 10:16:28 -05:00
compile _scripts = function compile _scripts ( ) {
2010-02-25 23:39:14 -05:00
var _a , _b , _c , _d , compile , source ;
2010-02-21 19:59:27 -05:00
compile = function compile ( source ) {
2010-02-25 18:36:43 -05:00
return path . exists ( source , function ( exists ) {
if ( ! ( exists ) ) {
2010-03-06 13:59:11 -05:00
throw new Error ( "File not found: " + source ) ;
2010-02-25 18:36:43 -05:00
}
return fs . readFile ( source , function ( err , code ) {
return compile _script ( source , code ) ;
} ) ;
2010-02-21 19:59:27 -05:00
} ) ;
} ;
_a = [ ] ; _b = sources ;
2010-02-25 23:39:14 -05:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-21 19:59:27 -05:00
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
2010-03-07 00:28:58 -05:00
// requested options. Both compile_scripts and watch_scripts share this method
// in common. If evaluating the script directly sets `__filename`, `__dirname`
// and `module.filename` to be correct relative to the script's path.
2010-02-16 23:59:32 -05:00
compile _script = function compile _script ( source , code ) {
2010-03-07 22:17:45 -05:00
var code _opts , js , o ;
2010-02-24 18:56:32 -05:00
o = options ;
2010-03-07 22:17:45 -05:00
code _opts = compile _options ( source ) ;
2010-02-16 23:59:32 -05:00
try {
2010-02-24 18:56:32 -05:00
if ( o . tokens ) {
2010-02-25 18:42:35 -05:00
return print _tokens ( CoffeeScript . tokens ( code ) ) ;
} else if ( o . nodes ) {
return puts ( CoffeeScript . nodes ( code ) . toString ( ) ) ;
2010-03-07 21:49:08 -05:00
} else if ( o . run ) {
2010-03-07 22:17:45 -05:00
return CoffeeScript . run ( code , code _opts ) ;
2010-02-12 23:09:57 -05:00
} else {
2010-03-07 22:17:45 -05:00
js = CoffeeScript . compile ( code , code _opts ) ;
2010-03-08 04:46:24 -05:00
if ( o . print ) {
return process . stdio . write ( js ) ;
} else if ( o . compile ) {
2010-02-27 00:38:04 -05:00
return write _js ( source , 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-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-03-07 00:28:58 -05:00
// Attach the appropriate listeners to compile scripts incoming over **stdin**,
// and write them back to **stdout**.
2010-02-24 18:18:29 -05:00
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 ( ) {
2010-03-08 04:46:24 -05:00
return compile _script ( 'stdio' , code ) ;
2010-02-24 18:18:29 -05:00
} ) ;
} ;
2010-03-07 00:28:58 -05:00
// Watch a list of source CoffeeScript files using `fs.watchFile`, recompiling
// them every time the files are updated. May be used in combination with other
// options, such as `--lint` or `--print`.
2010-02-16 23:59:32 -05:00
watch _scripts = function watch _scripts ( ) {
2010-02-25 23:39:14 -05:00
var _a , _b , _c , _d , source , watch ;
2010-02-21 19:59:27 -05:00
watch = function watch ( source ) {
2010-03-03 08:32:28 -05:00
return fs . 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 ;
2010-02-25 23:39:14 -05:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-21 19:59:27 -05:00
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-03-07 00:28:58 -05:00
// Write out a JavaScript source file with the compiled code. By default, files
// are written out in `cwd` as `.js` files with the same name, but the output
// directory can be customized with `--output`.
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-03-07 00:28:58 -05:00
// Pipe compiled JS through JSLint (requires a working `jsl` command), printing
// any errors or warnings that arise.
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-03-07 00:28:58 -05:00
// Pretty-print a stream of tokens.
2010-02-24 18:56:32 -05:00
print _tokens = function print _tokens ( tokens ) {
2010-03-06 13:59:11 -05:00
var _a , _b , _c , _d , _e , strings , tag , token , value ;
2010-02-24 18:56:32 -05:00
strings = ( function ( ) {
_a = [ ] ; _b = tokens ;
2010-02-25 23:39:14 -05:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-24 18:56:32 -05:00
token = _b [ _c ] ;
2010-03-06 13:59:11 -05:00
_a . push ( ( function ( ) {
_e = [ token [ 0 ] , token [ 1 ] . toString ( ) . replace ( /\n/ , '\\n' ) ] ;
tag = _e [ 0 ] ;
value = _e [ 1 ] ;
return "[" + tag + " " + value + "]" ;
} ) . call ( this ) ) ;
2010-02-24 18:56:32 -05:00
}
return _a ;
} ) . call ( this ) ;
return puts ( strings . join ( ' ' ) ) ;
} ;
2010-03-07 00:28:58 -05:00
// Use the [OptionParser module](optparse.html) to extract all options from
2010-03-07 13:41:15 -05:00
// `process.argv` that are specified in `SWITCHES`.
2010-02-13 10:16:28 -05:00
parse _options = function parse _options ( ) {
2010-03-07 21:49:08 -05:00
var o ;
2010-02-24 17:57:58 -05:00
option _parser = new optparse . OptionParser ( SWITCHES , BANNER ) ;
2010-03-07 21:49:08 -05:00
o = ( options = option _parser . parse ( process . argv ) ) ;
2010-03-08 04:46:24 -05:00
options . run = ! ( o . compile || o . print || o . lint ) ;
options . print = ! ! ( o . print || ( o . eval || o . stdio && o . compile ) ) ;
2010-02-24 17:57:58 -05:00
return sources = options . arguments . slice ( 2 , options . arguments . length ) ;
2010-02-11 01:57:33 -05:00
} ;
2010-03-07 00:28:58 -05:00
// The compile-time options to pass to the CoffeeScript compiler.
2010-03-07 22:08:24 -05:00
compile _options = function compile _options ( source ) {
var o ;
o = {
source : source
} ;
2010-03-08 04:28:26 -05:00
o [ 'no_wrap' ] = options [ 'no-wrap' ] ;
2010-03-07 22:08:24 -05:00
return o ;
2010-02-24 18:18:29 -05:00
} ;
2010-03-07 00:28:58 -05:00
// Print the `--help` usage message and exit.
usage = function usage ( ) {
puts ( option _parser . help ( ) ) ;
return process . exit ( 0 ) ;
} ;
2010-03-07 11:41:56 -05:00
// Print the `--version` message and exit.
2010-03-07 00:28:58 -05:00
version = function version ( ) {
puts ( "CoffeeScript version " + ( CoffeeScript . VERSION ) ) ;
return process . exit ( 0 ) ;
} ;
2010-02-24 18:56:32 -05:00
} ) ( ) ;