-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (28 loc) · 973 Bytes
/
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
const core = require('@actions/core');
function replaceVars(instring, vars) {
if (typeof instring !== 'string') {
throw Error('Non-string input given');
}
if (!(vars instanceof Object)) {
throw Error('Variable configuration is not an object');
}
const replacer = (match, varname) => vars[varname];
const regexStr = `\\{(${Object.keys(vars).join('|')})\\}`;
const regex = new RegExp(regexStr, 'gu');
const result = instring.replace(regex, replacer);
return result;
}
try {
const prefix = core.getInput('prefix');
const variables = {};
Object.keys(process.env)
.filter(key => key.startsWith(prefix))
.forEach(key => (
variables[key.slice(prefix.length)] = process.env[key]
));
const input = core.getInput('instring');
core.setOutput('outstring', replaceVars(input, variables));
} catch (error) {
core.setFailed(error.message);
}
module.exports = replaceVars;