forked from martinhaintz/ga-yaml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (64 loc) · 2.2 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
const core = require("@actions/core");
const fs = require("fs");
const yaml = require("js-yaml");
async function run() {
try {
const file = core.getInput("file", { required: true });
const key = core.getInput("key", { required: false });
const delimiter = core.getInput("delimiter", { required: false });
const returnToOutputs = core.getInput("return_to_outputs", {
required: false,
});
const esportToEnvs = core.getInput("export_to_envs", { required: false });
const content = yaml.load(fs.readFileSync(file, "utf8"));
if (key) {
const flattened_object = flattenObject(content);
core.setOutput("result", flattened_object[key]);
} else {
const flattened_object = flattenObject(content, false, delimiter);
const keys = Object.keys(flattened_object);
keys.forEach((current) => {
if (flattened_object[current] != null) {
if (returnToOutputs.toLowerCase() == "true")
core.setOutput(current, flattened_object[current]);
if (esportToEnvs.toLowerCase() == "true")
core.exportVariable(current, flattened_object[current]);
}
});
}
} catch (error) {
core.setFailed(error.message);
}
}
// Code from Stackoverflow Post: https://stackoverflow.com/a/59787588
// Author: https://stackoverflow.com/users/2977175/tofandel
/**
* @param ob Object The object to flatten
* @param prefix String (Optional) The prefix to add before each key, also used for recursion
**/
function flattenObject(ob, prefix = false, delimiter = ".", result = null) {
result = result || {};
// Preserve empty objects and arrays, they are lost otherwise
if (
prefix &&
typeof ob === "object" &&
ob !== null &&
Object.keys(ob).length === 0
) {
result[prefix] = Array.isArray(ob) ? [] : {};
return result;
}
prefix = prefix ? prefix + delimiter : "";
for (const i in ob) {
if (Object.prototype.hasOwnProperty.call(ob, i)) {
if (typeof ob[i] === "object" && ob[i] !== null) {
// Recursion on deeper objects
flattenObject(ob[i], prefix + i, delimiter, result);
} else {
result[prefix + i] = ob[i];
}
}
}
return result;
}
run();