generated from EVerest/everest-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(fetch-schema-plugin): added vite plugin to fetch schemas from e…
…verest framework Closes #131 Signed-off-by: Lukas Mertens <[email protected]> commit-id:bb068b2d
- Loading branch information
1 parent
c7fdebf
commit c441b14
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters