A cross-platform function for spawning Ruby processes
npm install ruby-spawncwd in the process options is not enough). rubySpawn automates the steps required to execute the process. The return value is a standard ChildProcess, so rubySpawn is (mostly) a drop-in replacement for child_process.spawn. It works on Linux, MacOS, and Windows.
// JavaScript
const rubySpawn = require('ruby-spawn');
// TypeScript
import { rubySpawn } from 'ruby-spawn';
`
The function definition:
`
rubySpawn(command, arguments = [], options = {}, forceKill = false);
`
Arguments:
* command: (string) The command to be executed. Can be a system command or any executable file.
* arguments: (string[]) An array of arguments passed to the command.
* options: (object) See child_process.spawn for more information.
* forceKill: (boolean) If true, kill orphaned processes when the parent process exits.
Examples:
`
// Run a script in the current directory
rubySpawn('ruby', ['./script.rb'])
// Run a script in a different directory
rubySpawn('ruby', ['./script.rb'], { cwd: '/path/to/directory' });
// Run Bundler in a different directory
rubySpawn('bundle', ['install'], { cwd: '/path/to/directory' });
// Run RSpec in a different directory
rubySpawn('rspec', ['/path/to/directory'], { cwd: '/path/to/directory' });
`
Force Killing Processes
If you need to kill processes programmatically, you might need to set the optional forceKill option:
`
let child = rubySpawn('ruby', ['file.rb'], { cwd: '/path/to/dir' }, true);
`
Then, when you call child.kill(), ruby-spawn will kill processes that it identifies as orphans of child`.