//@ts-check const { exec, fork, spawn } = require('child_process'); /** * @param {string} path * @deprecated */ function hookTo(path) { exec(`node ${path}`) } /** * @param {string} path */ async function fHook(path) { const child = fork(path) return new Promise((res, rej) => { child.on('exit', () => { res() }) }) } /** * @param { string } cmd * @param { import('child_process').SpawnOptionsWithoutStdio } [option] */ async function spawnAndWait(cmd, option) { const child = spawn(cmd, option) child.stdout.pipe(process.stdout) child.stderr.pipe(process.stderr) process.stdin.pipe(child.stdin) await new Promise((res, rej) => { child.on('exit', () => { res() }) }) process.stdin.unpipe(child.stdin) } /** * @param { string } path * @param { string[] } [args] * @param { import('child_process').SpawnOptionsWithoutStdio } [options] */ async function startjs(path, args = [], options = {}) { const arguments = args.map(arg => {return arg.includes(' ') ? `"${arg}"` : arg}).join(' ') return spawnAndWait(`/usr/local/bin/node ${path} ${arguments}`, options) } module.exports = { hookTo, fHook, spawnAndWait, startjs }