简体   繁体   English

Arguments 用于节点 child_process“spawn()”

[英]Arguments for Node child_process "spawn()"

Using child_process (spawn) to open a new BASH terminal and execute the commands in each file.使用 child_process (spawn) 打开一个新的 BASH 终端并执行每个文件中的命令。

I want to add all of my.sh files, that I'm going to spawn, into a folder to clean up my project directory.我想将我要生成的所有 my.sh 文件添加到一个文件夹中以清理我的项目目录。

PROBLEM: I can't figure out how to change the directory the scripts run from, and the docs are a little to heavy for me at this point.问题:我不知道如何更改脚本运行的目录,此时文档对我来说有点重。

SIMPLE TEST EXAMPLE, WITH MY FIRST TWO FILES FOR CONNECTING (each.sh file would be in the project folder):简单的测试示例,使用我的前两个连接文件(每个 .sh 文件将位于项目文件夹中):


  
  
  
    const { spawn } = require('node:child_process');

    const bat = spawn('cmd.exe', ['/c', 'connect.sh']);

/*
    <connect.sh> // 1st .sh file
    #!/usr/bin/env bash  //to make it bash
    HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev

    <connect1.sh> // 2nd .sh file
    #!/usr/bin/env bash   //to make it bash
    HTTP_PORT=3003 P2P_PORT=5003 PEERS=ws://localhost:5002,ws://localhost:5001 npm run dev
*/

I have 9 of these files to connect up to 10 peers.我有 9 个这样的文件可以连接多达 10 个对等点。 And I would like to put them in a folder to simplify my project structure.我想将它们放在一个文件夹中以简化我的项目结构。

This is my actual API call below.... // Uses length to determine which file to run这是我下面的实际 API 调用.... // 使用长度来确定要运行的文件


  
  
  
     app.post("/peers/connect", async function (req, res) {
      const peerInfo = await peers.info();
      // no peers yet
      if (typeof peerInfo === "string") {
        let bat = spawn("cmd.exe", ["/c", "connect.sh"]);
        res.json("A new terminal has opened! You are now connected!");
      } else { 
        // peers exist
        let length = peerInfo.peers;
        // console.log(length);
        let bat = spawn("cmd.exe", ["/c", `connect${length}.sh`]);
        res.json("A new terminal has opened! You are now connected!");
      }
    });

My file structure here...you can see why I want a folder for these!我的文件结构在这里......你可以明白为什么我想要一个文件夹!

我的文件结构

RECAP: Help my put all of these files into a folder (shellScripts) and have the code still work:) Thanks?回顾:帮助我将所有这些文件放入一个文件夹 (shellScripts) 并让代码仍然有效:) 谢谢? (just realized we might have to cd back into project folder before "npm run dev" in each file?) (刚刚意识到我们可能必须在每个文件中的“npm run dev”之前 cd 回到项目文件夹?)

You are using the cmd.exe utility to run a.sh file, but that wont work.您正在使用 cmd.exe 实用程序来运行 .sh 文件,但这不起作用。 You have to install a bash interpreter on your windows device or install WSL.您必须在 windows 设备上安装 bash 解释器或安装 WSL。 (If necessary add bash.exe to the windows path) Then change your code to this: (如有必要,将 bash.exe 添加到 windows 路径)然后将代码更改为:

const { spawn } = require('node:child_process');
const bat = spawn('bash.exe',['connect.sh']);

I hope this answer helped我希望这个答案有所帮助

For running multiple files:对于运行多个文件:

const { spawn } = require('node:child_process');
const fs = require("node:fs")
const dir = "" // Replace this with the location of the directory containing connect shellscripts
let entrys = fs.readdirSync(dir)
entrys = entrys.filter(v=>v.startsWith("connect"))
for(let ent of entrys){
const bat = spawn('bash.exe',[ent]);
// your code here
}

Figured out the answer on me own.我自己想出了答案。 Thanks to everyone that tried to help:) And to those saying my above code doesn't work, it works perfectly fine.感谢所有试图提供帮助的人:) 对于那些说我上面的代码不起作用的人,它工作得很好。

I've provided a picture to clarify.我提供了一张图片来澄清。 1st is what the code below produces.第一个是下面的代码产生的。 2nd is manually pasting it into GIT BASH.第二个是手动将其粘贴到 GIT BASH 中。

// test.js in project structure pic above
var exec = require('child_process').exec;
var path = require('path')

var parentDir = path.resolve(process.cwd(), 'shellScripts');
exec('my.sh', {cwd: parentDir}, function (error, stdout, stderr) {
  // if you also want to change current process working directory:
  process.chdir(parentDir);
});

This is what the code produces.这就是代码产生的结果。 图 1. 代码输出

And this is opening a GIT BASH in project folder and pasting the command in这是在项目文件夹中打开一个 GIT BASH 并将命令粘贴到

图 2. 手动 GIT BASH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM