| 16 | |
| 17 | /**Metadata descriptor parser */ |
| 18 | export default async function metadata({log = true, diff = false} = {}) { |
| 19 | //Paths |
| 20 | const __metrics = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "../../..") |
| 21 | const __templates = path.join(__metrics, "source/templates") |
| 22 | const __plugins = path.join(__metrics, "source/plugins") |
| 23 | const __package = path.join(__metrics, "package.json") |
| 24 | const __descriptor = path.join(__metrics, "action.yml") |
| 25 | |
| 26 | //Init |
| 27 | const logger = log ? console.debug : () => null |
| 28 | |
| 29 | //Diff with latest version |
| 30 | if (diff) { |
| 31 | try { |
| 32 | previous = yaml.load(await fetch("https://raw.githubusercontent.com/lowlighter/metrics/latest/action.yml").then(response => response.text())) |
| 33 | } |
| 34 | catch (error) { |
| 35 | logger(error) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | //Load plugins metadata |
| 40 | let Plugins = {} |
| 41 | logger("metrics/metadata > loading plugins metadata") |
| 42 | for (const name of await fs.promises.readdir(__plugins)) { |
| 43 | if (!(await fs.promises.lstat(path.join(__plugins, name))).isDirectory()) |
| 44 | continue |
| 45 | switch (name) { |
| 46 | case "community": { |
| 47 | const ___plugins = path.join(__plugins, "community") |
| 48 | for (const name of await fs.promises.readdir(___plugins)) { |
| 49 | if (!(await fs.promises.lstat(path.join(___plugins, name))).isDirectory()) |
| 50 | continue |
| 51 | logger(`metrics/metadata > loading plugin metadata [community/${name}]`) |
| 52 | Plugins[name] = await metadata.plugin({__plugins: ___plugins, __templates, name, logger}) |
| 53 | Plugins[name].community = true |
| 54 | } |
| 55 | continue |
| 56 | } |
| 57 | default: |
| 58 | logger(`metrics/metadata > loading plugin metadata [${name}]`) |
| 59 | Plugins[name] = await metadata.plugin({__plugins, __templates, name, logger}) |
| 60 | } |
| 61 | } |
| 62 | //Reorder keys |
| 63 | const {base, core, ...plugins} = Plugins //eslint-disable-line no-unused-vars |
| 64 | Plugins = Object.fromEntries(Object.entries(Plugins).sort(([_an, a], [_bn, b]) => a.category === b.category ? (a.index ?? Infinity) - (b.index ?? Infinity) : categories.indexOf(a.category) - categories.indexOf(b.category))) |
| 65 | logger(`metrics/metadata > loaded [${Object.keys(Plugins).join(", ")}]`) |
| 66 | //Load templates metadata |
| 67 | let Templates = {} |
| 68 | logger("metrics/metadata > loading templates metadata") |
| 69 | for (const name of await fs.promises.readdir(__templates)) { |
| 70 | if (!(await fs.promises.lstat(path.join(__templates, name))).isDirectory()) |
| 71 | continue |
| 72 | logger(`metrics/metadata > loading template metadata [${name}]`) |
| 73 | Templates[name] = await metadata.template({__templates, name, plugins, logger}) |
| 74 | } |
| 75 | //Reorder keys |