2010-07-24 11:31:43 -04:00
( function ( ) {
2011-01-18 13:19:00 -05:00
var BANNER , CoffeeScript , EventEmitter , SWITCHES , compileJoin , compileOptions , compileScript , compileScripts , compileStdio , contents , exec , forkNode , fs , helpers , lint , loadRequires , optionParser , optparse , opts , parseOptions , path , printLine , printTokens , printWarn , sources , spawn , usage , version , watch , writeJs , _ref ;
2010-02-15 19:08:14 -05:00
fs = require ( 'fs' ) ;
2010-02-12 22:59:21 -05:00
path = require ( 'path' ) ;
2010-11-04 23:05:04 -04:00
helpers = require ( './helpers' ) ;
2010-03-15 23:39:46 -04:00
optparse = require ( './optparse' ) ;
CoffeeScript = require ( './coffee-script' ) ;
2010-09-28 16:47:12 -04:00
_ref = require ( 'child_process' ) , spawn = _ref . spawn , exec = _ref . exec ;
EventEmitter = require ( 'events' ) . EventEmitter ;
2010-09-25 04:39:19 -04:00
helpers . extend ( CoffeeScript , new EventEmitter ) ;
2010-11-28 08:57:01 -05:00
printLine = function ( line ) {
return process . stdout . write ( line + '\n' ) ;
} ;
printWarn = function ( line ) {
return process . binding ( 'stdio' ) . writeError ( line + '\n' ) ;
} ;
2010-11-04 23:05:04 -04:00
BANNER = 'Usage: coffee [options] path/to/script.coffee' ;
2010-12-23 00:26:15 -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' ] , [ '-j' , '--join' , 'concatenate the scripts before compiling' ] , [ '-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' ] , [ '-r' , '--require [FILE*]' , 'require a library before executing your script' ] , [ '-b' , '--bare' , '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' ] , [ '--nodejs [ARGS]' , 'pass options through to the "node" binary' ] , [ '-v' , '--version' , 'display CoffeeScript version' ] , [ '-h' , '--help' , 'display this help message' ] ] ;
2010-08-15 08:32:09 -04:00
opts = { } ;
2010-02-13 10:16:28 -05:00
sources = [ ] ;
2010-12-23 00:26:15 -05:00
contents = [ ] ;
2010-06-12 19:05:13 -04:00
optionParser = null ;
2010-05-14 23:40:04 -04:00
exports . run = function ( ) {
2010-06-12 19:05:13 -04:00
parseOptions ( ) ;
2010-12-18 09:29:04 -05:00
if ( opts . nodejs ) {
return forkNode ( ) ;
2010-12-17 02:39:39 -05:00
}
2010-08-15 08:32:09 -04:00
if ( opts . help ) {
2010-02-24 17:57:58 -05:00
return usage ( ) ;
}
2010-08-15 08:32:09 -04:00
if ( opts . version ) {
2010-02-24 17:57:58 -05:00
return version ( ) ;
}
2011-01-13 14:50:00 -05:00
if ( opts . require ) {
2011-01-15 10:46:53 -05:00
loadRequires ( ) ;
2011-01-13 14:50:00 -05:00
}
2010-08-15 08:32:09 -04:00
if ( opts . interactive ) {
2010-03-15 23:39:46 -04:00
return require ( './repl' ) ;
2010-02-13 10:07:59 -05:00
}
2010-08-15 08:32:09 -04:00
if ( opts . stdio ) {
2010-06-12 19:05:13 -04:00
return compileStdio ( ) ;
2010-02-24 18:18:29 -05:00
}
2010-08-15 08:32:09 -04:00
if ( opts . eval ) {
2010-10-30 17:35:54 -04:00
return compileScript ( null , sources [ 0 ] ) ;
2010-02-13 23:27:13 -05:00
}
2010-10-07 07:05:22 -04:00
if ( ! sources . length ) {
2010-06-12 11:09:30 -04:00
return require ( './repl' ) ;
2010-02-13 10:16:28 -05:00
}
2010-08-15 08:32:09 -04:00
if ( opts . run ) {
2010-12-18 09:29:04 -05:00
opts . literals = sources . splice ( 1 ) . concat ( opts . literals ) ;
2010-07-10 15:36:54 -04:00
}
2010-12-18 09:29:04 -05:00
process . ARGV = process . argv = process . argv . slice ( 0 , 2 ) . concat ( opts . literals ) ;
2010-06-12 19:05:13 -04:00
return compileScripts ( ) ;
2010-02-11 01:57:33 -05:00
} ;
2010-06-12 19:05:13 -04:00
compileScripts = function ( ) {
2010-12-21 15:45:46 -05:00
var base , compile , source , _i , _len , _results ;
_results = [ ] ;
for ( _i = 0 , _len = sources . length ; _i < _len ; _i ++ ) {
source = sources [ _i ] ;
2010-10-24 01:36:23 -04:00
base = path . join ( source ) ;
compile = function ( source , topLevel ) {
return path . exists ( source , function ( exists ) {
2010-12-28 20:46:54 -05:00
if ( topLevel && ! exists ) {
2010-10-24 01:36:23 -04:00
throw new Error ( "File not found: " + source ) ;
}
return fs . stat ( source , function ( err , stats ) {
if ( stats . isDirectory ( ) ) {
return fs . readdir ( source , function ( err , files ) {
2010-11-09 00:17:08 -05:00
var file , _i , _len , _results ;
_results = [ ] ;
2010-11-02 00:31:42 -04:00
for ( _i = 0 , _len = files . length ; _i < _len ; _i ++ ) {
file = files [ _i ] ;
2010-11-09 00:17:08 -05:00
_results . push ( compile ( path . join ( source , file ) ) ) ;
2010-10-24 01:36:23 -04:00
}
2010-11-09 00:17:08 -05:00
return _results ;
2010-10-24 01:36:23 -04:00
} ) ;
} else if ( topLevel || path . extname ( source ) === '.coffee' ) {
fs . readFile ( source , function ( err , code ) {
2010-12-23 00:26:15 -05:00
if ( opts . join ) {
contents [ sources . indexOf ( source ) ] = code . toString ( ) ;
if ( helpers . compact ( contents ) . length === sources . length ) {
return compileJoin ( ) ;
}
} else {
return compileScript ( source , code . toString ( ) , base ) ;
}
2010-10-24 01:36:23 -04:00
} ) ;
2010-12-24 01:22:34 -05:00
if ( opts . watch && ! opts . join ) {
2010-11-08 23:07:51 -05:00
return watch ( source , base ) ;
}
2010-10-13 07:29:22 -04:00
}
} ) ;
2010-10-24 01:36:23 -04:00
} ) ;
} ;
2010-12-21 15:45:46 -05:00
_results . push ( compile ( source , true ) ) ;
2010-02-11 01:57:33 -05:00
}
2010-11-09 00:17:08 -05:00
return _results ;
2010-02-16 23:59:32 -05:00
} ;
2010-08-15 08:32:09 -04:00
compileScript = function ( file , input , base ) {
2011-01-13 14:50:00 -05:00
var o , options , t , task ;
2010-08-15 08:32:09 -04:00
o = opts ;
options = compileOptions ( file ) ;
2010-02-16 23:59:32 -05:00
try {
2010-10-20 13:29:06 -04:00
t = task = {
2010-08-15 08:32:09 -04:00
file : file ,
input : input ,
options : options
2010-10-20 13:29:06 -04:00
} ;
2010-08-15 08:32:09 -04:00
CoffeeScript . emit ( 'compile' , task ) ;
2010-02-24 18:56:32 -05:00
if ( o . tokens ) {
2010-08-15 08:32:09 -04:00
return printTokens ( CoffeeScript . tokens ( t . input ) ) ;
2010-02-25 18:42:35 -05:00
} else if ( o . nodes ) {
2010-11-28 08:57:01 -05:00
return printLine ( CoffeeScript . nodes ( t . input ) . toString ( ) . trim ( ) ) ;
2010-03-07 21:49:08 -05:00
} else if ( o . run ) {
2010-08-15 08:32:09 -04:00
return CoffeeScript . run ( t . input , t . options ) ;
2010-02-12 23:09:57 -05:00
} else {
2010-08-15 08:32:09 -04:00
t . output = CoffeeScript . compile ( t . input , t . options ) ;
2010-08-12 18:28:38 -04:00
CoffeeScript . emit ( 'success' , task ) ;
2010-11-08 23:07:51 -05:00
if ( o . print ) {
2010-11-28 08:57:01 -05:00
return printLine ( t . output . trim ( ) ) ;
2010-11-08 23:07:51 -05:00
} else if ( o . compile ) {
return writeJs ( t . file , t . output , base ) ;
} else if ( o . lint ) {
return lint ( t . file , t . output ) ;
}
2010-02-12 23:09:57 -05:00
}
2010-02-16 23:59:32 -05:00
} catch ( err ) {
2010-08-12 18:28:38 -04:00
CoffeeScript . emit ( 'failure' , err , task ) ;
2010-08-07 23:33:35 -04:00
if ( CoffeeScript . listeners ( 'failure' ) . length ) {
2010-10-06 22:24:52 -04:00
return ;
2010-02-16 23:59:32 -05:00
}
2010-08-15 08:32:09 -04:00
if ( o . watch ) {
2010-11-28 08:57:01 -05:00
return printLine ( err . message ) ;
Add command-line compiler hooks. To invoke, pass a file after -r and listen for any of these events: 'compile', 'success' and 'exception'. Example:
coffee -e -r ./snarl 'Hello!'
Contents of 'snarl.coffee' in the working directory:
http = require 'http'
CoffeeScript.on 'exception', (err) ->
client = http.createClient 9889, 'localhost'
request = client.request 'GET', '/?d={"action":1,"applicationName":"CoffeeScript","title":' + JSON.stringify(err.message) + ',"description":' + JSON.stringify(err.stack) + ',"priority":3}'
request.end()
err.handled = yes
To examine arguments available for each event (for debugging and getting started), use `puts JSON.stringify arguments`.
See http://nodejs.org/api.html#modules-309 and NODE_PATH for more details on how -r looks for files.
2010-08-07 13:24:37 -04:00
}
2010-11-28 08:57:01 -05:00
printWarn ( err . stack ) ;
2010-08-15 08:32:09 -04:00
return process . exit ( 1 ) ;
2010-02-16 23:59:32 -05:00
}
} ;
2010-06-12 19:05:13 -04:00
compileStdio = function ( ) {
2010-04-10 18:05:35 -04:00
var code , stdin ;
2010-02-24 18:18:29 -05:00
code = '' ;
2010-04-10 18:05:35 -04:00
stdin = process . openStdin ( ) ;
2010-07-18 07:54:44 -04:00
stdin . on ( 'data' , function ( buffer ) {
2010-11-08 23:07:51 -05:00
if ( buffer ) {
return code += buffer . toString ( ) ;
}
2010-02-24 18:18:29 -05:00
} ) ;
2010-07-18 07:54:44 -04:00
return stdin . on ( 'end' , function ( ) {
2010-11-06 09:21:45 -04:00
return compileScript ( null , code ) ;
2010-02-24 18:18:29 -05:00
} ) ;
} ;
2010-12-23 00:26:15 -05:00
compileJoin = function ( ) {
var code ;
code = contents . join ( '\n' ) ;
2010-12-23 00:46:06 -05:00
return compileScript ( "concatenation" , code , "concatenation" ) ;
2010-12-23 00:26:15 -05:00
} ;
2011-01-15 10:46:53 -05:00
loadRequires = function ( ) {
var realFilename , req , _i , _len , _ref ;
realFilename = module . filename ;
module . filename = '.' ;
_ref = opts . require ;
for ( _i = 0 , _len = _ref . length ; _i < _len ; _i ++ ) {
req = _ref [ _i ] ;
require ( req ) ;
}
return module . filename = realFilename ;
} ;
2010-05-14 23:40:04 -04:00
watch = function ( source , base ) {
2010-05-03 17:38:59 -04:00
return fs . watchFile ( source , {
persistent : true ,
interval : 500
} , function ( curr , prev ) {
2010-09-08 21:08:17 -04:00
if ( curr . size === prev . size && curr . mtime . getTime ( ) === prev . mtime . getTime ( ) ) {
2010-10-06 22:24:52 -04:00
return ;
2010-05-03 17:38:59 -04:00
}
return fs . readFile ( source , function ( err , code ) {
2010-09-08 20:55:34 -04:00
if ( err ) {
throw err ;
}
2010-06-12 19:05:13 -04:00
return compileScript ( source , code . toString ( ) , base ) ;
2010-02-21 19:59:27 -05:00
} ) ;
2010-05-03 17:38:59 -04:00
} ) ;
2010-02-11 01:57:33 -05:00
} ;
2010-06-12 19:05:13 -04:00
writeJs = function ( source , js , base ) {
var baseDir , compile , dir , filename , jsPath , srcDir ;
2010-02-12 22:59:21 -05:00
filename = path . basename ( source , path . extname ( source ) ) + '.js' ;
2010-06-12 19:05:13 -04:00
srcDir = path . dirname ( source ) ;
2010-12-23 17:34:50 -05:00
baseDir = base === '.' ? srcDir : srcDir . substring ( base . length ) ;
2010-08-15 08:32:09 -04:00
dir = opts . output ? path . join ( opts . output , baseDir ) : srcDir ;
2010-06-12 19:05:13 -04:00
jsPath = path . join ( dir , filename ) ;
2010-05-14 23:40:04 -04:00
compile = function ( ) {
2010-08-04 23:42:46 -04:00
if ( js . length <= 0 ) {
js = ' ' ;
2010-08-04 21:36:03 -04:00
}
2010-06-13 14:21:02 -04:00
return fs . writeFile ( jsPath , js , function ( err ) {
2010-11-08 23:07:51 -05:00
if ( err ) {
2010-11-28 08:57:01 -05:00
return printLine ( err . message ) ;
2010-11-08 23:07:51 -05:00
} else if ( opts . compile && opts . watch ) {
2011-01-18 23:28:37 -05:00
return console . log ( "" + ( ( new Date ) . toTimeString ( ) ) + " - compiled " + source ) ;
2010-11-08 23:07:51 -05:00
}
2010-06-13 14:21:02 -04:00
} ) ;
2010-05-04 23:22:28 -04:00
} ;
return path . exists ( dir , function ( exists ) {
2010-11-08 23:07:51 -05:00
if ( exists ) {
return compile ( ) ;
} else {
return exec ( "mkdir -p " + dir , compile ) ;
}
2010-05-03 17:38:59 -04:00
} ) ;
2010-02-12 22:59:21 -05:00
} ;
2010-10-24 20:51:55 -04:00
lint = function ( file , js ) {
2010-08-14 16:02:01 -04:00
var conf , jsl , printIt ;
2010-06-12 19:05:13 -04:00
printIt = function ( buffer ) {
2010-11-28 08:57:01 -05:00
return printLine ( file + ':\t' + buffer . toString ( ) . trim ( ) ) ;
2010-04-10 18:05:35 -04:00
} ;
2010-08-14 16:02:01 -04:00
conf = _ _dirname + '/../extras/jsl.conf' ;
jsl = spawn ( 'jsl' , [ '-nologo' , '-stdin' , '-conf' , conf ] ) ;
2010-07-18 07:54:44 -04:00
jsl . stdout . on ( 'data' , printIt ) ;
jsl . stderr . on ( 'data' , printIt ) ;
2010-04-10 18:05:35 -04:00
jsl . stdin . write ( js ) ;
return jsl . stdin . end ( ) ;
2010-02-12 23:09:57 -05:00
} ;
2010-06-12 19:05:13 -04:00
printTokens = function ( tokens ) {
2010-12-13 21:24:32 -05:00
var strings , tag , token , value ;
2010-12-23 13:50:52 -05:00
strings = ( function ( ) {
2010-12-13 21:24:32 -05:00
var _i , _len , _ref , _results ;
2010-11-09 00:17:08 -05:00
_results = [ ] ;
2010-10-21 21:51:06 -04:00
for ( _i = 0 , _len = tokens . length ; _i < _len ; _i ++ ) {
2010-10-01 18:26:37 -04:00
token = tokens [ _i ] ;
2010-11-02 00:31:42 -04:00
_ref = [ token [ 0 ] , token [ 1 ] . toString ( ) . replace ( /\n/ , '\\n' ) ] , tag = _ref [ 0 ] , value = _ref [ 1 ] ;
2010-11-09 00:17:08 -05:00
_results . push ( "[" + tag + " " + value + "]" ) ;
2010-02-24 18:56:32 -05:00
}
2010-11-09 00:17:08 -05:00
return _results ;
2010-12-23 13:50:52 -05:00
} ) ( ) ;
2010-11-28 08:57:01 -05:00
return printLine ( strings . join ( ' ' ) ) ;
2010-02-24 18:56:32 -05:00
} ;
2010-06-12 19:05:13 -04:00
parseOptions = function ( ) {
2010-03-07 21:49:08 -05:00
var o ;
2010-12-18 09:29:04 -05:00
optionParser = new optparse . OptionParser ( SWITCHES , BANNER ) ;
2010-10-20 13:29:06 -04:00
o = opts = optionParser . parse ( process . argv . slice ( 2 ) ) ;
2010-10-01 18:17:35 -04:00
o . compile || ( o . compile = ! ! o . output ) ;
2010-08-15 08:32:09 -04:00
o . run = ! ( o . compile || o . print || o . lint ) ;
o . print = ! ! ( o . print || ( o . eval || o . stdio && o . compile ) ) ;
2010-12-18 09:29:04 -05:00
return sources = o . arguments ;
2010-02-11 01:57:33 -05:00
} ;
2011-01-15 10:46:53 -05:00
compileOptions = function ( filename ) {
2010-10-13 15:09:56 -04:00
return {
2011-01-15 10:46:53 -05:00
filename : filename ,
2010-12-18 09:29:04 -05:00
bare : opts . bare
2010-03-07 22:08:24 -05:00
} ;
2010-02-24 18:18:29 -05:00
} ;
2010-12-18 09:29:04 -05:00
forkNode = function ( ) {
var args , nodeArgs ;
nodeArgs = opts . nodejs . split ( /\s+/ ) ;
args = process . argv . slice ( 1 ) ;
args . splice ( args . indexOf ( '--nodejs' ) , 2 ) ;
return spawn ( process . execPath , nodeArgs . concat ( args ) , {
2010-12-17 02:39:39 -05:00
cwd : process . cwd ( ) ,
env : process . env ,
customFds : [ 0 , 1 , 2 ]
} ) ;
} ;
2010-05-14 23:40:04 -04:00
usage = function ( ) {
2010-12-26 20:15:55 -05:00
return printLine ( ( new optparse . OptionParser ( SWITCHES , BANNER ) ) . help ( ) ) ;
2010-03-07 00:28:58 -05:00
} ;
2010-05-14 23:40:04 -04:00
version = function ( ) {
2010-12-26 20:15:55 -05:00
return printLine ( "CoffeeScript version " + CoffeeScript . VERSION ) ;
2010-03-07 00:28:58 -05:00
} ;
2010-09-21 03:53:58 -04:00
} ) . call ( this ) ;