node.js - 通过child_process执行命令时如何处理 Node 的输入提示?

标签 node.js typescript 1password

对于上下文,我在 Mac 上,我正在尝试通过他们的 command-line tool 编写 1Password CLI 登录脚本。 .我正在尝试使用如下命令以编程方式签名:

op signin <signinaddress> <emailaddress> <secretkey> --output=raw

我尝试过使用/不使用 --output=raw 参数,但每次我只是得到一个看起来像这样的错误

[LOG] 2019/06/04 00:57:45 (ERROR) operation not supported on socket

child process exited with code 1

我最初的预感是,它与下图中显示此特殊键字符的命令执行提示有关:

op signin prompt img

相关代码是用 TypeScript 编写的,如下所示:

import { spawn } from 'child_process'

// ends up being `op signin <signinaddress> <emailaddress> <secretkey>`
const op = spawn(opExecutable, args);
let result: string | null = null

op.on('message', (message, sendHandle) => {
  console.log('message', message, sendHandle)
});
op.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
  if (data && typeof data.toString === 'function') {
    result = data.toString()
  }
});

op.on('close', (code, ...args) => {
  console.log(`child process exited with code ${code}`, args);
});

最终,我想在所有平台上运行,并能够传入 stdin 作为登录所需的主密码,但我想弄清楚为什么我的 Node 应用程序首先崩溃了:)

最佳答案

显然,我很接近使用 spawn 的解决方案。 ,但我需要为 stdio 指定配置.这是我如何使用对我有用的 spawn 的示例片段:

const proc = spawn(
  cmd, // the command you want to run, in my case `op`
  args, // arguments you want to use with the above cmd `signin`, etc.
  {
    stdio: [
      'inherit', // stdin: changed from the default `pipe`
      'pipe', // stdout
      'inherit' // stderr: changed from the default `pipe`
    ]
  });

关于node.js - 通过child_process执行命令时如何处理 Node 的输入提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56438183/

相关文章:

node.js - 在 node.js 的子进程中使用 vim 时出现 stdio/管道问题

node.js - mongoosastic findOneAndUpdate 不在elasticsearch 中建立索引

javascript - 带有 TypeScript 的 JQWidget

typescript - 将字符串数组转换为 TypeScript 类型

typescript - 使用 Typescript、Babel 7、装饰器保留元数据

google-chrome - 1Password Chrome 扩展如何实现圆角?

node.js - 用于简单 nodejs webapp 的 greenlock-express

java - Node 服务器拦截从 Angular (http)到java spring后端的所有请求