-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
89 lines (66 loc) · 2.64 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
const core = require('@actions/core');
const exec = require('@actions/exec');
const path = require('path');
// 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32'.
const os = require('os');
const fs = require('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 unformattedShell = '';
let tmpPath = path.join(path.sep, 'tmp', 'knicknic', 'os-specific-run')
await fs.promises.mkdir(tmpPath, { recursive: true });
let file = path.join(tmpPath, uuidv4())
// https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#using-a-specific-shell
let platform = os.platform()
if(platform == 'darwin'){
command = core.getInput('macos')
unformattedShell = core.getInput('macosShell')
}
else if(platform == 'linux'){
command = core.getInput('linux')
unformattedShell = core.getInput('linuxShell')
} else if (platform == 'win32'){
command = core.getInput('windows');
unformattedShell = core.getInput('windowsShell')
} else{
core.setFailed("Unrecognized os " + platform)
}
let fileExtension = fileExtensions[unformattedShell] || ''
file = file+fileExtension
let shell = builtInShells[unformattedShell] || unformattedShell
let formattedShell = shell.format(file)
fs.writeFileSync(file, command)
core.info(`About to run command ${command}`)
const error_code = await exec.exec(formattedShell);
if(error_code != 0){
core.setFailed(`Failed with error code ${error_code}`)
}
}catch(error){
core.setFailed(error.message);
}
}
body()