summaryrefslogtreecommitdiff
path: root/docs/src/setup-script-generation-plugin/index.js
diff options
context:
space:
mode:
authorPeter Johanson <peter@peterjohanson.com>2021-07-26 00:25:34 -0400
committerPete Johanson <peter@peterjohanson.com>2021-09-11 00:50:36 -0400
commitb82bbb5ba22ef5c4346e82ccd41d3f794a4bf376 (patch)
treeda505a57c9071e200fbc4a1749ba943371d8241d /docs/src/setup-script-generation-plugin/index.js
parent683991aa9346a29c265299020a59c0b4c1805926 (diff)
feat: Generate setup scripts from metadata.
Diffstat (limited to 'docs/src/setup-script-generation-plugin/index.js')
-rw-r--r--docs/src/setup-script-generation-plugin/index.js62
1 files changed, 62 insertions, 0 deletions
diff --git a/docs/src/setup-script-generation-plugin/index.js b/docs/src/setup-script-generation-plugin/index.js
new file mode 100644
index 0000000..e97a4d0
--- /dev/null
+++ b/docs/src/setup-script-generation-plugin/index.js
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2021 The ZMK Contributors
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+var PrebuildPlugin = require("prebuild-webpack-plugin");
+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.flatMap((f) =>
+ yaml.safeLoadAll(fs.readFileSync(f, "utf8"))
+ );
+
+ 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.boards.push(item);
+ }
+ break;
+ }
+ return agg;
+ },
+ { keyboards: [], boards: [] }
+ );
+
+ 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,
+ }),
+ ],
+ };
+ },
+ };
+};