Files
bpsets/src/BPManager.ts

55 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-01-16 15:48:55 +09:00
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()
2025-01-16 15:48:55 +09:00
public static getInstance = () => this._instance
2024-12-24 15:25:16 +09:00
// ---
2025-01-16 15:48:55 +09:00
private readonly bpSets: 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'))
2025-01-16 15:48:55 +09:00
2024-12-30 14:40:09 +09:00
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)
2025-01-16 15:48:55 +09:00
const bpSetClasses = (await import(bpSetPath)) as Record<string, new () => BPSet>
2024-12-30 14:40:09 +09:00
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
}
2025-01-16 15:48:55 +09:00
public runCheckAll(finished = (_: string) => {}) {
const checkJobs: Promise<void>[] = []
for (const bpset of Object.values(this.bpSets))
2025-01-16 15:48:55 +09:00
checkJobs.push(bpset.check().then(() => finished(bpset.getMetadata().name)))
2024-12-30 11:19:53 +09:00
return Promise.all(checkJobs)
}
2025-01-16 15:48:55 +09:00
public runFix(name: string, requiredParametersForFix: { name: string; value: string }[]) {
return this.bpSets[name].fix(this.bpSets[name].getStats().nonCompliantResources, requiredParametersForFix)
2024-12-26 13:06:44 +09:00
}
2025-01-16 15:48:55 +09:00
public readonly getBPSets = () => Object.values(this.bpSets)
2024-12-24 15:25:16 +09:00
}