shell 自定义节点REPL,带有 typescript 和ts节点

wgxvkvu9  于 2023-02-13  发布在  Shell
关注(0)|答案(1)|浏览(166)

我想创建一个自定义节点REPL,并使其与typescript一起工作。
节点评估
节点评估(代码:字符串,上下文:任意,文件名(_F):字符串,回调:(错误:空|〉错误,结果?:任何)=〉任何):空隙
在src/repl. ts:91中定义
eval实现与节点的REPL API兼容
如果您想手动创建自己的node〉REPL示例并将eval委托给这个ReplService,则可以在高级场景中使用。
示例:

import {start} from 'repl'; 
const replService: tsNode.ReplService = ...; // assuming you have already created a ts-node ReplService
const nodeRepl = start({eval: replService.eval});

块引号
参数
代码:字符串
上下文:任何
_文件名:字符串
回调:(错误:零|错误,结果?:任何)=〉任何
(err:空|错误,结果?:任何):任何
参数
错误:空|错误
可选结果:任何
返回任何
退货无效
nodeEval是用createRepl方法创建的ReplService类的成员。它似乎与可以向下传递给start方法的eval参数兼容。
所以我写了这个:

import { start } from "repl";
import * as tsnode from "ts-node";

const tsNodeConfig = {
  compilerOptions: {
    target: "ES2017",
    module: "commonjs",
  },
};

tsnode.register({
  compilerOptions: {
    target: "ES2017",
    module: "CommonJS",
  },
});

const tsrepl: tsnode.ReplService = tsnode.createRepl({
  stdin: process.stdin,
  stdout: process.stdout,
});

const { nodeEval } = tsrepl;

tsnode.register(tsNodeConfig);

start({
  prompt: "$  ",
  ignoreUndefined: true,
  eval: nodeEval,
});

然后我在创建repl. ts文件的终端中运行代码,并运行ts-node repl.ts shell启动,但是当我输入let foo = "bar"这样的值时,它会给我这个错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'options')
    at REPLServer.Interface._ttyWrite (node:readline:1216:14)
    at REPLServer.Interface._line (node:readline:864:8)
    at REPLServer.Interface._onLine (node:readline:487:10)
    at REPLServer.emit (node:domain:475:12)
    at REPLServer.emit (node:events:527:28)
    at REPLServer.onLine (node:repl:893:10)
    at REPLServer.runBound [as eval] (node:domain:432:12)
    at bound (node:domain:421:15)
    at REPLServer.nodeEval (/Users/WAW/Documents/Projects/noiz-network-state/node_modules/ts-node/src/repl.ts:262:7)
    at handleError (/Users/WAW/Documents/Projects/noiz-network-state/node_modules/ts-node/src/repl.ts:271:18)
$

如果我不带eval参数传递对象,就像这样

start({
  prompt: "$  ",
  ignoreUndefined: true
});

repl作为节点repl工作,并且它不计算类似type foo = string的类型脚本代码。
你知道吗?

c3frrgcw

c3frrgcw1#

我找到了解决方案,事实上需要正确配置ts-node repl service才能正常工作。
首先我们需要导入

  • repl来自节点程序包
  • typescript npm软件包升级到ts
  • 来自 Package 的ts-node
import repl from "repl";
import ts from "typescript";
import * as tsnode from "ts-node";

那么我们需要创建一个TS节点repl服务,如下所示:

const replService: tsnode.ReplService = tsnode.createRepl();

现在我们可以创建一个服务了,这要归功于evalAwarePartialHost成员,它位于我们刚刚创建的replService中:

const service = tsnode.create({ ...replService.evalAwarePartialHost });

然后将servicets成员设置为导入的ts

service.ts = ts;

现在我们必须在TS节点Repl:

replService.setService(service);

现在是时候创建实际的repl服务器了:

const replServer = repl.start({
  prompt: "$  ",
  ignoreUndefined: true,
  eval: replService.nodeEval,
});

整个代码如下所示:

import repl from "repl";
import ts from "typescript";
import * as tsnode from "ts-node";

// Create a ts-node replService
const replService: tsnode.ReplService = tsnode.createRepl();
const service = tsnode.create({ ...replService.evalAwarePartialHost });
service.ts = ts;
replService.setService(service);

// create a node-repl server
const replServer = repl.start({
  prompt: "$  ",
  ignoreUndefined: true,
  eval: replService.nodeEval,
});

// setup environment
replServer.setupHistory(".log", () => {});

希望这能对其他人有所帮助!
=)

相关问题