@intlayer/config
Version:
Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.
89 lines (87 loc) • 3.3 kB
JavaScript
import { colorizePath, logger } from "../logger.mjs";
import { parseFileContent } from "./parseFileContent.mjs";
import { transpileTSToCJS, transpileTSToCJSSync } from "./transpileTSToCJS.mjs";
import { readFileSync } from "node:fs";
import { extname } from "node:path";
import { readFile } from "node:fs/promises";
import JSON5 from "json5";
//#region src/loadExternalFile/loadExternalFile.ts
const parseJSON5 = JSON5.parse || JSON5.default?.parse;
/**
* Load the content declaration from the given path
*
* Accepts JSON, JS, MJS and TS files as configuration
*/
const loadExternalFileSync = (filePath, options) => {
const fileExtension = extname(filePath) || ".json";
try {
if (fileExtension === ".json" || fileExtension === ".json5" || fileExtension === ".jsonc") return parseJSON5(readFileSync(filePath, "utf-8"));
const moduleResultString = transpileTSToCJSSync(readFileSync(filePath, "utf-8"), filePath, options?.buildOptions);
if (!moduleResultString) {
logger("File could not be loaded.", { level: "error" });
return;
}
const fileContent = parseFileContent(moduleResultString, {
projectRequire: options?.projectRequire,
envVarOptions: options?.envVarOptions,
additionalEnvVars: options?.additionalEnvVars,
mocks: options?.mocks,
aliases: options?.aliases
});
if (typeof fileContent === "undefined") {
logger(`File could not be loaded. Path : ${filePath}`);
return;
}
return fileContent;
} catch (error) {
logger([`Error: ${error.message} - `, JSON.stringify(error.stack, null, 2)], { level: "error" });
}
};
const withPreloadedGlobals = (globals, fn) => {
if (!globals) return fn();
const globalVars = globalThis;
const prev = {};
for (const [key, value] of Object.entries(globals)) {
prev[key] = globalVars[key];
globalVars[key] = value;
}
try {
return fn();
} finally {
for (const key of Object.keys(globals)) if (prev[key] !== void 0) globalVars[key] = prev[key];
else delete globalVars[key];
}
};
/**
* Load the content declaration from the given path
*
* Accepts JSON, JS, MJS and TS files as configuration
*/
const loadExternalFile = async (filePath, options) => {
const fileExtension = extname(filePath);
try {
if (fileExtension === ".json" || fileExtension === ".json5" || fileExtension === ".jsonc") return parseJSON5(await readFile(filePath, "utf-8"));
const moduleResultString = await transpileTSToCJS(await readFile(filePath, "utf-8"), filePath, options?.buildOptions);
if (!moduleResultString) {
logger("File could not be loaded.", { level: "error" });
return;
}
const fileContent = withPreloadedGlobals(options?.preloadGlobals, () => parseFileContent(moduleResultString, {
projectRequire: options?.projectRequire,
envVarOptions: options?.envVarOptions,
additionalEnvVars: options?.additionalEnvVars,
mocks: options?.mocks,
aliases: options?.aliases
}));
if (typeof fileContent === "undefined") {
logger(`File could not be loaded. Path : ${colorizePath(filePath)}`);
return;
}
return fileContent;
} catch (error) {
if (options?.logError ?? true) logger([`Error: ${error.message} - `, JSON.stringify(error.stack, null, 2)], { level: "error" });
}
};
//#endregion
export { loadExternalFile, loadExternalFileSync };
//# sourceMappingURL=loadExternalFile.mjs.map