使用 Deno 的 CLI REPL

CLI REPL with Deno

我想使用 Deno 构建一个 CLI 应用程序,但是我找不到一个模块可以让我继续提示用户进行类似于命令行应用程序与 Node.js[= 上的 REPL 模块的交互10=]

有什么建议吗?

您可以使用 std/io 构建 REPL。

import { readLines } from "https://deno.land/std@v0.52.0/io/bufio.ts";


async function read() {
   // Listen to stdin input, once a new line is entered return
   for await(const line of readLines(Deno.stdin)) {
      console.log('Received', line)
      return line;
   }
}

console.log('Start typing');
while(true) {
        await read()
}

您可以从这里构建、处理每一行、添加命令等等。

如果你只想要一行,你可以这样做

import { readLines } from "https://raw.githubusercontent.com/denoland/deno/master/std/io/bufio.ts";

const word = (await readLines(Deno.stdin).next()).value.trim()

console.log(`You typed: ${word}`)
D:\WorkSpace\VSCode\deno-play>deno run -A main.ts
hello
You typed: hello

Current denoland lib Commits on Jun 19, 2020