summaryrefslogtreecommitdiff
path: root/docs/src/setup-script-generation-plugin/index.js
blob: 908ac6597d2f46fe6c3cbb0c8a078bb0d89cf41f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Copyright (c) 2021 The ZMK Contributors
 *
 * SPDX-License-Identifier: MIT
 */

var PrebuildPlugin = require("prebuild-webpack-plugin");
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const yaml = require("js-yaml");
const Mustache = require("mustache");

function generateSetupScripts() {
  return glob("../app/boards/**/*.zmk.yml", (error, files) => {
    const aggregated = files.map((f) => ({
      ...yaml.load(fs.readFileSync(f, "utf8")),
      __base_dir: path.basename(path.dirname(f)),
    }));

    const data = aggregated.reduce(
      (agg, item) => {
        switch (item.type) {
          case "shield":
            item.compatible = true;
            item.split = item.siblings?.length > 1;
            agg.keyboards.push(item);
            break;
          case "board":
            if (item.features?.includes("keys")) {
              agg.keyboards.push(item);
            } else {
              agg.boards.push(item);
            }
            break;
        }
        return agg;
      },
      { keyboards: [], boards: [] }
    );

    data.keyboards.sort((a, b) => a.name.localeCompare(b.name));
    data.boards.sort((a, b) => a.name.localeCompare(b.name));

    for (let script_ext of ["sh", "ps1"]) {
      const templateBuffer = fs.readFileSync(
        `src/templates/setup.${script_ext}.mustache`,
        "utf8"
      );
      const script = Mustache.render(templateBuffer, data);
      fs.writeFileSync(`static/setup.${script_ext}`, script);
    }
  });
}

module.exports = function () {
  return {
    name: "setup-script-generation-plugin",
    configureWebpack() {
      return {
        plugins: [
          new PrebuildPlugin({
            build: generateSetupScripts,
          }),
        ],
      };
    },
  };
};