Files
humans.txt/action.js
2019-12-20 12:27:20 +00:00

65 lines
1.7 KiB
JavaScript

'use strict'
const fs = require('fs')
const yaml = require('yaml')
const chalk = require('chalk')
const formatters = {
txt: txtFormatter,
json: jsonFormatter,
shell: (data, opts) => txtFormatter(data, { ...opts, colors: true}),
}
main()
function main() {
const format = process.argv[2] || ((process.env.GITHUB_ACTION || process.stdout.isTTY) ? "shell" : "txt")
const output = process.argv[3] ? fs.openSync(process.argv[3], "w+") : 1;
const formatter = formatters[format]
if (!formatter) {
invalidFormat(format)
return
}
const data = yaml.parse(fs.readFileSync("./humans.txt.yaml", {encoding: "utf8"}))
formatter(data, {output})
}
function invalidFormat(format) {
const list = Object.keys(formatters).sort().join(",")
console.error(`'${format}' is not one of the accepted formats ${list}`)
process.statusCode = 1
return
}
function jsonFormatter({humans, output}) {
fs.writeSync(output, JSON.stringify(humans, null, 4))
}
function txtFormatter({humans}, {colors = false, output} = {}) {
let active = humans.filter(h => !h.alum).map(h => h.name)
let alum = humans.filter(h => h.alum).map(h => h.name)
if(colors) {
const total = active.length + alum.length
active = mapColorRange(active, 0, total)
alum = mapColorRange(alum, active.length, total)
}
fs.writeSync(output, "Current humans\n")
fs.writeSync(output,"==============\n\n")
fs.writeSync(output,active.join("\n"))
fs.writeSync(output,"\n\n")
fs.writeSync(output,"Human alumni\n")
fs.writeSync(output,"============\n\n")
fs.writeSync(output,alum.join("\n"))
}
function mapColorRange(strings, base, total) {
return strings.map((n, i) => chalk.hsl(((i + base) / total) * 360, 100, 50)(n))
}