Skip to content

Instantly share code, notes, and snippets.

@thislooksfun
Created December 5, 2019 21:03
Show Gist options
  • Save thislooksfun/90fdb0cb616249085d307221c9d5966c to your computer and use it in GitHub Desktop.
Save thislooksfun/90fdb0cb616249085d307221c9d5966c to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const DEFAULT_KEY = "out";
function setOutput(name, val) {
console.log(`::set-output name=${name}::${val}`);
}
function parseAndOutput(data) {
try {
flattenAndOutput("", JSON.parse(data));
} catch (e) {
console.log(e);
// Something went wrong, just return the input
return setOutput(DEFAULT_KEY, data);
}
}
function flattenAndOutput(keypath, data) {
if (data == null) return;
if (typeof data != "object") {
// Not an object or array, just return it
return setOutput(keypath || DEFAULT_KEY, data);
}
if (Array.isArray(data)) {
const root = keypath || DEFAULT_KEY;
flattenAndOutput(`${root}.len`, data.length);
for (let i = 0; i < data.length; i++) {
flattenAndOutput(`${root}[${i}]`, data[i]);
}
return;
}
for (const key in data) {
const kp = keypath ? `${keypath}.${key}` : key;
flattenAndOutput(kp, data[key]);
}
}
parseAndOutput(process.argv[2]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment