generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
Copy pathhelpers.js
82 lines (75 loc) · 2.1 KB
/
helpers.js
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
// @ts-check
import { spawn } from 'child_process'
import * as core from "@actions/core"
import fs from 'fs'
import os from 'os'
/**
* @returns {boolean}
*/
export const useSudoPrefix = () => {
const input = core.getInput("sudo");
return input === "auto" ? os.userInfo().uid !== 0 : input === "true";
}
/**
* @param {string} cmd
* @returns {Promise<string>}
*/
export const execShellCommand = (cmd) => {
core.debug(`Executing shell command: [${cmd}]`)
return new Promise((resolve, reject) => {
const proc = process.platform !== "win32" ?
spawn(cmd, [], {
shell: true,
env: {
...process.env,
HOMEBREW_GITHUB_API_TOKEN: core.getInput('github-token') || undefined
}
}) :
spawn("C:\\msys64\\usr\\bin\\bash.exe", ["-lc", cmd], {
env: {
...process.env,
"MSYS2_PATH_TYPE": "inherit", /* Inherit previous path */
"CHERE_INVOKING": "1", /* do not `cd` to home */
"MSYSTEM": "MINGW64", /* include the MINGW programs in C:/msys64/mingw64/bin/ */
}
})
let stdout = ""
proc.stdout.on('data', (data) => {
process.stdout.write(data);
stdout += data.toString();
});
proc.stderr.on('data', (data) => {
process.stderr.write(data)
});
proc.on('exit', (code) => {
if (code !== 0) {
reject(new Error(code ? code.toString() : undefined))
}
resolve(stdout.trim())
});
});
}
/**
* @param {string} key
* @param {RegExp} re regex to use for validation
* @return {string} {undefined} or throws an error if input doesn't match regex
*/
export const getValidatedInput = (key, re) => {
const value = core.getInput(key);
if (value !== undefined && !re.test(value)) {
throw new Error(`Invalid value for '${key}': '${value}'`);
}
return value;
}
/**
* @return {Promise<string>}
*/
export const getLinuxDistro = async () => {
try {
const osRelease = await fs.promises.readFile("/etc/os-release")
const match = osRelease.toString().match(/^ID=(.*)$/m)
return match ? match[1] : "(unknown)"
} catch (e) {
return "(unknown)"
}
}