shellService.js
1 import { exec, spawn } from "child_process"; 2 import { promisify } from "util"; 3 4 export class ShellService { 5 constructor() { 6 this.execAsync = promisify(exec); 7 } 8 9 run = async (command) => { 10 try { 11 const { stdout, stderr } = await this.execAsync(command); 12 return stdout; 13 } catch (error) { 14 console.error("Error:", error.message); 15 throw error; 16 } 17 }; 18 19 spawnDetachedProcess = async (cmd, workingDir, args) => { 20 var child = spawn(cmd, args, { 21 cwd: workingDir, 22 detached: true, 23 stdio: ["ignore", "ignore", "ignore"], 24 }); 25 26 // child.stdout.on("data", (data) => { 27 // console.log(`stdout: ${data}`); 28 // }); 29 30 // child.stderr.on("data", (data) => { 31 // console.error(`stderr: ${data}`); 32 // }); 33 34 // child.on("close", (code) => { 35 // console.log(`child process exited with code ${code}`); 36 // }); 37 38 child.unref(); 39 40 await new Promise((resolve) => setTimeout(resolve, 2000)); 41 }; 42 }