@tsed/config
Version:
Configuration management for Ts.ED
23 lines (22 loc) • 677 B
JavaScript
import { existsSync, readFileSync, watch } from "node:fs";
import { logger } from "@tsed/di";
export class JsonConfigSource {
getAll() {
const { path, encoding = "utf8" } = this.options;
// Check if the file exists
if (!existsSync(path)) {
logger().warn(`Configuration file not found: ${path}`);
return {};
}
// Read the file
const fileContent = readFileSync(path, encoding);
return JSON.parse(fileContent);
}
watch(onChange) {
const { path } = this.options;
const watcher = watch(path, onChange);
return () => {
watcher.close();
};
}
}