-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathexec.js
More file actions
43 lines (35 loc) · 1.12 KB
/
exec.js
File metadata and controls
43 lines (35 loc) · 1.12 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
import { resolve } from "path";
import { template, isNil, isPlainObject, isString } from "lodash-es";
import { execa } from "execa";
export default async function exec(
cmdProp,
{ shell, execCwd, ...config },
{ cwd, env, stdout, stderr, logger, ...context },
) {
const cmd = config[cmdProp] ? cmdProp : "cmd";
const cmdParsed = parseCommand(config[cmd]);
const script = template(cmdParsed.cmd)({ config, ...context });
const envInterpolated = {
...env,
...Object.entries(cmdParsed.env).reduce((acc, [key, value]) => {
acc[key] = template(value)({ config, ...context });
return acc;
}, {}),
};
logger.log("Call script %s", script);
const result = execa(script, {
shell: shell || true,
cwd: execCwd ? resolve(cwd, execCwd) : cwd,
env: envInterpolated,
});
result.stdout.pipe(stdout, { end: false });
result.stderr.pipe(stderr, { end: false });
return (await result).stdout.trim();
}
function parseCommand(cmd) {
if (isString(cmd)) {
return { cmd, env: {} };
} else if (isPlainObject(cmd) && !isNil(cmd.cmd)) {
return { cmd: cmd.cmd, env: cmd.env || {} };
}
}