forked from antifree/json-to-variables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (38 loc) · 1.3 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
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') || '';
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 key in variable) {
if(rootObj.hasOwnProperty(key)){
processVariable(variable[key], key);
}
else {
processVariable(variable[key], `${name}_${key}`);
}
}
}
else {
core.info(`SET ENV '${prefix}${name}' = ${variable}`);
core.exportVariable(name, variable.toString());
}
};
processVariable(rootObj);
} catch (error) {
core.setFailed(error.message);
}