|
| 1 | +import { |
| 2 | + ChildProcessWithoutNullStreams, |
| 3 | + execSync, |
| 4 | + spawn, |
| 5 | +} from 'node:child_process'; |
| 6 | +import { resolve as pathResolve } from 'node:path'; |
| 7 | +import process from 'node:process'; |
| 8 | + |
| 9 | +import { Task, TaskStatus } from '@angular-cli-gui/shared/data'; |
| 10 | +import { |
| 11 | + Global, |
| 12 | + Injectable, |
| 13 | + InternalServerErrorException, |
| 14 | + Logger, |
| 15 | + NotFoundException, |
| 16 | +} from '@nestjs/common'; |
| 17 | + |
| 18 | +import { SessionService } from '../session/session.service'; |
| 19 | + |
| 20 | +import { ChildProcesses } from './child-processes'; |
| 21 | +import { spawnedProcessCommandLine } from './utils'; |
| 22 | + |
| 23 | +const NG = 'npx ng'; |
| 24 | + |
| 25 | +@Global() |
| 26 | +@Injectable() |
| 27 | +export class ExecutorsService { |
| 28 | + private readonly logger = new Logger(ExecutorsService.name); |
| 29 | + private childProcesses = new ChildProcesses(); |
| 30 | + |
| 31 | + constructor(private readonly sessionService: SessionService) { |
| 32 | + process.on('SIGINT', () => this.destroy()); |
| 33 | + process.on('SIGTERM', () => this.destroy()); |
| 34 | + } |
| 35 | + |
| 36 | + async execAsync(args: string[]): Promise<number> { |
| 37 | + try { |
| 38 | + const childProcess = await this.childProcessSpawn(args); |
| 39 | + this.childProcesses.add(childProcess); |
| 40 | + |
| 41 | + return childProcess.pid as number; |
| 42 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 43 | + } catch (err: any) { |
| 44 | + this.logger.error(err.message); |
| 45 | + throw new InternalServerErrorException(err.message); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + readTaskStatus(pid: number): TaskStatus { |
| 50 | + const childProcess = this.childProcesses.get(pid); |
| 51 | + |
| 52 | + if (!childProcess) { |
| 53 | + throw new NotFoundException(`Process with pid: ${pid} was not found`); |
| 54 | + } |
| 55 | + |
| 56 | + return childProcess.status; |
| 57 | + } |
| 58 | + |
| 59 | + killTask(pid: number): void { |
| 60 | + const childProcess = this.childProcesses.get(pid); |
| 61 | + |
| 62 | + if (!childProcess) { |
| 63 | + throw new NotFoundException(`Process with pid: ${pid} was not found`); |
| 64 | + } |
| 65 | + |
| 66 | + this.kill(pid); |
| 67 | + } |
| 68 | + |
| 69 | + readAllTasks(): Task[] { |
| 70 | + const tasks: Task[] = []; |
| 71 | + |
| 72 | + for (const [pid, childProcess] of this.childProcesses.entries()) { |
| 73 | + const { isRunning, exitCode } = childProcess.status; |
| 74 | + const commandLine = spawnedProcessCommandLine( |
| 75 | + childProcess.spawnedProcess |
| 76 | + ); |
| 77 | + tasks.push({ pid, isRunning, exitCode, commandLine }); |
| 78 | + } |
| 79 | + |
| 80 | + return tasks; |
| 81 | + } |
| 82 | + |
| 83 | + private destroy(): void { |
| 84 | + this.logger.log('Application destroy hook'); |
| 85 | + } |
| 86 | + |
| 87 | + private childProcessSpawn( |
| 88 | + args: string[] |
| 89 | + ): Promise<ChildProcessWithoutNullStreams> { |
| 90 | + return new Promise((resolve, reject) => { |
| 91 | + const childProcess = spawn(NG, args, { |
| 92 | + cwd: pathResolve(this.sessionService.cwd), |
| 93 | + env: process.env, |
| 94 | + shell: true, |
| 95 | + }); |
| 96 | + |
| 97 | + childProcess.on('error', (err) => reject(err)); |
| 98 | + |
| 99 | + if (childProcess.pid) { |
| 100 | + resolve(childProcess); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + private kill(pid: number): void { |
| 106 | + switch (process.platform) { |
| 107 | + case 'win32': |
| 108 | + execSync(`taskkill /pid ${pid} /T /F`); |
| 109 | + break; |
| 110 | + case 'darwin': |
| 111 | + throw new Error('Not yet implemented'); |
| 112 | + break; |
| 113 | + default: |
| 114 | + throw new Error('Not yet implemented'); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments