Skip to content

Commit

Permalink
feat: Generate setup scripts from metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Aug 3, 2021
1 parent 8895d5a commit da0d184
Show file tree
Hide file tree
Showing 8 changed files with 568 additions and 393 deletions.
4 changes: 3 additions & 1 deletion docs/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
build
.docusaurus
.docusaurus
*.mustache
hardware-metadata.json
1 change: 1 addition & 0 deletions docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
path.resolve(__dirname, "src/docusaurus-tree-sitter-plugin"),
path.resolve(__dirname, "src/hardware-metadata-collection-plugin"),
path.resolve(__dirname, "src/hardware-schema-typescript-plugin"),
path.resolve(__dirname, "src/setup-script-generation-plugin"),
],
themeConfig: {
colorMode: {
Expand Down
352 changes: 226 additions & 126 deletions docs/package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"react-copy-to-clipboard": "^5.0.3",
"react-dom": "^17.0.2",
"react-toastify": "^7.0.4",
"web-tree-sitter": "^0.19.4"
"web-tree-sitter": "^0.19.4",
"@mdx-js/react": "^1.6.22"
},
"browserslist": {
"production": [
Expand All @@ -46,12 +47,14 @@
"eslint-plugin-mdx": "^1.13.0",
"eslint-plugin-react": "^7.23.2",
"json-schema-to-typescript": "^10.1.3",
"mustache": "^4.2.0",
"null-loader": "^4.0.0",
"prebuild-webpack-plugin": "^1.1.1",
"prettier": "2.3.1",
"string-replace-loader": "^3.0.3",
"typescript": "^4.2.3",
"@docusaurus/module-type-aliases": "^2.0.0-alpha.72",
"webpack": "^5.46.0",
"@docusaurus/module-type-aliases": "^2.0.0-beta.3",
"@tsconfig/docusaurus": "^1.0.2",
"@types/react": "^17.0.3",
"@types/react-helmet": "^6.1.0",
Expand Down
62 changes: 62 additions & 0 deletions docs/src/setup-script-generation-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* TODO: COPYRIGHT AND LICENSE HEADER */

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: [] }
);

const templateBuffer = fs.readFileSync(
"src/templates/setup.sh.mustache",
"utf8"
);
const script = Mustache.render(templateBuffer, data);
fs.writeFileSync("static/setup.sh", script);
const powershellTemplateBuffer = fs.readFileSync(
"src/templates/setup.ps1.mustache",
"utf8"
);
const psScript = Mustache.render(powershellTemplateBuffer, data);
return fs.writeFileSync("static/setup.ps1", psScript);
});
}

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

0 comments on commit da0d184

Please sign in to comment.