代码之家  ›  专栏  ›  技术社区  ›  Exortions

节点。JS子进程更改终端类型

  •  0
  • Exortions  · 技术社区  · 3 年前

    我使用Node JS子进程来执行终端命令并获得它们的输出,这一切都很顺利。然而,我在一台WSL Windows机器上,我想将子进程的类型更改为终端 zsh .

    下面是我当前执行命令和接收输出的代码。

    async function execute(
        command: string,
        cwd: string,
    ): Promise<{ output: string; error: string }> {
        const out = await promisify(exec)(command, { cwd: cwd });
    
        const output = out.stdout.trim();
        const error = out.stderr.trim();
    
        return { output: output, error: error };
    }
    

    如何更改终端的类型 command prompt ?

    1 回复  |  直到 3 年前
        1
  •  1
  •   Khalfoun Mohamed El Mehdi    3 年前

    你能做到的

    exec(command , { shell : "zsh" })
    

    您可以在此处阅读更多有关外壳要求的信息: https://nodejs.org/api/child_process.html#shell-requirements

    所以你的代码应该是这样的:

    async function execute(
        command: string,
        cwd: string,
    ): Promise<{ output: string; error: string }> {
        const out = await promisify(exec)(command, { cwd: cwd , 
        shell:"zsh"});
    
        const output = out.stdout.trim();
        const error = out.stderr.trim();
    
        return { output: output, error: error };
    }