or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

ast-nodes.mdcli-tools.mdcompilation.mdexecution.mdindex.mdrepl-interactive.mdutilities.md
tile.json

repl-interactive.mddocs/

REPL and Interactive Features

Interactive CoffeeScript Read-Eval-Print Loop and runtime registration for seamless integration with Node.js environments.

Capabilities

REPL Start Function

Starts an interactive CoffeeScript REPL session with customizable options and enhanced features.

/**
 * Start interactive CoffeeScript REPL session
 * @param opts - REPL configuration options
 * @returns REPL instance
 */
function start(opts);

Parameters:

  • opts (object, optional): REPL configuration options

Returns:

  • REPL instance with CoffeeScript evaluation capabilities

Usage Examples:

const repl = require('coffee-script/repl');

// Start basic REPL
repl.start();

// Start REPL with custom options
repl.start({
  prompt: 'coffee> ',
  eval: customEvalFunction,
  historyFile: './my_coffee_history'
});

// Start REPL from command line
// coffee -i

File Registration

Enable Node.js to require CoffeeScript files directly by registering appropriate file extensions.

/**
 * Register CoffeeScript file extensions with Node.js
 * Enables require() calls for .coffee, .litcoffee, and .coffee.md files
 */
function register();

Usage Examples:

// Method 1: Using the main API
const CoffeeScript = require('coffee-script');
CoffeeScript.register();

// Method 2: Using the register entry point
require('coffee-script/register');

// Method 3: Using the root register file
require('coffee-script').register();

// Now you can require CoffeeScript files directly
const myModule = require('./my-script.coffee');
const literate = require('./documentation.litcoffee');
const markdown = require('./readme.coffee.md');

Browser REPL Integration

Browser-specific functionality for client-side CoffeeScript evaluation and script execution.

/**
 * Load and execute CoffeeScript from URL in browser
 * @param url - URL of CoffeeScript file
 * @param callback - Completion callback
 * @param options - Loading options
 * @param hold - Whether to hold execution
 */
function load(url, callback, options, hold);

/**
 * Execute all CoffeeScript script tags on page
 */
function runScripts();

Browser Usage Examples:

<!-- Include CoffeeScript compiler -->
<script src="coffee-script.js"></script>

<!-- CoffeeScript in script tags -->
<script type="text/coffeescript">
  console.log "Hello from browser CoffeeScript!"
  
  $ ->
    $('button').click ->
      alert 'Button clicked!'
</script>

<!-- Load external CoffeeScript file -->
<script type="text/coffeescript" src="app.coffee"></script>

<script>
  // Execute all CoffeeScript on page
  CoffeeScript.runScripts();
  
  // Load CoffeeScript file dynamically
  CoffeeScript.load('utils.coffee', function() {
    console.log('Utils loaded!');
  });
</script>

REPL Configuration

Default REPL Options

interface REPLDefaults {
  /** REPL prompt string */
  prompt: string; // 'coffee> '
  
  /** History file location */
  historyFile?: string; // ~/.coffee_history
  
  /** Maximum history input size */
  historyMaxInputSize: number; // 10240
  
  /** Custom evaluation function */
  eval: Function;
}

Custom REPL Evaluation

The REPL uses a custom evaluation function that handles CoffeeScript compilation and execution:

const customRepl = require('coffee-script/repl');

customRepl.start({
  prompt: 'my-coffee> ',
  
  eval: function(input, context, filename, callback) {
    // Custom preprocessing
    input = input.replace(/\uFF00/g, '\n');
    input = input.replace(/^\(([\s\S]*)\n\)$/m, '$1');
    
    try {
      // Compile CoffeeScript to JavaScript  
      const tokens = CoffeeScript.tokens(input);
      const ast = CoffeeScript.nodes(tokens);
      const js = ast.compile({ bare: true, locals: Object.keys(context) });
      
      // Execute in context
      const result = vm.runInContext(js, context, filename);
      callback(null, result);
    } catch (error) {
      callback(error);
    }
  }
});

REPL Features

Multi-line Input Support: The REPL automatically detects incomplete expressions and allows multi-line input:

coffee> if true
......    console.log "multi-line"
......    console.log "input works"
multi-line
input works

History Management:

  • Automatic command history saving to ~/.coffee_history
  • Up/down arrow navigation through history
  • Configurable history file location and size limits

Context Preservation: Variables and functions defined in the REPL persist across evaluations:

coffee> x = 42
42
coffee> square = (n) -> n * n  
[Function: square]
coffee> square x
1764

Node.js Integration: Full access to Node.js built-in modules and npm packages:

coffee> fs = require 'fs'
coffee> path = require 'path'
coffee> _ = require 'lodash'
coffee> _.range 5
[ 0, 1, 2, 3, 4 ]

File Extension Registration

Automatic Registration

When you require the register module, it automatically sets up handlers for CoffeeScript file extensions:

// This happens automatically when you require coffee-script/register
const Module = require('module');

// Handler for .coffee, .litcoffee, .coffee.md files
const loadFile = function(module, filename) {
  const answer = CoffeeScript._compileFile(filename, false, true);
  return module._compile(answer, filename);
};

// Register extensions
require.extensions['.coffee'] = loadFile;
require.extensions['.litcoffee'] = loadFile;
require.extensions['.coffee.md'] = loadFile;

Supported File Extensions

/** Supported CoffeeScript file extensions */
const FILE_EXTENSIONS = [
  '.coffee',     // Standard CoffeeScript files
  '.litcoffee',  // Literate CoffeeScript files
  '.coffee.md'   // Markdown-style literate CoffeeScript
];

Module Loading Behavior

Once registered, CoffeeScript files are handled transparently:

// These all work after registration
const utils = require('./utils.coffee');
const docs = require('./README.litcoffee');
const guide = require('./guide.coffee.md');

// Standard Node.js module resolution applies
const lib = require('my-coffee-lib'); // Looks for index.coffee

Advanced REPL Usage

REPL with Custom Context

const repl = require('coffee-script/repl');
const vm = require('vm');

// Create custom context with pre-loaded utilities
const context = vm.createContext({
  _: require('lodash'),
  moment: require('moment'),
  fs: require('fs'),
  path: require('path'),
  customHelper: function(msg) {
    return `[HELPER] ${msg}`;
  }
});

repl.start({
  prompt: 'enhanced-coffee> ',
  useGlobal: false,
  context: context
});

REPL Event Handling

const coffeRepl = require('coffee-script/repl');

const replInstance = coffeRepl.start({
  prompt: 'coffee> '
});

// Handle REPL events
replInstance.on('exit', () => {
  console.log('CoffeeScript REPL exited');
});

replInstance.on('reset', (context) => {
  console.log('REPL context reset');
  // Re-initialize custom context if needed
});

Integration with Development Tools

Package.json Scripts:

{
  "scripts": {
    "repl": "coffee -i",
    "dev-repl": "coffee -i -r ./lib/dev-helpers"
  }
}

Development Workflow:

# Start REPL with project modules pre-loaded
coffee -i -r ./lib/app -r lodash -r moment

# Test functions interactively
coffee> myApp = require './lib/app'
coffee> result = myApp.processData testData
coffee> console.log result

Debugging Integration:

# In REPL, you can inspect compiled JavaScript
coffee> CoffeeScript.compile "square = (x) -> x * x", bare: true
"var square;\n\nsquare = function(x) {\n  return x * x;\n};"

# Examine AST structure
coffee> ast = CoffeeScript.nodes "class Animal"
coffee> console.log JSON.stringify ast, null, 2