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
2 Answers
Reset to default 5Nesh 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
版权声明:本文标题:javascript - How do I start a CoffeeScript repl from within a CoffeeScript script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392398a2466245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论