-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.ts
92 lines (75 loc) · 2.42 KB
/
process.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import type { ChildProcessWithoutNullStreams } from "node:child_process";
import { spawn } from "cross-spawn";
import { readdir, readFile, rm, stat, writeFile } from "node:fs/promises";
import type { Stats } from "node:fs";
import { join } from "node:path";
export const currentDir = process.cwd();
export function withCurrentDir(...paths: string[]) {
return join(currentDir, ...paths);
}
export function promisifyChildProcess(
childProcess: ChildProcessWithoutNullStreams,
): Promise<number> {
return new Promise((resolve, reject) => {
const errorChunks: string[] = [];
let lastOutput: string;
childProcess.once("error", (error) => {
reject(error);
});
childProcess.stdout.once("error", (error) => {
reject(error);
});
childProcess.stderr.on("data", (data) => {
errorChunks.push(data);
});
// Fix problem with stdout stream
// eslint-disable-next-line @typescript-eslint/no-empty-function
childProcess.stdout.on("data", (data) => {
lastOutput = data.toString();
});
childProcess.once("close", (code) => {
if (code === 0) {
resolve(code);
} else if (errorChunks.length === 0) {
reject(new Error(lastOutput));
} else {
reject(new Error(errorChunks.join("\n")));
}
});
});
}
export function runCommand(command: string, args?: string[]): Promise<number> {
const result = spawn(command, args);
return promisifyChildProcess(result);
}
export async function getFileStats(file: string): Promise<Stats | undefined> {
try {
return await stat(file);
} catch {
return undefined;
}
}
export async function writeContentToFile(file: string, content: string, flag = "a") {
const stats = await getFileStats(file);
if (stats) {
if (stats.isDirectory()) {
throw new Error(`Cannot write content to a folder (${file})`);
}
await writeFile(file, `\n${content.trim()}\n`, { flag });
} else {
await writeFile(file, `${content.trim()}\n`, { flag });
}
}
export async function readContentFromFile(file: string): Promise<string> {
const buffer = await readFile(file, { flag: "r" });
return buffer.toString();
}
export async function removeFile(file: string) {
await rm(file, { force: true });
}
export async function readCurrentDir() {
return readdir(currentDir, { withFileTypes: true });
}
export function formatJson(obj: Record<string, unknown>): string {
return JSON.stringify(obj, null, 2);
}