From fe60206904768a252c23ca0d25648e5df6027d5b Mon Sep 17 00:00:00 2001 From: Lukas Mertens Date: Thu, 11 Apr 2024 10:24:16 +0200 Subject: [PATCH] build(fetch-schema-plugin): added vite plugin to fetch schemas from everest framework Closes #131 Signed-off-by: Lukas Mertens commit-id:bb068b2d --- build-tools/fetch-schemas-plugin.ts | 37 +++++++++++++++++++++++++++++ public/schemas/.gitignore | 1 + schemas.config.ts | 23 ++++++++++++++++++ tsconfig.node.json | 4 +++- vite.config.ts | 6 +++-- 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 build-tools/fetch-schemas-plugin.ts create mode 100644 public/schemas/.gitignore create mode 100644 schemas.config.ts diff --git a/build-tools/fetch-schemas-plugin.ts b/build-tools/fetch-schemas-plugin.ts new file mode 100644 index 00000000..aed1d53b --- /dev/null +++ b/build-tools/fetch-schemas-plugin.ts @@ -0,0 +1,37 @@ +import {remoteSchemas} from "../schemas.config"; +import yaml from 'js-yaml'; +import crypto from 'crypto'; +import fs from "fs"; + + +export function vitePluginFetchSchemas() { + return { + name: 'vite-plugin-fetch-schema', + buildStart() { + fetchRemoteSchemas(); + } + }; +} + +function sha256(data: string): string { + return crypto.createHash('sha256').update(data, 'utf8').digest('hex'); +} + +export function fetchRemoteSchemas() { + remoteSchemas.forEach(async (schema) => { + if (!fs.existsSync(`./public/schemas/${schema.name}.json`)) { + const fetchedSchemaYAML = await fetch(schema.url).then(res => res.text()); + const fetchedSchemaHash = sha256(fetchedSchemaYAML); + if (fetchedSchemaHash !== schema.hash) { + throw new Error(`Schema hash mismatch for ${schema.url}. Expected: ${schema.hash}, got: ${fetchedSchemaHash}`); + } + const schemaJSON = yaml.load(fetchedSchemaYAML); + + schema.hash = fetchedSchemaHash; + fs.mkdirSync('./public/schemas', {recursive: true}); + fs.writeFileSync(`./public/schemas/${schema.name}.json`, JSON.stringify(schemaJSON)); + + console.log(`${schema.url} fetched and cached successfully.`); + } + }); +} diff --git a/public/schemas/.gitignore b/public/schemas/.gitignore new file mode 100644 index 00000000..a6c57f5f --- /dev/null +++ b/public/schemas/.gitignore @@ -0,0 +1 @@ +*.json diff --git a/schemas.config.ts b/schemas.config.ts new file mode 100644 index 00000000..9939b46f --- /dev/null +++ b/schemas.config.ts @@ -0,0 +1,23 @@ +// configure the release branch here +const releaseBranch = 'release/2024.3.0-rc1'; + +export type RemoteSchema = { + url: string; + name: string; + hash: string; +} + +// TODO : Currently we reference the schemas from a branch of the git repo. +// TODO : In the future we might want to release the schemas separately with a version number. +export const remoteSchemas: RemoteSchema[] = [ + { + url: `https://raw.githubusercontent.com/EVerest/everest-framework/${releaseBranch}/schemas/config.yaml`, + name: 'config', + hash: 'cd2296cdf0bd29bfffa58e920e5052d1bd5d2f1d2fd63d92fd5ac7e8af64d80b' + }, + { + url: `https://raw.githubusercontent.com/EVerest/everest-framework/${releaseBranch}/schemas/interface.yaml`, + name: 'interface', + hash: '9b1059249c4073737bec94753a6735f664d77b97321d4379b9de3179ea0a9cd5' + } +]; diff --git a/tsconfig.node.json b/tsconfig.node.json index b8afcc8f..0a4c7ff1 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -6,6 +6,8 @@ "allowSyntheticDefaultImports": true }, "include": [ - "vite.config.ts" + "vite.config.ts", + "build-tools/fetch-schemas-plugin.ts", + "schemas.config.ts" ] } diff --git a/vite.config.ts b/vite.config.ts index 9012f654..eb73cb8d 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -7,7 +7,8 @@ import {defineConfig, loadEnv} from "vite"; import {commonjsDeps} from '@koumoul/vjsf/utils/build'; import {fileURLToPath} from "node:url"; import commonjs from "@rollup/plugin-commonjs"; -import { VitePWA } from 'vite-plugin-pwa'; +import {VitePWA} from 'vite-plugin-pwa'; +import {vitePluginFetchSchemas} from "./build-tools/fetch-schemas-plugin"; export default defineConfig(({ mode}) => { loadEnv(mode, process.cwd(), ''); @@ -33,7 +34,8 @@ export default defineConfig(({ mode}) => { Vuetify({ autoImport: true, }), - VitePWA({ registerType: 'autoUpdate' }) + VitePWA({registerType: 'autoUpdate'}), + vitePluginFetchSchemas(), ], resolve: { alias: {