import { BPSet, BPSetMetadata } from "./types"; import { readdir, readFile } from 'node:fs/promises' import path from 'node:path' export class BPManager { private static _instance = new BPManager() public static getInstance = () => this._instance // --- private readonly bpSets: Record = {} private readonly bpSetMetadatas: Record = {} private constructor() { this.loadBPSets() this.loadBPSetMetadatas() } private async loadBPSets() { const bpSetFolders = await readdir(path.join(__dirname, 'bpsets')) for (const bpSetFolder of bpSetFolders) { const bpSetFiles = await readdir(path.join(__dirname, 'bpsets', bpSetFolder)) for (const bpSetFile of bpSetFiles) { const bpSetPath = path.join(__dirname, 'bpsets', bpSetFolder, bpSetFile) const bpSetClasses = await import(bpSetPath) as Record BPSet> for (const bpSetClass of Object.keys(bpSetClasses)) { this.bpSets[bpSetClass] = new bpSetClasses[bpSetClass]() console.log('BPSet implement,', bpSetClass, 'loaded') } } } } private async loadBPSetMetadatas() { const bpSetMetadatasRaw = await readFile(path.join(__dirname, '../bpset_metadata.json')) const bpSetMetadatas = JSON.parse(bpSetMetadatasRaw.toString('utf-8')) as BPSetMetadata[] for (const [idx, bpSetMetadata] of bpSetMetadatas.entries()) { this.bpSetMetadatas[bpSetMetadata.name] = { ...bpSetMetadata, nonCompliantResources: [], compliantResources: [], status:'LOADED', errorMessage: [], idx } } } public runCheckOnce(name: string) { return this .bpSets[name].check() .catch((err) => { this.bpSetMetadatas[name].status = 'ERROR' this.bpSetMetadatas[name].errorMessage.push({ date: new Date(), message: err }) return undefined }) .then((result) => { if (result === undefined) return this.bpSetMetadatas[name].compliantResources = result.compliantResources this.bpSetMetadatas[name].nonCompliantResources = result.nonCompliantResources this.bpSetMetadatas[name].status = 'FINISHED' }) } public runCheckAll(finished = (name: string) => {}) { const checkJobs = Object .values(this.bpSetMetadatas) .map(({ name }) => { this.bpSetMetadatas[name].status = 'CHECKING' return this .bpSets[name].check() .catch((err) => { this.bpSetMetadatas[name].status = 'ERROR' this.bpSetMetadatas[name].errorMessage.push({ date: new Date(), message: err }) return undefined }) .then((result) => { if (result === undefined) return this.bpSetMetadatas[name].compliantResources = result.compliantResources this.bpSetMetadatas[name].nonCompliantResources = result.nonCompliantResources this.bpSetMetadatas[name].status = 'FINISHED' finished(name) }) }) return Promise.all(checkJobs) } public runFix(name: string, requiredParametersForFix: { name: string, value: string }[]) { return this .bpSets[name] .fix( this.bpSetMetadatas[name].nonCompliantResources, requiredParametersForFix ) } public readonly getBPSet = (name: string) => this.bpSets[name] public readonly getBPSetMetadata = (name: string) => this.bpSetMetadatas[name] public readonly getBPSets = () => Object.values(this.bpSets) public readonly getBPSetMetadatas = () => Object.values(this.bpSetMetadatas) }