Files
bpsets/src/BPManager.ts

68 lines
1.7 KiB
TypeScript
Raw Normal View History

import { BPSet } from "./types";
import { readdir } from 'node:fs/promises'
2024-12-24 15:25:16 +09:00
import path from 'node:path'
export class BPManager {
private static _instance = new BPManager()
public static getInstance = () =>
this._instance
// ---
private readonly bpSets:
2024-12-26 13:06:44 +09:00
Record<string, BPSet> = {}
2024-12-24 15:25:16 +09:00
private constructor() {
this.loadBPSets()
}
private async loadBPSets() {
2024-12-30 14:40:09 +09:00
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<string, new () => BPSet>
for (const bpSetClass of Object.keys(bpSetClasses)) {
this.bpSets[bpSetClass] = new bpSetClasses[bpSetClass]()
console.log('BPSet implement,', bpSetClass, 'loaded')
}
}
2024-12-24 15:25:16 +09:00
}
}
2024-12-30 11:19:53 +09:00
public runCheckOnce(name: string) {
return this.bpSets[name].check()
2024-12-30 11:19:53 +09:00
}
public runCheckAll(finished = (name: string) => {}) {
const checkJobs: Promise<void>[] = []
for (const bpset of Object.values(this.bpSets))
checkJobs.push(
bpset
.check()
.then(() =>
finished(bpset.getMetadata().name))
)
2024-12-30 11:19:53 +09:00
return Promise.all(checkJobs)
}
public runFix(name: string, requiredParametersForFix: { name: string, value: string }[]) {
return this
.bpSets[name]
.fix(
this.bpSets[name].getStats().nonCompliantResources,
2024-12-30 11:19:53 +09:00
requiredParametersForFix
)
2024-12-26 13:06:44 +09:00
}
2024-12-24 15:25:16 +09:00
public readonly getBPSets = () =>
Object.values(this.bpSets)
}