nx 在通过 readline 的输入提示中不显示问题

问题描述 投票:0回答:0

我通过

nx run
在 nx monorepo 中运行 CLI,看起来 nx 正在缓冲换行符上的 CLI 输出,并阻止从终端交互地回答提示。这是一个简单的例子:

假设我有一个用 Python 或 JavaScript 编写的 CLI,它显示一条消息,然后要求输入。我怎样才能获得另一个nodejs脚本来运行这个CLI并向我显示问题:

What is your name?
而不需要在问题后换行

输出示例

Welcome to my CLI!
What is your name? Paul
Hi, Paul!

Python 实现

print("Welcome to my CLI")
user_name = input("What is your name?")
print(f"Hi, {user_name}")

TypeScript 实现

import readline from 'readline';

export const askQuestion = (query: string): Promise<string> => {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
  });

  return new Promise((resolve, reject) => {
    rl.question(query, (ans) => {
      rl.close();
      if (ans) {
        resolve(ans);
      } else {
        reject('No answer received');
      }
    });
  });
};

const main = async () => {
  console.log("Welcome to my CLI");
  const userName = await askQuestion("What is your name?");
  console.log(`Hi, ${userName}!`);
}

我的尝试(未能显示问题)

import * as execa from 'execa';
const main = async () => {
  await execa(cmd, args, {
    stdio: [process.stdin, process.stdout, 'pipe'],
  });
}
main()

输出

Welcome to my CLI!

就是这样!它不会显示“你叫什么名字?”。我可以输入“Paul”,程序就会继续进行。但通过 nx 运行时它实际上不会显示问题。如果问题末尾有

\n
(
"What is your name?\n"
),那么它就会显示。但不幸的是,这个 CLI 运行程序需要与我无法控制的 CLI 一起使用,并且会提示输入而不换行。

node.js child-process spawn nrwl-nx
© www.soinside.com 2019 - 2024. All rights reserved.