Version 1.9 (#6)

This commit is contained in:
Simon Lecoq
2020-10-20 00:19:58 +02:00
committed by GitHub
parent 047d8630e6
commit f21273172d
23 changed files with 454 additions and 860 deletions

67
src/setup.mjs Normal file
View File

@@ -0,0 +1,67 @@
//Imports
import fs from "fs"
import path from "path"
/** Setup */
export default async function () {
//Init
console.debug(`metrics/setup > setup`)
const templates = "src/templates"
const conf = {
templates:{},
settings:{},
statics:path.resolve("src/html")
}
//Load settings
console.debug(`metrics/setup > load settings.json`)
if (fs.existsSync(path.resolve("settings.json"))) {
conf.settings = JSON.parse(`${await fs.promises.readFile(path.resolve("settings.json"))}`)
console.debug(`metrics/setup > load settings.json > success`)
}
else
console.debug(`metrics/setup > load settings.json > (missing)`)
if (!conf.settings.templates)
conf.settings.templates = {default:"classic", enabled:[]}
if (conf.settings.debug)
console.debug(conf.settings)
//Load templates
if (fs.existsSync(path.resolve(templates))) {
for (const name of await fs.promises.readdir(templates)) {
//Cache templates
if (/^index.mjs$/.test(name))
continue
console.debug(`metrics/setup > load template [${name}]`)
const files = [
`${templates}/${name}/query.graphql`,
`${templates}/${name}/image.svg`,
`${templates}/${name}/style.css`,
]
const [query, image, style] = await Promise.all(files.map(async file => `${await fs.promises.readFile(path.resolve(file))}`))
conf.templates[name] = {query, image, style}
console.debug(`metrics/setup > load template [${name}] > success`)
//Debug
if (conf.settings.debug) {
Object.defineProperty(conf.templates, name, {
get() {
console.debug(`metrics/setup > reload template [${name}]`)
const [query, image, style] = files.map(file => `${fs.readFileSync(path.resolve(file))}`)
console.debug(`metrics/setup > reload template [${name}] > success`)
return {query, image, style}
}
})
}
}
}
else {
console.debug(`metrics/setup > load templates from build`)
conf.templates = JSON.parse(Buffer.from(`<#assets>`, "base64").toString("utf8"))
}
//Conf
console.debug(`metrics/setup > setup > success`)
return conf
}