How to start CoffeeScript replacement with CoffeeScript script?

If i do

repl = require 'repl' repl.start {useGlobal: true} 

He runs Node repl. How can I replace CoffeeScript instead?

thanks

+4
source share
2 answers

Nesh is a project that tries to make it a little easier and extensible:

http://danielgtaylor.github.com/nesh/

It provides the ability to embed REPL with support for several languages, such as CoffeeScript, as well as provide an asynchronous plug-in architecture, support for executing code in the context of REPL at startup, etc. For instance:

 nesh = require 'nesh' nesh.loadLanguage 'coffee' nesh.start (err, repl) -> nesh.log.error err if err 

It also supports many options with default plugins and provides some built-in convenience features:

 opts = welcome: 'Welcome to my interpreter!' prompt: '> ' evalData: CoffeeScript.compile 'hello = (name="world") -> "Hello, #{world}!"', {bare: true} nesh.start opts, (err, repl) -> nesh.log.error err if err 
+5
source

I think that the coffee-script module does not export REPL functionality that will be used programmatically, as the Node repl module does. But CoffeeScript has a repl.coffee file that you can use, although it is not exported in the main coffee-script module. Taking the command.coffee prompt (this is the file that was executed when the coffee command was run), we see that REPL only works with the repl file. So, running this script should launch CoffeeScript REPL:

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

This approach, however, is pretty hacky. The most important drawback is that it depends heavily on how the coffee-script module works internally and how it is organized. Nothing prevents you from moving the repl.coffee file from coffee-script/lib/coffee-script or changing the way it works.

A better approach would be to invoke the coffee command with no arguments, as on the command line, from Node:

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

The stdio: 'inherit' causes the programmed command to read from stdin and write to stdout of the current process.

+5
source

Source: https://habr.com/ru/post/1438801/


All Articles