ci: update presets_examples
This commit is contained in:
32
.github/scripts/presets_examples.mjs
vendored
32
.github/scripts/presets_examples.mjs
vendored
@@ -5,6 +5,7 @@ import yaml from "js-yaml"
|
|||||||
import paths from "path"
|
import paths from "path"
|
||||||
import sgit from "simple-git"
|
import sgit from "simple-git"
|
||||||
import url from "url"
|
import url from "url"
|
||||||
|
import fetch from "node-fetch"
|
||||||
|
|
||||||
//Mode
|
//Mode
|
||||||
const [mode = "dryrun"] = process.argv.slice(2)
|
const [mode = "dryrun"] = process.argv.slice(2)
|
||||||
@@ -20,26 +21,20 @@ const git = sgit(__presets)
|
|||||||
await git.pull()
|
await git.pull()
|
||||||
const staged = new Set()
|
const staged = new Set()
|
||||||
|
|
||||||
//Github action
|
//Web instance
|
||||||
const action = yaml.load(await fs.readFile(paths.join(__metrics, "action.yml"), "utf8"))
|
const web = {}
|
||||||
action.defaults = Object.fromEntries(Object.entries(action.inputs).map(([key, { default: value }]) => [key, value]))
|
web.run = async vars => await fetch(`http://localhost:3000/lowlighter?${new url.URLSearchParams(Object.fromEntries(Object.entries(vars).map(([key, value]) => [key.replace(/^plugin_/, "").replace(/_/g, "."), value])))}`).then(response => response.text())
|
||||||
action.input = vars => Object.fromEntries([...Object.entries(action.defaults), ...Object.entries(vars)].map(([key, value]) => [`INPUT_${key.toLocaleUpperCase()}`, value]))
|
web.start = async () =>
|
||||||
action.run = async vars =>
|
new Promise(solve => {
|
||||||
await new Promise((solve, reject) => {
|
let stdout = ""
|
||||||
let [stdout, stderr] = ["", ""]
|
web.instance = processes.spawn("node", ["source/app/web/index.mjs"], { env: { ...process.env, SANDBOX: true } })
|
||||||
const env = { ...process.env, ...action.input(vars), GITHUB_REPOSITORY: "lowlighter/metrics" }
|
web.instance.stdout.on("data", data => (stdout += data, /Server ready !/.test(stdout) ? solve() : null))
|
||||||
const child = processes.spawn("node", ["source/app/action/index.mjs"], { env })
|
web.instance.stderr.on("data", data => console.error(`${data}`))
|
||||||
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 ?? `<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>`)
|
|
||||||
console.log(stdout, stderr)
|
|
||||||
reject(stdout)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
web.stop = async () => await web.instance.kill("SIGKILL")
|
||||||
|
|
||||||
//Generate presets examples
|
//Generate presets examples
|
||||||
|
await web.start()
|
||||||
for (const path of await fs.readdir(__presets)) {
|
for (const path of await fs.readdir(__presets)) {
|
||||||
if (/^[.@]/.test(path))
|
if (/^[.@]/.test(path))
|
||||||
continue
|
continue
|
||||||
@@ -49,7 +44,7 @@ for (const path of await fs.readdir(__presets)) {
|
|||||||
|
|
||||||
//Example
|
//Example
|
||||||
console.log(`generating: ${preset}/example.svg`)
|
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" })
|
const svg = await web.run({ config_presets: `@${preset}`, plugins_errors_fatal: true, verify: true, optimize: "css, xml" })
|
||||||
await fs.writeFile(paths.join(__presets, path, "example.svg"), svg)
|
await fs.writeFile(paths.join(__presets, path, "example.svg"), svg)
|
||||||
staged.add(paths.join(__presets, path, "example.svg"))
|
staged.add(paths.join(__presets, path, "example.svg"))
|
||||||
|
|
||||||
@@ -71,6 +66,7 @@ for (const path of await fs.readdir(__presets)) {
|
|||||||
)
|
)
|
||||||
staged.add(paths.join(__presets, path, "README.md"))
|
staged.add(paths.join(__presets, path, "README.md"))
|
||||||
}
|
}
|
||||||
|
await web.stop()
|
||||||
|
|
||||||
//Commit and push
|
//Commit and push
|
||||||
if (mode === "publish") {
|
if (mode === "publish") {
|
||||||
|
|||||||
Reference in New Issue
Block a user