Files
humans.txt/action.js

77 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2019-12-19 14:53:51 +00:00
'use strict'
const fs = require('fs')
const yaml = require('yaml')
2019-12-19 15:05:06 +00:00
const chalk = require('chalk')
2019-12-19 14:53:51 +00:00
const formatters = {
txt: txtFormatter,
2019-12-20 12:31:00 +00:00
html: htmlFormatter,
2019-12-19 14:53:51 +00:00
json: jsonFormatter,
2019-12-20 12:27:20 +00:00
shell: (data, opts) => txtFormatter(data, { ...opts, colors: true}),
2019-12-19 14:53:51 +00:00
}
main()
function main() {
2019-12-19 17:27:36 +00:00
const format = process.argv[2] || ((process.env.GITHUB_ACTION || process.stdout.isTTY) ? "shell" : "txt")
2019-12-20 12:27:20 +00:00
const output = process.argv[3] ? fs.openSync(process.argv[3], "w+") : 1;
2019-12-19 14:53:51 +00:00
const formatter = formatters[format]
if (!formatter) {
invalidFormat(format)
return
}
2019-12-20 13:11:47 +00:00
const data = yaml.parse(fs.readFileSync(__dirname + "/humans.txt.yaml", {encoding: "utf8"}))
2019-12-19 14:53:51 +00:00
2019-12-20 13:17:12 +00:00
data.humans = data.humans.sort((a,b) => a.name > b.name ? 1 : -1)
2019-12-20 12:27:20 +00:00
formatter(data, {output})
2019-12-19 14:53:51 +00:00
}
function invalidFormat(format) {
const list = Object.keys(formatters).sort().join(",")
console.error(`'${format}' is not one of the accepted formats ${list}`)
2019-12-20 12:49:42 +00:00
process.exitCode = 1
2019-12-19 14:53:51 +00:00
return
}
2019-12-20 12:42:04 +00:00
function jsonFormatter({humans}, {output}) {
2019-12-20 12:27:20 +00:00
fs.writeSync(output, JSON.stringify(humans, null, 4))
2019-12-19 14:53:51 +00:00
}
2019-12-20 12:27:20 +00:00
function txtFormatter({humans}, {colors = false, output} = {}) {
2019-12-19 14:53:51 +00:00
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
2019-12-19 15:05:06 +00:00
active = mapColorRange(active, 0, total)
alum = mapColorRange(alum, active.length, total)
2019-12-19 14:53:51 +00:00
}
2019-12-20 12:27:20 +00:00
fs.writeSync(output, "Current humans\n")
fs.writeSync(output,"==============\n\n")
fs.writeSync(output,active.join("\n"))
fs.writeSync(output,"\n\n")
2019-12-19 14:53:51 +00:00
2019-12-20 12:27:20 +00:00
fs.writeSync(output,"Human alumni\n")
fs.writeSync(output,"============\n\n")
fs.writeSync(output,alum.join("\n"))
2019-12-19 14:53:51 +00:00
}
2019-12-19 15:05:06 +00:00
function mapColorRange(strings, base, total) {
return strings.map((n, i) => chalk.hsl(((i + base) / total) * 360, 100, 50)(n))
}
2019-12-19 14:53:51 +00:00
2019-12-20 12:31:00 +00:00
function htmlFormatter(data, opts) {
fs.writeSync(opts.output, `<!doctype html>
<meta charset='utf8' />
2019-12-20 13:11:47 +00:00
<link rel="icon" type="image/x-icon" href="favicon.ico">
2019-12-20 12:31:00 +00:00
<title>Actions - humans.txt</title>
2019-12-20 13:11:47 +00:00
<body><pre>`)
2019-12-20 12:31:00 +00:00
txtFormatter(data, opts)
fs.writeSync(opts.output, "</pre></body>")
}