deno.land / x / velociraptor@1.5.0 / src / run_commands.ts

run_commands.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
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
136
import { kill } from "../deps.ts";import { getEnvVars } from "./env.ts";import { escape, isWindows, notNull, OneOrMore } from "./util.ts";import { log } from "./logger.ts";import { Command, CompoundCommandItem, isParallel, ParallelCommands,} from "./command.ts";import { buildCommandString } from "./build_command_string.ts";import { ArgsForwardingMode } from "./run_script.ts";
const runningProcesses: Set<Deno.Process> = new Set();
interface RunCommandsOptions { shell: string; cwd: string; commands: CompoundCommandItem[]; prefix?: string; additionalArgs?: string[]; argsForwardingMode?: ArgsForwardingMode;}
export async function runCommands({ shell, cwd, commands, prefix, additionalArgs, argsForwardingMode,}: RunCommandsOptions): Promise<void> { const _runCommands = async ( commands: OneOrMore<CompoundCommandItem>, ): Promise<unknown> => { if (!commands) return; if (Array.isArray(commands)) { for (let command of commands) { await _runCommands(command); } } else { if (isParallel(commands)) { return Promise.all(commands.pll.map((c) => _runCommands(c))); } const command = commands as Command; return runCommand({ shell, cwd, command, prefix, additionalArgs, argsForwardingMode, }); } }; try { await _runCommands(commands as Array<Command | ParallelCommands>); } catch (e) { runningProcesses.forEach((p) => { kill(p.pid, { force: true, tree: true }); p.close(); }); throw e; }}
type RunCommandOptions = Omit<RunCommandsOptions, "commands"> & { command: Command;};
async function runCommand({ shell, cwd, command, prefix, additionalArgs, argsForwardingMode,}: RunCommandOptions): Promise<void> { const cmd = buildCommandString(command); const runOptions: Deno.RunOptions = { cmd: [ shell, ...buildShellArgs({ shell, command: cmd, prefix, additionalArgs, argsForwardingMode, }), ], cwd, env: getEnvVars(command), }; log.debug(`Running ${JSON.stringify(runOptions, null, " ")}`); const process = Deno.run(runOptions); runningProcesses.add(process); const status = await process.status(); process.close(); runningProcesses.delete(process); if (status.code !== 0) { throw new Error(`Command returned error code ${status.code}`); }}
type BuildShellArgsOptions = Omit<RunCommandOptions, "command" | "cwd"> & { command: string;};
function buildShellArgs({ shell, command, prefix, additionalArgs, argsForwardingMode,}: BuildShellArgsOptions): string[] { const cmd = [ prefix, command, argsForwardingMode === ArgsForwardingMode.DIRECT && additionalArgs && additionalArgs.length > 0 ? additionalArgs.map((a) => `"${escape(a, '"')}"`).join(" ") : null, ].filter(notNull) .join(" "); if (isWindows && /^(?:.*\\)?cmd(?:\.exe)?$/i.test(shell)) { return ["/d", "/s", "/c", cmd]; } return [ "-c", cmd, ...(argsForwardingMode === ArgsForwardingMode.INDIRECT && additionalArgs ? additionalArgs : []), ];}
velociraptor

Version Info

Tagged at
2 years ago