admin管理员组

文章数量:1336098

If I do

repl = require 'repl'

repl.start {useGlobal: true}

It starts a Node repl. How do I start a CoffeeScript repl instead?

Thanks

If I do

repl = require 'repl'

repl.start {useGlobal: true}

It starts a Node repl. How do I start a CoffeeScript repl instead?

Thanks

Share Improve this question edited Oct 10, 2012 at 5:08 Nick asked Oct 10, 2012 at 3:59 NickNick 5,44011 gold badges43 silver badges71 bronze badges 2
  • coffeescript/documentation/docs/repl.html – Mudassir Ali Commented Oct 10, 2012 at 6:19
  • That's what I ended up doing but I thought there would be a more elegant solution. – Nick Commented Oct 10, 2012 at 6:21
Add a ment  | 

2 Answers 2

Reset to default 5

Nesh is a project to try and make this a bit easier and extensible:

http://danielgtaylor.github./nesh/

It provides a way to embed a REPL with support for multiple languages like CoffeeScript as well as providing an asyncronous plugin architecture, support to execute code in the context of the REPL on startup, etc. For example:

nesh = require 'nesh'

nesh.loadLanguage 'coffee'

nesh.start (err, repl) ->
    nesh.log.error err if err

It also supports a bunch of options with the default plugins and exposes some built-in convenience functions as well:

opts =
    wele: 'Wele to my interpreter!'
    prompt: '> '
    evalData: CoffeeScript.pile 'hello = (name="world") -> "Hello, #{world}!"', {bare: true}

nesh.start opts, (err, repl) ->
    nesh.log.error err if err

I think the coffee-script module does not export the REPL functionality to be used programmatically, like the Node repl module does. But CoffeeScript has a repl.coffee file that can be used, even though it's not exported in the main coffee-script module. Taking a hint from mand.coffee (which is the file that's executed when you run the coffee mand) we can see that the REPL works just by requiring the repl file. So, running this script should start a CoffeeScript REPL:

require 'coffee-script/lib/coffee-script/repl'

This approach, however, is quite hacky. The most important flaw is that it heavily depends on how the coffee-script module works internally and how it's organized. Nothing prevents the repl.coffee file from being moved from coffee-script/lib/coffee-script, or changing the way it works.

A better approach might be calling the coffee mand without arguments, just like one would do from the mandline, from Node:

{spawn} = require 'child_process'
spawn 'coffee', [], stdio: 'inherit'

The stdio: 'inherit' option makes the spawned mand to read from stdin and write to the stdout of the current process.

本文标签: javascriptHow do I start a CoffeeScript repl from within a CoffeeScript scriptStack Overflow