-
Notifications
You must be signed in to change notification settings - Fork 45
/
action.js
76 lines (60 loc) · 2.09 KB
/
action.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
72
73
74
75
76
'use strict'
const fs = require('fs')
const yaml = require('yaml')
const chalk = require('chalk')
const formatters = {
txt: txtFormatter,
html: htmlFormatter,
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(__dirname + "/humans.txt.yaml", {encoding: "utf8"}))
data.humans = data.humans.sort((a,b) => a.name > b.name ? 1 : -1)
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.exitCode = 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))
}
function htmlFormatter(data, opts) {
fs.writeSync(opts.output, `<!doctype html>
<meta charset='utf8' />
<link rel="icon" type="image/x-icon" href="favicon.ico">
<title>Actions - humans.txt</title>
<body><pre>`)
txtFormatter(data, opts)
fs.writeSync(opts.output, "</pre></body>")
}