8

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.

Kenneth Cheng
  • 446
  • 4
  • 7
  • 1
    From http://stackoverflow.com/a/13386057/149330: Use `cat hello.coffee - | coffee` – int3 Nov 27 '12 at 00:19
  • 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 Mar 03 '13 at 20:48
  • 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 Dec 30 '14 at 22:38

3 Answers3

4

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'
Daniel
  • 8,212
  • 2
  • 43
  • 36
1

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.

rgiar
  • 1,688
  • 18
  • 10
  • This is pretty neat. Has the same issue that it doesn't parse multiline statements, and it requires a `process.exit()` plus a ctrl-c, but it's still a useful hack to bootstrap a few things... – chmac Sep 17 '14 at 01:52
0

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

Albert Netymk
  • 1,102
  • 8
  • 24