@layerfig/config
Version:
Layer and runtime-validate type-safe configs for JavaScript apps.
37 lines (35 loc) • 1.41 kB
JavaScript
import { Source } from "../../source-BvmiqUNz.js";
import "../../set-LPILcL1N.js";
import { readIfExist } from "../../utils-B21a12rH.js";
import path from "node:path";
//#region src/sources/file.ts
const APP_ROOT_PATH = process.cwd();
var FileSource = class extends Source {
#fileName;
constructor(fileName) {
super();
this.#fileName = fileName;
}
loadSource({ relativeConfigFolderPath, parser, slotPrefix, runtimeEnv }) {
const absoluteConfigFolderPath = path.join(APP_ROOT_PATH, relativeConfigFolderPath);
const absoluteFilePath = path.resolve(absoluteConfigFolderPath, this.#fileName);
const fileExtension = this.#getFileExtension(absoluteFilePath);
if (parser.acceptsExtension(fileExtension) === false) throw new Error(`".${fileExtension}" file is not supported by this parser. Accepted files are: "${parser.acceptedFileExtensions.join(", ")}"`);
const fileContentResult = readIfExist(absoluteFilePath);
if (fileContentResult.ok === false) throw new Error(fileContentResult.error);
const finalContent = this.maybeReplaceSlotFromValue({
value: fileContentResult.data,
slotPrefix,
runtimeEnv
});
const parserResult = parser.load(finalContent);
if (!parserResult.ok) throw parserResult.error;
return parserResult.data;
}
#getFileExtension(filePath) {
return path.extname(filePath).slice(1);
}
};
//#endregion
export { FileSource };
//# sourceMappingURL=index.js.map