1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import { ChildProcess, spawn, StdioOptions } from 'child_process';
import { existsSync, mkdirSync } from 'fs';
import { rimraf } from 'rimraf';
import { Stream } from 'stream';
import { fileDescriptorFromStream, pathFromRoot } from './ActionUtilities';
export namespace Logger {
const logPath = pathFromRoot('./logs');
export async function initialize() {
if (existsSync(logPath)) {
if (!process.env.SPAWNED) {
await new Promise<any>(resolve => {
rimraf(logPath).then(resolve);
});
}
}
mkdirSync(logPath);
}
function generateLogPath(command: string, args?: readonly string[]) {
return pathFromRoot(`./logs/${command}-${args?.length}-${new Date().toUTCString()}.log`);
}
export async function create(command: string, args?: readonly string[]): Promise<number> {
return fileDescriptorFromStream(generateLogPath(command, args));
}
}
export namespace ProcessFactory {
export type Sink = 'pipe' | 'ipc' | 'ignore' | 'inherit' | Stream | number | null | undefined;
export async function createWorker(command: string, args?: readonly string[], stdio?: StdioOptions | 'logfile', detached = true): Promise<ChildProcess> {
if (stdio === 'logfile') {
const logFd = await Logger.create(command, args);
// eslint-disable-next-line no-param-reassign
stdio = ['ignore', logFd, logFd];
}
const child = spawn(command, args ?? [], { detached, stdio });
child.unref();
return child;
}
}
|