-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec-utils.ts
43 lines (35 loc) · 1.21 KB
/
exec-utils.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
import { exec } from 'child_process';
import { promisify } from 'util';
export const execPromise = promisify(exec);
export type EnvironmentVariables = Record<string, string>;
export interface Command {
command: string;
env?: EnvironmentVariables;
}
export type CommandSet = Array<Command>;
export const envToString = (env: EnvironmentVariables): string => {
const parts: Array<string> = [];
for (const [key, value] of Object.entries(env)) {
parts.push(`${key}="${value}"`);
}
return parts.join(' ');
};
export const commandToString = (command: Command): string => {
if (command.env && Object.keys(command.env).length) {
return `${envToString(command.env)} ${command.command}`;
} else {
return command.command;
}
};
export const commandSetToString = (commandSet: CommandSet): string => {
const parts = [];
for (const command of commandSet) {
parts.push(commandToString(command));
}
return parts.join(' && ');
};
export const executeCommand = async (commandSet: CommandSet): Promise<void> => {
console.error(`Running: ${commandSetToString(commandSet)}`);
await execPromise(commandSetToString(commandSet));
console.error('Done');
};