From ba7273226251a579f5d40bd00004918d48f9af18 Mon Sep 17 00:00:00 2001 From: lowlighter <22963968+lowlighter@users.noreply.github.com> Date: Wed, 26 Jan 2022 19:29:46 -0500 Subject: [PATCH] feat(presets): add workflow [skip ci] --- .github/scripts/presets_examples.mjs | 83 ++++++++++++++++++++++++++ .github/workflows/examples.presets.yml | 26 ++++++++ package.json | 1 + 3 files changed, 110 insertions(+) create mode 100644 .github/scripts/presets_examples.mjs create mode 100644 .github/workflows/examples.presets.yml diff --git a/.github/scripts/presets_examples.mjs b/.github/scripts/presets_examples.mjs new file mode 100644 index 00000000..87b098f3 --- /dev/null +++ b/.github/scripts/presets_examples.mjs @@ -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(/(?)/)?.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"), ` + + + + +

${name}

${description}

+ + +
+`.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!") \ No newline at end of file diff --git a/.github/workflows/examples.presets.yml b/.github/workflows/examples.presets.yml new file mode 100644 index 00000000..77fb7542 --- /dev/null +++ b/.github/workflows/examples.presets.yml @@ -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 + + diff --git a/package.json b/package.json index 78613d4d..af7ff55e 100644 --- a/package.json +++ b/package.json @@ -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",