Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use global with nesh #3

Closed
freefrancisco opened this issue Aug 1, 2013 · 5 comments
Closed

can't use global with nesh #3

freefrancisco opened this issue Aug 1, 2013 · 5 comments

Comments

@freefrancisco
Copy link

I don't know if this is a problem with nesh, or user error, but when I start nesh with the option useGlobal: true (which is one of the options for repl) it doesn't work any more. Here is my test code:

 nesh = require 'nesh' 
opts = welcome: 'Howdy!', prompt: 'nesh> ', useGlobal: true
nesh.loadLanguage 'coffee'
nesh.start opts, (err) ->
  nesh.log.error err if err

And here is my output:

16:59 ~/code/play $ coffee neshtest.coffee
Howdy!
nesh> x=2
TypeError: needs a 'context' argument.
at REPLServer.replDefaults.eval (/usr/local/share/npm/lib/node_modules/coffee-script/lib/coffee-script/repl.js:33:28)
at repl.js:239:12
at Interface. (/usr/local/share/npm/lib/node_modules/coffee-script/lib/coffee-script/repl.js:62:9)
at Interface.EventEmitter.emit (events.js:117:20)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:754:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:117:20)
at emitKey (readline.js:1089:12)
at ReadStream.onData (readline.js:834:14)
at ReadStream.EventEmitter.emit (events.js:95:17)
at ReadStream. (stream_readable.js:720:14)
at ReadStream.EventEmitter.emit (events.js:92:17)
at emitReadable
(_stream_readable.js:392:10)
at emitReadable (_stream_readable.js:388:5)
at readableAddChunk (_stream_readable.js:150:9)
at ReadStream.Readable.push (_stream_readable.js:113:10)
at TTY.onread (net.js:511:21)

nesh>

What I am trying to do is run nesh with the same context as the main program so I can interact with the objects in node through nesh just like I would interact with ActiveRecord objects through the rails console. Is this possible? What am I doing wrong?

@danielgtaylor
Copy link
Owner

This definitely seems like it should work and I can confirm that it doesn't. I'm not sure why as the vm and repl modules are a bit of black magic. Here's a workaround that I would suggest until I can figure it out:

nesh = require 'nesh'

foo1 = 1
foo2 = 'something'
foo3 = new ...

nesh.loadLanguage 'coffee'
nesh.start prompt: 'nesh> ', (err, repl) ->
    if err then return nesh.log.error(err)

    repl.context.foo1 = foo1
    repl.context.foo2 = foo2
    repl.context.foo3 = foo3

This way you can expose objects and let users interact with them one by one. If you have a models module you can even expose it all in one go, e.g. repl.context.models = require('models'). Then a user can do stuff like nesh> user = new models.User(...) right in the interpreter. Alternatively you can loop through all the keys and expose each value one by one.

If you find yourself needing this often it would be relatively easy to write a plugin that does this during the postStart event.

Hope this helps.

@danielgtaylor
Copy link
Owner

I believe the changes proposed in #4 will fix this issue. They require upstream changes to work with Coffeescript though, so I don't have a timeframe for implementation. See #4 and jashkenas/coffeescript#3113 for more info.

@danielgtaylor
Copy link
Owner

It looks like this is fixed with CoffeeScript 1.7 - feel free to update (npm install -g coffee-script) and give it a try.

@freefrancisco
Copy link
Author

It is fixed, yay! Thanks!

@danielgtaylor
Copy link
Owner

Closing as it's fixed with an upstream update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants