69 řádky
1.9 KiB
JavaScript
Executable File
69 řádky
1.9 KiB
JavaScript
Executable File
import yaml from 'js-yaml'
|
|
import fs from 'fs'
|
|
import jsonata from 'jsonata'
|
|
import fetch from 'node-fetch'
|
|
|
|
import * as dotenv from 'dotenv'
|
|
dotenv.config()
|
|
|
|
const SOURCE_FILE = process.env.GWEICZ_SYSINFO_SPEC || './spec/services.yaml'
|
|
|
|
const outputFn = 'build/spec.json'
|
|
|
|
function defaultOptions(conf) {
|
|
const opts = {}
|
|
if (conf.auth && conf.auth.type === 'basic') {
|
|
opts.headers = {
|
|
Authorization: `Basic ${Buffer.from(process.env[conf.auth.env]).toString('base64')}`
|
|
}
|
|
}
|
|
return opts
|
|
}
|
|
|
|
const strategies = {
|
|
async default (conf, all) {
|
|
const req = await fetch(conf.url, defaultOptions(conf))
|
|
const resp = await req.json()
|
|
return jsonata(conf.query).evaluate(resp)
|
|
},
|
|
async github (conf, all) {
|
|
const req = await fetch('https://api.github.com/repos/@@/releases/latest'.replace(/@@/, all.repo))
|
|
const resp = await req.json()
|
|
return jsonata('$match(tag_name,/^v(.+)/)[0].groups[0]').evaluate(resp)
|
|
},
|
|
async html (conf, all) {
|
|
const req = await fetch(conf.url)
|
|
const resp = await req.text()
|
|
return resp.match(conf.query)[1]
|
|
}
|
|
}
|
|
|
|
async function getVersions (config, all) {
|
|
const local = await strategies[config.from.strategy ? config.from.strategy : 'default'](config.from, all)
|
|
const latest = await strategies[config.to.strategy ? config.to.strategy : 'default'](config.to, all)
|
|
return { local, latest }
|
|
}
|
|
|
|
export default async function build (src = SOURCE_FILE) {
|
|
console.log(`Source: ${src}`)
|
|
|
|
const spec = yaml.load(fs.readFileSync(src))
|
|
|
|
for (const item of spec.services) {
|
|
|
|
item.url = item.host ? `https://${item.host}` : item.url
|
|
if (item.version_conf) {
|
|
item.versions = await getVersions(item.version_conf, item)
|
|
}
|
|
delete item.version_conf
|
|
}
|
|
|
|
spec.time = new Date
|
|
|
|
fs.writeFileSync(outputFn, JSON.stringify(spec, null, 2))
|
|
console.log(`saved: ${outputFn}`)
|
|
console.log('build done')
|
|
|
|
return spec
|
|
}
|