From 5e6634d2e522289c5169b22a2a0d8b600a0e008d Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Mon, 29 Mar 2021 00:59:15 +0000 Subject: feat(docs): Add TS type gen, metadata JSON gen * Generate TS types from new hardware metadata schema. * Aggregate all hw metadata YAML into one aggregate JSON file for consumption by others. --- docs/src/.gitignore | 1 + docs/src/data/.gitignore | 1 + docs/src/docusaurus-tree-sitter-plugin/index.js | 6 ++++ .../hardware-metadata-collection-plugin/index.js | 33 ++++++++++++++++++++++ .../src/hardware-schema-typescript-plugin/index.js | 29 +++++++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 docs/src/.gitignore create mode 100644 docs/src/data/.gitignore create mode 100644 docs/src/hardware-metadata-collection-plugin/index.js create mode 100644 docs/src/hardware-schema-typescript-plugin/index.js (limited to 'docs/src') diff --git a/docs/src/.gitignore b/docs/src/.gitignore new file mode 100644 index 0000000..3a722c8 --- /dev/null +++ b/docs/src/.gitignore @@ -0,0 +1 @@ +hardware-metadata.d.ts diff --git a/docs/src/data/.gitignore b/docs/src/data/.gitignore new file mode 100644 index 0000000..201a309 --- /dev/null +++ b/docs/src/data/.gitignore @@ -0,0 +1 @@ +hardware-metadata.json diff --git a/docs/src/docusaurus-tree-sitter-plugin/index.js b/docs/src/docusaurus-tree-sitter-plugin/index.js index c8a9500..e782aea 100644 --- a/docs/src/docusaurus-tree-sitter-plugin/index.js +++ b/docs/src/docusaurus-tree-sitter-plugin/index.js @@ -1,3 +1,9 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + module.exports = function () { return { configureWebpack(config, isServer) { diff --git a/docs/src/hardware-metadata-collection-plugin/index.js b/docs/src/hardware-metadata-collection-plugin/index.js new file mode 100644 index 0000000..cbea805 --- /dev/null +++ b/docs/src/hardware-metadata-collection-plugin/index.js @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +var PrebuildPlugin = require("prebuild-webpack-plugin"); +const fs = require("fs"); +const yaml = require("js-yaml"); +const glob = require("glob"); +const { compile, compileFromFile } = require('json-schema-to-typescript'); + +function generateHardwareMetadataAggregate() { + glob("../app/boards/**/*.zmk.yml", (error, files) => { + const aggregated = files.flatMap(f => yaml.safeLoadAll(fs.readFileSync(f, "utf8"))); + fs.writeFileSync("src/data/hardware-metadata.json", JSON.stringify(aggregated)); + }); +} + +module.exports = function () { + return { + name: "hardware-metadata-collection-plugin", + configureWebpack() { + return { + plugins: [ + new PrebuildPlugin({ + build: generateHardwareMetadataAggregate, + }), + ], + }; + }, + }; +}; \ No newline at end of file diff --git a/docs/src/hardware-schema-typescript-plugin/index.js b/docs/src/hardware-schema-typescript-plugin/index.js new file mode 100644 index 0000000..2ccbf19 --- /dev/null +++ b/docs/src/hardware-schema-typescript-plugin/index.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2021 The ZMK Contributors + * + * SPDX-License-Identifier: MIT + */ + +var PrebuildPlugin = require("prebuild-webpack-plugin"); +const fs = require("fs"); +const { compileFromFile } = require('json-schema-to-typescript'); + +async function generateHardwareMetadataTypescript() { + const ts = await compileFromFile("../schema/hardware-metadata.schema.json"); + fs.writeFileSync("src/hardware-metadata.d.ts", ts); +} + +module.exports = function () { + return { + name: "hardware-metadata-typescript-plugin", + configureWebpack() { + return { + plugins: [ + new PrebuildPlugin({ + build: generateHardwareMetadataTypescript, + }), + ], + }; + }, + }; +}; \ No newline at end of file -- cgit v1.2.3