Skip to content

Commit 78eb3e5

Browse files
committed
repo: reorder .github
1 parent 43dac8a commit 78eb3e5

20 files changed

+157
-162
lines changed

.github/examples.mjs

-82
This file was deleted.

.github/index.mjs

-65
This file was deleted.

.github/scripts/build.mjs

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//Imports
2+
import ejs from "ejs"
3+
import fs from "fs/promises"
4+
import fss from "fs"
5+
import paths from "path"
6+
import url from "url"
7+
import sgit from "simple-git"
8+
import metadata from "../source/app/metrics/metadata.mjs"
9+
import yaml from "js-yaml"
10+
11+
//Mode
12+
const [mode = "dryrun"] = process.argv.slice(2)
13+
console.log(`Mode: ${mode}`)
14+
15+
//Paths
16+
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..")
17+
const __action = paths.join(__metrics, "source/app/action")
18+
const __web = paths.join(__metrics, "source/app/web")
19+
const __readme = paths.join(__metrics, ".github/readme")
20+
const __templates = paths.join(paths.join(__metrics, "source/templates/"))
21+
const __plugins = paths.join(paths.join(__metrics, "source/plugins/"))
22+
const __test_plugins = paths.join(paths.join(__metrics, "tests/plugins"))
23+
const __test_secrets = paths.join(paths.join(__metrics, "tests/secrets.json"))
24+
25+
//Git setup
26+
const git = sgit(__metrics)
27+
const staged = new Set()
28+
29+
//Config and general documentation auto-generation
30+
for (const step of ["config", "documentation"]) {
31+
32+
//Load plugins metadata
33+
const {plugins, templates, packaged, descriptor} = await metadata({log:false})
34+
35+
//Update generated files
36+
async function update({source, output, options = {}}) {
37+
//Regenerate file
38+
console.log(`Generating ${output}`)
39+
const content = await ejs.renderFile(source, {plugins, templates, packaged, descriptor}, {async:true, ...options})
40+
//Save result
41+
const file = paths.join(__metrics, output)
42+
await fs.writeFile(file, content)
43+
//Add to git
44+
staged.add(file)
45+
}
46+
47+
//Templating
48+
switch (step) {
49+
case "config":
50+
await update({source:paths.join(__action, "action.yml"), output:"action.yml"})
51+
await update({source:paths.join(__web, "settings.example.json"), output:"settings.example.json"})
52+
break
53+
case "documentation":
54+
await update({source:paths.join(__readme, "README.md"), output:"README.md", options:{root:__readme}})
55+
await update({source:paths.join(__readme, "partials/documentation/plugins.md"), output:"source/plugins/README.md"})
56+
await update({source:paths.join(__readme, "partials/documentation/templates.md"), output:"source/templates/README.md"})
57+
break
58+
}
59+
}
60+
61+
{
62+
//Load plugins metadata and secrets
63+
const {plugins, templates} = await metadata({log:false, diff:true})
64+
const secrets = Object.assign(JSON.parse(`${await fs.readFile(__test_secrets)}`), {$regex:/\$\{\{\s*secrets\.(?<secret>\w+)\s*\}\}/})
65+
66+
//Get plugin infos
67+
async function plugin(id) {
68+
const path = paths.join(__plugins, id)
69+
const readme = paths.join(path, "README.md")
70+
const examples = paths.join(path, "examples.yml")
71+
const tests = paths.join(__test_plugins, `${id}.yml`)
72+
return {
73+
readme:{
74+
path:readme,
75+
content:`${await fs.readFile(readme)}`
76+
},
77+
tests:{
78+
path:tests
79+
},
80+
examples:fss.existsSync(examples) ? yaml.load(await fs.readFile(examples), "utf8") ?? [] : [],
81+
options:plugins[id].readme.table
82+
}
83+
}
84+
85+
//Plugins
86+
for (const id of Object.keys(plugins)) {
87+
const {examples, options, readme, tests} = await plugin(id)
88+
89+
//Plugin readme
90+
await fs.writeFile(readme.path, readme.content
91+
.replace(/(<!--examples-->)[\s\S]*(<!--\/examples-->)/g, `$1\n${examples.map(({test, prod, ...step}) => ["```yaml", yaml.dump(step), "```"].join("\n")).join("\n")}\n$2`)
92+
.replace(/(<!--options-->)[\s\S]*(<!--\/options-->)/g, `$1\n${options}\n$2`)
93+
)
94+
console.log(`Generating ${readme.path}`)
95+
96+
//Plugin tests
97+
await fs.writeFile(tests.path, yaml.dump(examples.map(({prod, test = {}, name = "", ...step}) => {
98+
if (test.skip)
99+
return null
100+
const result = {name:`${plugins[id].name} - ${name}`, ...step, ...test}
101+
test.with ??= {}
102+
for (const [k, v] of Object.entries(result.with)) {
103+
if (k in test.with)
104+
result.with[k] = test.with[k]
105+
if (secrets.$regex.test(v))
106+
result.with[k] = v.replace(secrets.$regex, secrets[v.match(secrets.$regex)?.groups?.secret])
107+
}
108+
if (!result.with.base)
109+
delete result.with.base
110+
delete result.with.filename
111+
return result
112+
}).filter(t => t)))
113+
console.log(`Generating ${tests.path}`)
114+
115+
}
116+
117+
}
118+
119+
//Commit and push
120+
if (mode === "publish") {
121+
console.log(`Pushing staged changes: \n${[...staged].map(file => ` - ${file}`).join("\n")}`)
122+
const gitted = await git
123+
.addConfig("user.name", "github-actions[bot]")
124+
.addConfig("user.email", "41898282+github-actions[bot]@users.noreply.github.com")
125+
.add([...staged])
126+
.commit("ci: auto-regenerate files")
127+
.push("origin", "master")
128+
console.log(gitted)
129+
}
130+
console.log("Success!")
File renamed without changes.
File renamed without changes.

.github/quickstart/index.mjs renamed to .github/scripts/quickstart/index.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import ejs from "ejs"
88
const [mode, name] = process.argv.slice(2)
99

1010
//Paths
11-
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..")
12-
const __quickstart = paths.join(__metrics, ".github/quickstart")
11+
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../../..")
12+
const __quickstart = paths.join(__metrics, ".github/scripts/quickstart")
1313

1414
//Check arguments
1515
if ((!mode)||(!name))
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
- name: <%= `${name.charAt(0).toLocaleUpperCase()}${name.substring(1)}` %> plugin (default)
22
uses: lowlighter/metrics@latest
33
with:
4-
token: MOCKED_TOKEN
5-
plugin_<%= name %>: yes
4+
filename: metrics.plugin.<%= name %>.svg
5+
token: ${{ secrets.METRICS_TOKEN }}
6+
base: ""
7+
plugin_<%= name %>: yes

.github/release.mjs renamed to .github/scripts/release.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import url from "url"
55
import sgit from "simple-git"
66

77
//Git setup
8-
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "..")
8+
const __metrics = paths.join(paths.dirname(url.fileURLToPath(import.meta.url)), "../..")
99
const git = sgit(__metrics)
1010

1111
//Setup octokit

.github/workflows/workflow.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
- name: Setup metrics
116116
run: npm ci
117117
- name: Publish rebuild metrics indexes
118-
run: npm run index -- publish
118+
run: npm run build -- publish
119119

120120
# Rebase main branch on master
121121
update-main:
@@ -269,7 +269,7 @@ jobs:
269269
- name: Setup metrics
270270
run: npm ci
271271
- name: Publish release
272-
run: node .github/release.mjs
272+
run: node .github/scripts/release.mjs
273273
env:
274274
GITHUB_TOKEN: ${{ github.token }}
275275
GITHUB_REPOSITORY: ${{ github.repository }}

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ RUN chmod +x /metrics/source/app/action/index.mjs \
2424
&& rm -rf /var/lib/apt/lists/* \
2525
# Install node modules and rebuild indexes
2626
&& npm ci \
27-
&& npm run index
27+
&& npm run build
2828

2929
# Environment variables
3030
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"scripts": {
77
"start": "node source/app/web/index.mjs",
88
"test": "jest --runInBand",
9-
"index": "node .github/index.mjs",
10-
"quickstart": "node .github/quickstart/index.mjs",
11-
"preview": "node .github/preview.mjs",
9+
"build": "node .github/scripts/build.mjs",
10+
"quickstart": "node .github/scripts/quickstart/index.mjs",
11+
"preview": "node .github/scripts/preview.mjs",
1212
"linter": "eslint source/**/*.mjs --quiet",
1313
"format": "eslint source/**/*.mjs --fix",
1414
"dev": "nodemon source/app/web/index.mjs -e mjs,css,ejs,json",

tests/ci.test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ describe("Check files editions (checkout your files if needed)", () => {
2727
".github/readme/partials/documentation/compatibility.md",
2828
".github/readme/partials/introduction.md",
2929
".github/workflows/*",
30+
".github/scripts/*",
3031
".github/FUNDING.yml",
31-
".github/examples.mjs",
32-
".github/index.mjs",
33-
".github/release.mjs",
34-
".github/markdown_example.mjs",
3532
".github/architecture.svg",
3633
"LICENSE",
3734
"ARCHITECTURE.md",

tests/secrets.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"METRICS_TOKEN":"MOCKED_TOKEN",
3+
"METRICS_BOT_TOKEN":"MOCKED_TOKEN",
4+
"PAGESPEED_TOKEN":"MOCKED_TOKEN",
5+
"SPOTIFY_TOKENS":"MOCKED_CLIENT_ID, MOCKED_CLIENT_SECRET, MOCKED_REFRESH_TOKEN",
6+
"YOUTUBE_MUSIC_TOKENS":"SAPISID=MOCKED_COOKIE; OTHER_PARAM=OTHER_VALUE;",
7+
"LASTFM_TOKEN":"MOCKED_TOKEN",
8+
"NIGHTSCOUT_URL":"https://testapp.herokuapp.com/",
9+
"WAKATIME_TOKEN":"MOCKED_TOKEN",
10+
"WAKATIME_TOKEN_NO_PROJECTS":"MOCKED_TOKEN_NO_PROJECTS",
11+
"TWITTER_TOKEN":"MOCKED_TOKEN",
12+
"RAPIDAPI_TOKEN":"MOCKED_TOKEN"
13+
}

0 commit comments

Comments
 (0)