Command-line tools for compiling, running, and building CoffeeScript projects. CoffeeScript provides two main CLI utilities: coffee for compilation and execution, and cake for build automation.
Main command-line interface for compiling and running CoffeeScript files.
/**
* Main command line interface function
* Handles compilation, execution, and various CoffeeScript operations
*/
function run();Binary Location: /bin/coffee
Module: lib/coffee-script/command.js
Usage Examples:
# Execute a CoffeeScript file
coffee script.coffee
# Compile to JavaScript
coffee -c script.coffee
# Compile and output to specific directory
coffee -c -o js/ src/
# Watch files for changes and recompile
coffee -w -c -o js/ src/
# Run interactive REPL
coffee -i
# Print compiled output to stdout
coffee -p script.coffee
# Compile with source maps
coffee -m -c script.coffee
# Compile without function wrapper
coffee -b -c script.coffee
# Join multiple files before compiling
coffee -j compiled.js -c file1.coffee file2.coffee
# Print tokens (for debugging)
coffee -t script.coffee
# Print AST nodes (for debugging)
coffee -n script.coffee
# Evaluate string directly
coffee -e "console.log 'Hello from CoffeeScript!'"
# Pass options to Node.js
coffee --nodejs --harmony script.coffeeCommand Options:
const SWITCHES = [
['-b', '--bare', 'compile without a top-level function wrapper'],
['-c', '--compile', 'compile to JavaScript and save as .js files'],
['-e', '--eval', 'pass a string from the command line as input'],
['-h', '--help', 'display this help message'],
['-i', '--interactive', 'run an interactive CoffeeScript REPL'],
['-j', '--join [FILE]', 'concatenate the source CoffeeScript before compiling'],
['-m', '--map', 'generate source map and save as .js.map files'],
['-M', '--inline-map', 'generate source map and include it directly in output'],
['-n', '--nodes', 'print out the parse tree that the parser produces'],
['--nodejs [ARGS]', 'pass options directly to the "node" binary'],
['--no-header', 'suppress the "Generated by" header'],
['-o', '--output [DIR]', 'set the output directory for compiled JavaScript'],
['-p', '--print', 'print out the compiled JavaScript'],
['-r', '--require [MODULE*]', 'require the given module before eval or REPL'],
['-s', '--stdio', 'listen for and compile scripts over stdio'],
['-l', '--literate', 'treat stdio as literate style coffee-script'],
['-t', '--tokens', 'print out the tokens that the lexer/rewriter produce'],
['-v', '--version', 'display the version number'],
['-w', '--watch', 'watch scripts for changes and rerun commands']
];Build automation tool for CoffeeScript projects, similar to Make or Rake.
/**
* Main function for cake build tool
* Executes build tasks defined in Cakefile
*/
function run();Binary Location: /bin/cake
Module: lib/coffee-script/cake.js
Usage Examples:
# List available tasks
cake
# Run a specific task
cake build
# Run task with options
cake deploy --production
# Show task help
cake --helpCakefile Example:
# Cakefile - build automation for CoffeeScript projects
fs = require 'fs'
{exec} = require 'child_process'
task 'build', 'Build project', ->
console.log 'Building project...'
exec 'coffee -c -o lib/ src/', (err, stdout, stderr) ->
throw err if err
console.log 'Build complete!'
task 'test', 'Run tests', ->
console.log 'Running tests...'
exec 'npm test', (err, stdout, stderr) ->
throw err if err
console.log stdout
task 'clean', 'Clean build artifacts', ->
console.log 'Cleaning...'
exec 'rm -rf lib/', (err) ->
throw err if err
console.log 'Clean complete!'
option '-p', '--production', 'Production build'
task 'deploy', 'Deploy application', (options) ->
if options.production
console.log 'Production deployment...'
else
console.log 'Development deployment...'Global Functions Available in Cakefile:
/**
* Define a build task
* @param name - Task name
* @param description - Task description
* @param action - Task function to execute
*/
function task(name, description, action);
/**
* Define a command line option
* @param letter - Short option (-p)
* @param flag - Long option (--production)
* @param description - Option description
*/
function option(letter, flag, description);
/**
* Invoke another task by name
* @param name - Task name to invoke
*/
function invoke(name);The coffee command supports file watching for automatic recompilation during development.
Watch Examples:
# Watch single file
coffee -w -c script.coffee
# Watch directory
coffee -w -c -o js/ src/
# Watch with multiple options
coffee -w -c -m -o build/ --bare src/
# Watch and execute (useful for development)
coffee -w script.coffeeWatch Features:
Launch an interactive CoffeeScript Read-Eval-Print Loop for experimentation and debugging.
# Start REPL
coffee -i
# Start REPL with required modules
coffee -i -r lodash -r momentREPL Features:
Standard Compilation:
coffee -c script.coffee # Creates script.jsBare Compilation (no wrapper function):
coffee -b -c script.coffeeSource Map Generation:
coffee -m -c script.coffee # Creates script.js and script.js.map
coffee -M -c script.coffee # Inline source map in script.jsOutput Directory:
coffee -c -o build/ src/ # Output to build/ directoryFile Joining:
coffee -j bundle.js -c file1.coffee file2.coffee file3.coffeeProcess CoffeeScript code through standard input/output pipes:
# Compile from stdin
echo "console.log 'hello'" | coffee -cs
# Process literate CoffeeScript
cat README.litcoffee | coffee -csl
# Use in shell pipelines
find . -name "*.coffee" | xargs coffee -cToken Inspection:
coffee -t script.coffee # Show lexical tokensAST Inspection:
coffee -n script.coffee # Show parse treeVersion Information:
coffee -v # Show CoffeeScript versionThe CLI tools integrate seamlessly with the CoffeeScript compilation API:
const CoffeeScript = require('coffee-script');
// The CLI essentially calls these API functions:
const fs = require('fs');
// For compilation:
const source = fs.readFileSync('script.coffee', 'utf8');
const compiled = CoffeeScript.compile(source, {
filename: 'script.coffee',
bare: false,
header: true
});
fs.writeFileSync('script.js', compiled);
// For execution:
CoffeeScript.run(source, { filename: 'script.coffee' });npm scripts integration:
{
"scripts": {
"build": "coffee -c -o lib/ src/",
"watch": "coffee -w -c -o lib/ src/",
"dev": "coffee -w app.coffee"
}
}Makefile integration:
build:
coffee -c -o lib/ src/
watch:
coffee -w -c -o lib/ src/
clean:
rm -rf lib/
.PHONY: build watch clean