Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Coffeescript Interactive (REPL) with a script

In python, I can run a script and enter interactive mode in the context of that script. This lets me mess with global variables and what not to examine program state.

$ python -i hello.py

Can I do this with Coffeescript? I've tried the following:

$ coffee -i hello.coffee

doesn't load hello.coffee. It's equivalent to coffee -i

$ cat hello.coffee | coffee -i

runs the script line by line in REPL but ends REPL after the EOF.

like image 406
Kenneth Cheng Avatar asked Nov 23 '12 06:11

Kenneth Cheng


2 Answers

I've recently started a project to create an advanced interactive shell for Node and associated languages like CoffeeScript. One of the features is loading a file or string in the context of the interpreter at startup which takes into account the loaded language.

http://danielgtaylor.github.com/nesh/

Example:

# Load a string
nesh -c -e 'hello = (name) -> "Hello, #{name}"'

# Load a file
nesh -c -e hello.coffee

Then in the interpreter you can access the hello function. Also a good idea to create an alias in bash:

alias cs='nesh -c'
like image 103
Daniel Avatar answered Sep 23 '22 23:09

Daniel


cat foo.coffee - | coffee -i

tells cat to first output your code and then output stdin, which gives you what you're looking for I think.

like image 38
rgiar Avatar answered Sep 22 '22 23:09

rgiar