Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(fetch-schema-plugin): added vite plugin to fetch schemas from everest framework #132

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions build-tools/fetch-schemas-plugin.ts
Original file line number Diff line number Diff line change
@@ -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.`);
}
});
}
1 change: 1 addition & 0 deletions public/schemas/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.json
23 changes: 23 additions & 0 deletions schemas.config.ts
Original file line number Diff line number Diff line change
@@ -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'
}
];
4 changes: 3 additions & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"allowSyntheticDefaultImports": true
},
"include": [
"vite.config.ts"
"vite.config.ts",
"build-tools/fetch-schemas-plugin.ts",
"schemas.config.ts"
]
}
6 changes: 4 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(), '');
Expand All @@ -33,7 +34,8 @@ export default defineConfig(({ mode}) => {
Vuetify({
autoImport: true,
}),
VitePWA({ registerType: 'autoUpdate' })
VitePWA({registerType: 'autoUpdate'}),
vitePluginFetchSchemas(),
],
resolve: {
alias: {
Expand Down
Loading