2010-02-11 06:57:33 +00:00
( function ( ) {
2010-02-13 03:59:21 +00:00
var BANNER , SWITCHES , WATCH _INTERVAL , coffee , optparse , path , posix ;
2010-02-11 06:57:33 +00:00
posix = require ( 'posix' ) ;
2010-02-13 03:59:21 +00:00
path = require ( 'path' ) ;
2010-02-11 06:57:33 +00:00
coffee = require ( 'coffee-script' ) ;
2010-02-13 03:59:21 +00:00
optparse = require ( './../../vendor/optparse-js/src/optparse' ) ;
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-12 04:11:05 +00: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' ] , [ '-e' , '--eval' , 'compile a cli scriptlet or read from stdin' ] , [ '-t' , '--tokens' , 'print the tokens that the lexer produces' ] , [ '--tree' , 'print the parse tree that Jison produces' ] , [ '-n' , '--no-wrap' , 'raw output, no function safety wrapper' ] , [ '-g' , '--globals' , 'attach all top-level variables as globals' ] , [ '-v' , '--version' , 'display CoffeeScript version' ] , [ '-h' , '--help' , 'display this help message' ] ] ;
2010-02-11 06:57:33 +00:00
WATCH _INTERVAL = 0.5 ;
// The CommandLine handles all of the functionality of the `coffee` utility.
exports . run = function run ( ) {
this . parse _options ( ) ;
this . compile _scripts ( ) ;
return this ;
} ;
// The "--help" usage message.
exports . usage = function usage ( ) {
puts ( '\n' + this . option _parser . toString ( ) + '\n' ) ;
return process . exit ( 0 ) ;
} ;
// The "--version" message.
exports . version = function version ( ) {
puts ( "CoffeeScript version " + coffee . VERSION ) ;
return process . exit ( 0 ) ;
} ;
// Compile a single source file to JavaScript.
exports . compile = function compile ( script , source ) {
var options ;
source = source || 'error' ;
2010-02-13 06:13:08 +00:00
options = { } ;
2010-02-11 06:57:33 +00:00
if ( this . options . no _wrap ) {
options . no _wrap = true ;
}
if ( this . options . globals ) {
options . globals = true ;
}
try {
return CoffeeScript . compile ( script , options ) ;
} catch ( error ) {
process . stdio . writeError ( source + ': ' + error . toString ( ) ) ;
if ( ! ( this . options . watch ) ) {
process . exit ( 1 ) ;
}
return null ;
}
} ;
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
// or JSLint results.
exports . compile _scripts = function compile _scripts ( ) {
2010-02-11 07:39:57 +00:00
var opts , source ;
2010-02-11 06:57:33 +00:00
if ( ! ( ( source = this . sources . shift ( ) ) ) ) {
return null ;
}
2010-02-11 07:39:57 +00:00
opts = this . options ;
return posix . cat ( source ) . addCallback ( function ( code ) {
2010-02-13 04:09:57 +00:00
var js ;
if ( opts . tokens ) {
puts ( coffee . tokenize ( code ) . join ( ' ' ) ) ;
} else {
opts . tree ? puts ( coffee . tree ( code ) . toString ( ) ) : ( js = coffee . compile ( code ) ) ;
opts . run ? eval ( js ) : opts . print ? puts ( js ) : opts . lint ? exports . lint ( js ) : exports . write _js ( source , coffee . compile ( code ) ) ;
}
2010-02-11 07:39:57 +00:00
return exports . compile _scripts ( ) ;
} ) ;
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.
exports . write _js = function write _js ( source , js ) {
var dir , filename , js _path ;
filename = path . basename ( source , path . extname ( source ) ) + '.js' ;
dir = this . options . output || path . dirname ( source ) ;
js _path = path . join ( dir , filename ) ;
2010-02-13 06:04:53 +00:00
return posix . open ( js _path , process . O _CREAT | process . O _WRONLY | process . O _TRUNC , parseInt ( '0755' , 8 ) ) . addCallback ( function ( fd ) {
2010-02-13 03:59:21 +00:00
return posix . write ( fd , js ) ;
} ) ;
} ;
2010-02-13 04:09:57 +00:00
// Pipe compiled JS through JSLint (requires a working 'jsl' command).
exports . lint = function lint ( js ) {
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-11 06:57:33 +00:00
// Use OptionParser for all the options.
exports . parse _options = function parse _options ( ) {
var oparser , opts , paths ;
2010-02-13 06:13:08 +00:00
opts = ( this . options = { } ) ;
2010-02-11 06:57:33 +00:00
oparser = ( this . option _parser = new optparse . OptionParser ( SWITCHES ) ) ;
oparser . add = oparser [ 'on' ] ;
oparser . add ( 'interactive' , function ( ) {
return opts . interactive = true ;
} ) ;
oparser . add ( 'run' , function ( ) {
return opts . run = true ;
} ) ;
2010-02-13 03:59:21 +00:00
oparser . add ( 'output' , function ( n , dir ) {
2010-02-11 06:57:33 +00:00
return opts . output = dir ;
} ) ;
oparser . add ( 'watch' , function ( ) {
return opts . watch = true ;
} ) ;
oparser . add ( 'print' , function ( ) {
return opts . print = true ;
} ) ;
oparser . add ( 'lint' , function ( ) {
return opts . lint = true ;
} ) ;
oparser . add ( 'eval' , function ( ) {
return opts . eval = true ;
} ) ;
oparser . add ( 'tokens' , function ( ) {
return opts . tokens = true ;
} ) ;
2010-02-12 04:11:05 +00:00
oparser . add ( 'tree' , function ( ) {
return opts . tree = true ;
} ) ;
2010-02-11 06:57:33 +00:00
oparser . add ( 'help' , ( function ( _ _this ) {
var _ _func = function ( ) {
return this . usage ( ) ;
} ;
return ( function ( ) {
return _ _func . apply ( _ _this , arguments ) ;
} ) ;
} ) ( this ) ) ;
oparser . add ( 'version' , ( function ( _ _this ) {
var _ _func = function ( ) {
return this . version ( ) ;
} ;
return ( function ( ) {
return _ _func . apply ( _ _this , arguments ) ;
} ) ;
} ) ( this ) ) ;
paths = oparser . parse ( process . ARGV ) ;
return this . sources = paths . slice ( 2 , paths . length ) ;
} ;
} ) ( ) ;