-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
135 lines (121 loc) · 3.37 KB
/
mod.ts
File metadata and controls
135 lines (121 loc) · 3.37 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* Get the shell and arguments to use for executing commands in Deno.
*
* @returns - Array of shell and arguments.
*/
export function getExecPrefix(): string[] {
const envShell = Deno.env.get("SHELL");
if (envShell?.endsWith("pwsh.exe") || envShell?.endsWith("powershell.exe")) {
return [envShell, "-Command"];
} else if (envShell?.endsWith("cmd.exe")) {
return [envShell, "/C"];
} else if (envShell) {
return [envShell, "-c"];
} else {
return ["sh", "-c"];
}
}
/**
* Execute a shell command.
*
* @param command - The shell command to execute.
* @returns - The child process.
*/
export function exec(command: string): Deno.ChildProcess {
const [shell, ...args] = getExecPrefix();
const process = new Deno.Command(shell, {
args: [...args, command],
stdout: "piped",
stderr: "piped",
});
return process.spawn();
}
const resetEscapeCode = "\x1b[0m";
const colorEscapeCodes: Record<string, string> = {
"blue": "\x1b[34m",
"magenta": "\x1b[35m",
"orange": "\x1b[33m",
"cyan": "\x1b[36m",
"green": "\x1b[32m",
"lightBlue": "\x1b[94m",
"lightMagenta": "\x1b[95m",
"lightCyan": "\x1b[96m",
"lightOrange": "\x1b[93m",
"lightGreen": "\x1b[92m",
};
// bold and red
const errorEscapeCode = "\x1b[1m\x1b[31m";
function mergeStreams(streams: ReadableStream[], isStdErr = false) {
const readers = streams.map((stream) => stream.getReader());
let outputController: ReadableStreamDefaultController;
const processStream = (
reader: ReadableStreamDefaultReader,
index: number,
done: () => void,
) => {
reader.read().then(({ value, done: readerDone }) => {
if (readerDone) {
done();
} else {
const decoded = new TextDecoder().decode(value);
// const color = ''
const color = isStdErr ? errorEscapeCode : colorEscapeCodes[
Object.keys(
colorEscapeCodes,
)[index % Object.keys(colorEscapeCodes).length]
];
const encoded = new TextEncoder().encode(
`${color}[${index}] ${decoded}${resetEscapeCode}`,
);
outputController.enqueue(encoded);
processStream(reader, index, done);
}
});
};
return new ReadableStream({
start(controller) {
outputController = controller;
let completed = 0;
readers.forEach((reader, index) => {
processStream(reader, index, () => {
completed++;
if (completed === readers.length) {
controller.close();
}
});
});
},
});
}
/**
* Run concurrent shell commands.
*
* @param commands - Array of full shell commands to run.
*/
export const concurrently = async (
commands: string[] = [],
): Promise<void> => {
if (commands.length === 0) {
console.error("Please provide at least one shell command.");
Deno.exit(1);
}
const processes = await Promise.all(commands.map((command) => exec(command)));
mergeStreams(processes.map((process) => process.stdout)).pipeTo(
Deno.stdout.writable,
);
mergeStreams(processes.map((process) => process.stderr)).pipeTo(
Deno.stderr.writable,
);
const statuses = await Promise.allSettled(
processes.map((process) => process.status),
);
if (statuses.some(({ status }) => status === "rejected")) {
Deno.exit(1);
} else {
Deno.exit(0);
}
};
if (import.meta.main) {
concurrently(Deno.args);
}
export default concurrently;