srele96 / run-them-all

Allows you to save the series of commands to a configuration and run them all easily.
MIT License
0 stars 1 forks source link

Use spawn instead of exec #9

Open srele96 opened 2 years ago

srele96 commented 2 years ago

Read information from: https://github.com/srele96/run-them-all/issues/6!

NodeJS spawn is better for long running processes. It also provides options.detached which (per my understanding) allows us to:

Things to add:

  1. Align requires
  2. Add cross-spawn 3.1 Update package.json 3.2 Update package-lock.json
  3. Use cross-spawn
  4. Add loggers for spawned command
srele96 commented 2 years ago

Notes:

  1. CLI can't exit if command runs long running process (for example: nodemon express server).
  2. Using options.stdio = 'inherit' will print the stuff from the executed command in current terminal.

Point 1 is solvable with https://nodejs.org/api/child_process.html#optionsdetached. That opens questions:

srele96 commented 2 years ago

Long running detached processes get spawned in new OS native terminal.

Run detached process like here https://nodejs.org/api/child_process.html#optionsdetached

const { openSync } = /* */ require('fs');
const { spawn } = /*    */ require('child_process');
const out = openSync('./out.log', 'a');
const err = openSync('./err.log', 'a');

// watch express server using nodemon
const subprocess = spawn('yarn start:serve-data', {
  cwd: 'C:\\Users\\Srecko\\Documents\\MyProjects\\Private\\webstorm-container\\sk-experiments\\tcp-packages',
  detached: true,
  shell: true,
  stdio: ['ignore', out, err],
});

subprocess.unref();
srele96 commented 2 years ago

Detached process should behave exactly the same as any terminal.

We can't use ignore for stdin like stdio: ['ignore', out, err]. CTRL+C does not terminate it.

Is it a problem if new detached process runs as NodeJS? (It has the NodeJS icon).

srele96 commented 2 years ago

So far i know about the two types of processes.

A process like nodemon app.js. Such process watches file changes and shows command line output.

Another type is echo hello or mkdir test. Those exit immediately, but may have command line output like echo.

I believe we shouldn't treat them differently. Both are the same, but one exits faster.

That means we need to handle processes that are:

Running process interfere with our cli. The cli is a parent process. Parent process waits for child processes to exit.

We currently can't handle user interaction while the child processes are running.

While child processes are running, so is cli. Terefore we need to treat that case accordingly.

Whenever we run commands, we should expect their process to be long running. Actually, we shouldn't even think how long they run. All of them behave the same, whether it's a millisecond, second, minute or more.

Therefore, we should behave as process-watcher.

The cli is in two states:

The watched processes are:

User should be able to use the cli in both states.

srele96 commented 2 years ago

Learn how does spawn run command on any OS.

That may provide a way to run command in new shell on any OS.