8
votes

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.

3
From stackoverflow.com/a/13386057/149330: Use cat hello.coffee - | coffee - int3
Its no t exactly what you're looking for, but how about just writing it as a module and then use require to load it in the normal repl mode? - mzedeler
This is the only real reason for not using coffeescript imo. one needs to be able to invoke the coffeescript repl from a script. you can use source maps n stuff but its a bit awkward - Adam Spence

3 Answers

4
votes

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'
1
votes

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.

0
votes

I am confronted with this problem as well. The one provide by @int3 doesn't solve this problem, for CoffeeScript is one indentation based language. stdin will pass the code line by line, but the repl is not smart enough to realize this. Since you post this question, I suggest you create one issue (feature request) on CoffeeScript