Ryan Peschel
Ryan Peschel

Reputation: 11976

How to execute WSL commands from a node process?

I'm developing a small node build script that I want to launch WSL, cd to my project directory, and invoke a build command. Something like this:

import { exec } from "child_process";

async function execCommand(cmd) {
  console.log("Executing: " + cmd);

  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
      if (error) { reject(); }
      else { resolve(); }
    });
  });
}

await execCommand(`wsl`);
await execCommand(`cd /mnt/c/Users/Admin/Documents/Projects/myproject/backend`);
await execCommand(`cargo build --release`);

My issue is that it just.. doesn't work. That is, the first execCommand calls wsl and then the terminal window seems to never complete the command (it never finishes).

Instead, it prompts the user to type something in the command prompt.

What is the proper way to launch WSL from a node script and then chain a bunch of commands together, each one waiting on the previous one to finish?

Upvotes: 0

Views: 1560

Answers (2)

Ryan Peschel
Ryan Peschel

Reputation: 11976

Actually, my other answer isn't that good, because it launches a separate shell and runs the commands through the cmd shell instead, so the binary will be built by Windows instead of WSL, which is not what we want. A better answer is:

let child = spawnSync("wsl", [ "bash", "-l", "-c", "cargo build --release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend"
});

console.log(child.stdout.toString());
console.log(child.stderr.toString());

You need to include bash -l because when you run a command through wsl it doesn't execute ~/.profile and ~/.bashrc (etc). Cargo needs that so it can insert ~/.cargo/bin to $PATH.

So with bash -l you force it to execute the profile init scripts. When you launch WSL without any other command, it will login interactively meaning those scripts are executed.

Upvotes: 1

Ryan Peschel
Ryan Peschel

Reputation: 11976

Finally got it working.

Async version:

let child = spawn("wsl", [ "cd", "/mnt/c/Users/Admin/Documents/Projects/myserver/backend", "&&", "cargo", "build", "--release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend",
  shell: true
});

child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

child.stdin.end();

Synchronous version:

let child = spawnSync("wsl", [ "cd", "/mnt/c/Users/Admin/Documents/Projects/myserver/backend", "&&", "cargo", "build", "--release" ], {
  cwd: "C:\\Users\\Admin\\Documents\\Projects\\myserver\\backend",
  shell: true
});

console.log(child.stdout.toString());
console.log(child.stderr.toString());

Upvotes: 2

Related Questions