-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
98 lines (75 loc) · 2.9 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { getInput, setFailed, info } from '@actions/core';
import { exec as _exec } from '@actions/exec';
import { join, sep } from 'path';
// 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.
import { platform as _platform } from 'os';
import { promises, writeFileSync } from 'fs';
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// add format because that seems to be how github does formatting
String.prototype.format = function () {
var a = this;
for (var k in arguments) {
a = a.replace(new RegExp("\\{" + k + "\\}", 'g'), arguments[k]);
}
return a
}
let fileExtensions = { cmd: '.cmd', pwsh: '.ps1', powershell: '.ps1' }
let builtInShells = {
bash: 'bash --noprofile --norc -eo pipefail {0}',
pwsh: 'pwsh -command "& \'{0}\'"',
python: 'python {0}',
sh: 'sh -e {0}',
cmd: 'cmd.exe /D /E:ON /V:OFF /S /C "CALL "{0}""',
powershell: 'powershell -command "& \'{0}\'"',
}
async function body() {
try {
let command = '';
let working_directory = getInput('working_directory');
let unformattedShell = '';
let file = null;
let temp_working_directory = null;
if (working_directory == "") {
temp_working_directory = join(sep, 'tmp', 'carlkidcrypto', 'os-specific-runner')
}
else {
working_directory = "/tmp/" + working_directory;
temp_working_directory = join(sep, working_directory)
}
await promises.mkdir(temp_working_directory, { recursive: true });
file = join(temp_working_directory, uuidv4())
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell
let platform = _platform()
if (platform == 'darwin') {
command = getInput('macos')
unformattedShell = getInput('macosShell')
}
else if (platform == 'linux') {
command = getInput('linux')
unformattedShell = getInput('linuxShell')
} else if (platform == 'win32') {
command = getInput('windows');
unformattedShell = getInput('windowsShell')
} else {
setFailed("Unrecognized os " + platform)
}
let fileExtension = fileExtensions[unformattedShell] || ''
file = file + fileExtension
let shell = builtInShells[unformattedShell] || unformattedShell
let formattedShell = shell.format(file)
writeFileSync(file, command)
info(`About to run command ${command}`)
const error_code = await _exec(formattedShell);
if (error_code != 0) {
setFailed(`Failed with error code ${error_code}`)
}
} catch (error) {
setFailed(error.message);
}
}
body()