@enspirit/emb
Version:
A replacement for our Makefile-for-monorepos
54 lines (53 loc) • 1.73 kB
JavaScript
import { Ajv } from 'ajv';
import { readFile, stat } from 'node:fs/promises';
import yaml from 'yaml';
import configSchema from './schema.json' with { type: 'json' };
const ajv = new Ajv();
ajv.addSchema(configSchema);
export const validateUserConfig = async (pathOrObject) => {
let embConfig;
if (typeof pathOrObject === 'string') {
if (await stat(pathOrObject)) {
const cfgYaml = (await readFile(pathOrObject)).toString();
embConfig = yaml.parse(cfgYaml.toString());
}
else {
throw new Error(`Could not find file: ${pathOrObject}`);
}
}
else {
embConfig = pathOrObject;
}
if (!ajv.validate(configSchema, embConfig)) {
ajv.errors?.forEach((err) => console.error(err));
throw new Error(`Your .emb.yml is incorrect`);
}
return embConfig;
};
export const validateEmbfile = async (pathOrObject) => {
let component;
if (typeof pathOrObject === 'string') {
if (await stat(pathOrObject)) {
const cfgYaml = (await readFile(pathOrObject)).toString();
component = yaml.parse(cfgYaml.toString());
}
else {
throw new Error(`Could not find file: ${pathOrObject}`);
}
}
else {
component = pathOrObject;
}
const validate = ajv.getSchema('/schemas/config#/definitions/ComponentConfig');
if (!validate) {
throw new Error('Could not find the JSON schema validator for Embfile');
}
if (!component) {
return {};
}
if (!validate(component)) {
ajv.errors?.forEach((err) => console.error(err));
throw new Error(`Your .emb.yml is incorrect`);
}
return component;
};