UNPKG

alia

Version:
22 lines (21 loc) 806 B
import { inject } from '../utils/di.js'; import fs from 'fs/promises'; import path from 'node:path'; export class FlagLoaderService { #flagsDir = path.join(import.meta.dirname, '..', 'flags'); async loadFlags() { const flagPaths = await this.#getFlags(); return Promise.all(flagPaths.map((f) => this.#loadFlag(f))); } async #getFlags() { const flags = await fs.readdir(this.#flagsDir); return flags.filter((f) => /\.flag\.[jt]s/i.exec(f)); } async #loadFlag(flag) { /* 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 inject(flagClass); } }