feat(presets): add workflow [skip ci]

This commit is contained in:
lowlighter
2022-01-26 19:29:46 -05:00
parent 31538d94cc
commit ba72732262
3 changed files with 110 additions and 0 deletions

83
.github/scripts/presets_examples.mjs vendored Normal file
View File

@@ -0,0 +1,83 @@
//Imports
import processes from "child_process"
import yaml from "js-yaml"
import sgit from "simple-git"
import paths from "path"
import url from "url"
import fs from "fs/promises"
//Mode
const [mode = "dryrun"] = process.argv.slice(2)
console.log(`Mode: ${mode}`)
//Git setup
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..")
const __presets = paths.join(__metrics, ".presets")
if (!(await fs.lstat(paths.join(__presets))).isDirectory())
await sgit().clone("https://github.com/lowlighter/metrics.git", __presets, {"--branch":"presets", "--single-branch":true})
const git = sgit(__presets)
await git.pull()
const staged = new Set()
//Github action
const action = yaml.load(await fs.readFile(paths.join(__metrics, "action.yml"), "utf8"))
action.defaults = Object.fromEntries(Object.entries(action.inputs).map(([key, { default: value }]) => [key, value]))
action.input = vars => Object.fromEntries([...Object.entries(action.defaults), ...Object.entries(vars)].map(([key, value]) => [`INPUT_${key.toLocaleUpperCase()}`, value]))
action.run = async vars =>
await new Promise((solve, reject) => {
let [stdout, stderr] = ["", ""]
const env = { ...process.env, ...action.input(vars), GITHUB_REPOSITORY: "lowlighter/metrics" }
const child = processes.spawn("node", ["source/app/action/index.mjs"], { env })
child.stdout.on("data", data => stdout += data)
child.stderr.on("data", data => stderr += data)
child.on("close", code => {
if (code === 0)
return solve(stdout.match(/(?<svg><svg [\s\S]+<\/svg>)/)?.groups.svg ?? "")
console.log(stdout, stderr)
reject(stdout)
})
})
//Generate presets examples
for (const path of await fs.readdir(__presets)) {
if (/^[.@]/.test(path))
continue
if (!(await fs.lstat(paths.join(__presets, path))).isDirectory())
continue
const preset = path
//Example
console.log(`generating: ${preset}/example.svg`)
const svg = await action.run({config_presets:`@${preset}`, debug_print: true, plugins_errors_fatal: true, dryrun: true, use_mocked_data: true, verify: true, token:"MOCKED_TOKEN"})
await fs.writeFile(paths.join(__presets, path, "example.svg"), svg)
staged.add(paths.join(__presets, path, "example.svg"))
//Readme
console.log(`generating: ${preset}/README.svg`)
const {name, description} = await yaml.load(await fs.readFile(paths.join(__presets, preset, "preset.yml")))
await fs.writeFile(paths.join(__presets, path, "README.md"), `
<table>
<tr><th><h3>${name}</h3></th></tr>
<tr><td align="center"><p>${description}</p></td></tr>
<tr><td align="center">
<img src="example.svg">
<img width="900" height="1" alt="">
</td></tr>
</table>
`.trim())
staged.add(paths.join(__presets, path, "README.md"))
}
//Commit and push
if (mode === "publish") {
console.log(`Pushing staged changes: \n${[...staged].map(file => ` - ${file}`).join("\n")}`)
const gitted = await git
.addConfig("user.name", "github-actions[bot]")
.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com")
.add([...staged])
.commit("ci: auto-regenerate files")
.push("origin", "presets")
console.log(gitted)
}
console.log("Success!")

26
.github/workflows/examples.presets.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
name: Publish examples (presets)
on:
workflow_dispatch:
push:
branches:
- presets
jobs:
examples:
runs-on: ubuntu-latest
if: "github.repository == 'lowlighter/metrics'"
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup NodeJS
uses: actions/setup-node@v2
with:
node-version: 17
- name: Setup metrics
run: npm ci
- name: Publish presets examples
run: npm run presets -- publish

View File

@@ -7,6 +7,7 @@
"start": "node source/app/web/index.mjs",
"test": "jest --runInBand",
"build": "node .github/scripts/build.mjs",
"presets": "node .github/scripts/presets_examples.mjs",
"quickstart": "node .github/scripts/quickstart/index.mjs",
"preview": "node .github/scripts/preview.mjs",
"linter": "eslint source/**/*.mjs --quiet",