2010-02-11 06:57:33 +00:00
( function ( ) {
2010-02-25 03:30:22 +00: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-02-16 00:08:14 +00:00
fs = require ( 'fs' ) ;
2010-02-13 03:59:21 +00:00
path = require ( 'path' ) ;
2010-02-14 20:16:33 +00:00
optparse = require ( 'optparse' ) ;
2010-02-25 03:30:22 +00:00
CoffeeScript = require ( 'coffee-script' ) ;
2010-02-11 06:57:33 +00:00
BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee" ;
2010-02-27 05:38:04 +00: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-02-13 15:16:28 +00:00
options = { } ;
sources = [ ] ;
option _parser = null ;
2010-02-11 06:57:33 +00:00
// The CommandLine handles all of the functionality of the `coffee` utility.
exports . run = function run ( ) {
2010-02-16 06:04:48 +00:00
var flags , separator ;
2010-02-13 15:16:28 +00:00
parse _options ( ) ;
2010-02-24 22:57:58 +00:00
if ( options . help ) {
return usage ( ) ;
}
if ( options . version ) {
return version ( ) ;
}
2010-02-13 15:16:28 +00:00
if ( options . interactive ) {
2010-02-17 05:50:08 +00:00
return require ( 'repl' ) ;
2010-02-13 15:07:59 +00:00
}
2010-02-24 23:18:29 +00:00
if ( options . stdio ) {
return compile _stdio ( ) ;
}
2010-02-14 04:27:13 +00:00
if ( options . eval ) {
2010-02-24 22:57:58 +00:00
return compile _script ( 'unknown' , sources [ 0 ] ) ;
2010-02-14 04:27:13 +00:00
}
2010-02-13 15:16:28 +00:00
if ( ! ( sources . length ) ) {
2010-02-24 22:57:58 +00:00
return usage ( ) ;
2010-02-13 15:16:28 +00:00
}
2010-02-16 06:04:48 +00: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-17 04:59:32 +00:00
if ( options . watch ) {
watch _scripts ( ) ;
}
2010-02-13 15:16:28 +00:00
compile _scripts ( ) ;
2010-02-11 06:57:33 +00:00
return this ;
} ;
// The "--help" usage message.
2010-02-13 15:16:28 +00:00
usage = function usage ( ) {
2010-02-14 20:16:33 +00:00
puts ( '\n' + option _parser . help ( ) + '\n' ) ;
2010-02-11 06:57:33 +00:00
return process . exit ( 0 ) ;
} ;
// The "--version" message.
2010-02-13 15:16:28 +00:00
version = function version ( ) {
2010-02-25 03:30:22 +00:00
puts ( "CoffeeScript version " + CoffeeScript . VERSION ) ;
2010-02-11 06:57:33 +00:00
return process . exit ( 0 ) ;
} ;
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
// or JSLint results.
2010-02-13 15:16:28 +00:00
compile _scripts = function compile _scripts ( ) {
2010-02-26 04:39:14 +00:00
var _a , _b , _c , _d , compile , source ;
2010-02-22 00:59:27 +00:00
compile = function compile ( source ) {
2010-02-25 23:36:43 +00:00
return path . exists ( source , function ( exists ) {
if ( ! ( exists ) ) {
throw new Error ( 'File not found: ' + source ) ;
}
return fs . readFile ( source , function ( err , code ) {
return compile _script ( source , code ) ;
} ) ;
2010-02-22 00:59:27 +00:00
} ) ;
} ;
_a = [ ] ; _b = sources ;
2010-02-26 04:39:14 +00:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-22 00:59:27 +00:00
source = _b [ _c ] ;
_a . push ( compile ( source ) ) ;
2010-02-11 06:57:33 +00:00
}
2010-02-22 00:59:27 +00:00
return _a ;
2010-02-17 04:59:32 +00: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 23:56:32 +00:00
var js , o ;
o = options ;
2010-02-17 04:59:32 +00:00
try {
2010-02-24 23:56:32 +00:00
if ( o . tokens ) {
2010-02-25 23:42:35 +00:00
return print _tokens ( CoffeeScript . tokens ( code ) ) ;
} else if ( o . nodes ) {
return puts ( CoffeeScript . nodes ( code ) . toString ( ) ) ;
2010-02-13 04:09:57 +00:00
} else {
2010-02-25 03:30:22 +00:00
js = CoffeeScript . compile ( code , compile _options ( ) ) ;
2010-02-27 05:38:04 +00:00
if ( o . compile ) {
return write _js ( source , js ) ;
2010-02-24 23:56:32 +00:00
} else if ( o . lint ) {
2010-02-17 04:59:32 +00:00
return lint ( js ) ;
2010-02-24 23:56:32 +00:00
} else if ( o . print || o . eval ) {
2010-02-25 01:41:56 +00:00
return print ( js ) ;
2010-02-13 14:39:25 +00:00
} else {
2010-02-27 05:38:04 +00:00
return eval ( js ) ;
2010-02-13 14:39:25 +00:00
}
2010-02-13 04:09:57 +00:00
}
2010-02-17 04:59:32 +00:00
} catch ( err ) {
2010-02-24 23:56:32 +00:00
if ( o . watch ) {
2010-02-17 04:59:32 +00:00
return puts ( err . message ) ;
} else {
throw err ;
}
}
} ;
2010-02-24 23:18:29 +00: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 ( ) {
2010-02-25 03:30:22 +00:00
return process . stdio . write ( CoffeeScript . compile ( code , compile _options ( ) ) ) ;
2010-02-24 23:18:29 +00:00
} ) ;
} ;
2010-02-17 04:59:32 +00:00
// Watch a list of source CoffeeScript files, recompiling them every time the
// files are updated.
watch _scripts = function watch _scripts ( ) {
2010-02-26 04:39:14 +00:00
var _a , _b , _c , _d , source , watch ;
2010-02-22 00:59:27 +00:00
watch = function watch ( source ) {
return process . watchFile ( source , {
2010-02-17 04:59:32 +00:00
persistent : true ,
interval : 500
} , function ( curr , prev ) {
if ( curr . mtime . getTime ( ) === prev . mtime . getTime ( ) ) {
return null ;
}
2010-02-21 19:06:01 +00:00
return fs . readFile ( source , function ( err , code ) {
2010-02-17 04:59:32 +00:00
return compile _script ( source , code ) ;
} ) ;
2010-02-22 00:59:27 +00:00
} ) ;
} ;
_a = [ ] ; _b = sources ;
2010-02-26 04:39:14 +00:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-22 00:59:27 +00:00
source = _b [ _c ] ;
_a . push ( watch ( source ) ) ;
2010-02-17 04:59:32 +00:00
}
return _a ;
2010-02-11 06:57:33 +00:00
} ;
2010-02-13 03:59:21 +00:00
// Write out a JavaScript source file with the compiled code.
2010-02-13 15:16:28 +00:00
write _js = function write _js ( source , js ) {
2010-02-13 03:59:21 +00:00
var dir , filename , js _path ;
filename = path . basename ( source , path . extname ( source ) ) + '.js' ;
2010-02-13 15:16:28 +00:00
dir = options . output || path . dirname ( source ) ;
2010-02-13 03:59:21 +00:00
js _path = path . join ( dir , filename ) ;
2010-02-19 23:27:50 +00:00
return fs . writeFile ( js _path , js ) ;
2010-02-13 03:59:21 +00:00
} ;
2010-02-13 04:09:57 +00:00
// Pipe compiled JS through JSLint (requires a working 'jsl' command).
2010-02-13 15:16:28 +00:00
lint = function lint ( js ) {
2010-02-13 04:09:57 +00:00
var jsl ;
jsl = process . createChildProcess ( 'jsl' , [ '-nologo' , '-stdin' ] ) ;
jsl . addListener ( 'output' , function ( result ) {
if ( result ) {
return puts ( result . replace ( /\n/g , '' ) ) ;
}
} ) ;
2010-02-13 04:10:51 +00:00
jsl . addListener ( 'error' , function ( result ) {
2010-02-13 04:09:57 +00:00
if ( result ) {
return puts ( result ) ;
}
} ) ;
jsl . write ( js ) ;
return jsl . close ( ) ;
} ;
2010-02-24 23:56:32 +00:00
// Pretty-print a token stream.
print _tokens = function print _tokens ( tokens ) {
2010-02-26 04:39:14 +00:00
var _a , _b , _c , _d , strings , token ;
2010-02-24 23:56:32 +00:00
strings = ( function ( ) {
_a = [ ] ; _b = tokens ;
2010-02-26 04:39:14 +00:00
for ( _c = 0 , _d = _b . length ; _c < _d ; _c ++ ) {
2010-02-24 23:56:32 +00:00
token = _b [ _c ] ;
_a . push ( '[' + token [ 0 ] + ' ' + token [ 1 ] . toString ( ) . replace ( /\n/ , '\\n' ) + ']' ) ;
}
return _a ;
} ) . call ( this ) ;
return puts ( strings . join ( ' ' ) ) ;
} ;
2010-02-11 06:57:33 +00:00
// Use OptionParser for all the options.
2010-02-13 15:16:28 +00:00
parse _options = function parse _options ( ) {
2010-02-24 22:57:58 +00: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 06:57:33 +00:00
} ;
2010-02-24 23:18:29 +00:00
// The options to pass to the CoffeeScript compiler.
compile _options = function compile _options ( ) {
return options [ 'no-wrap' ] ? {
no _wrap : true
} : { } ;
} ;
2010-02-24 23:56:32 +00:00
} ) ( ) ;