UNPKG

alia

Version:
21 lines (20 loc) 857 B
import fs from 'fs/promises'; import path from 'node:path'; export class FlagLoaderService { flagsDir = path.join(import.meta.dirname, '..', 'flags'); async loadFlags(confService, gistService) { const flagPaths = await this.getFlags(); return Promise.all(flagPaths.map((f) => this.loadFlag(f, confService, gistService))); } async getFlags() { const flags = await fs.readdir(this.flagsDir); return flags.filter((f) => /\.flag\.[jt]s/i.exec(f)); } async loadFlag(flag, confService, gistService) { /* c8 ignore next */ // Coverage picks up "as FlagModule" as uncovered branch const module = (await import(`../flags/${flag}`)); const defaultKey = Object.keys(module)[0]; const flagClass = module[defaultKey]; return new flagClass(confService, gistService); } }