forked from antifree/json-to-variables
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
46 lines (37 loc) · 1.28 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
const fs = require('fs');
const path = require('path');
const core = require('@actions/core');
try {
const fileName = core.getInput('filename', { required: true });
const prefix = core.getInput('prefix') || 'json';
const masked = (core.getInput('masked') || 'false') === 'true';
const fullPath = path.resolve(fileName);
core.info(`Processing file: ${fullPath}`);
const rawdata = fs.readFileSync(fullPath);
const rootObj = JSON.parse(rawdata);
const processVariable = (variable, name) => {
if (typeof variable === 'undefined' || variable === null) {
return;
}
if (Array.isArray(variable)) {
variable.forEach((value, index) => {
processVariable(value, `${name}_${index}`);
});
}
else if (typeof variable === 'object') {
for (const field in variable) {
processVariable(variable[field], `${name}_${field}`);
}
}
else {
if (masked) {
core.setSecret(variable);
}
core.info(`SET ENV '${name}' = ${variable}`);
core.exportVariable(name, variable.toString());
}
};
processVariable(rootObj, prefix);
} catch (error) {
core.setFailed(error.message);
}