UNPKG

@intlayer/config

Version:

Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.

93 lines (91 loc) 3.95 kB
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const require_runtime = require('../_virtual/_rolldown/runtime.cjs'); const require_logger = require('../logger.cjs'); const require_loadExternalFile_parseFileContent = require('./parseFileContent.cjs'); const require_loadExternalFile_transpileTSToCJS = require('./transpileTSToCJS.cjs'); let node_fs = require("node:fs"); let node_path = require("node:path"); let node_fs_promises = require("node:fs/promises"); let json5 = require("json5"); json5 = require_runtime.__toESM(json5); //#region src/loadExternalFile/loadExternalFile.ts const parseJSON5 = json5.default.parse || json5.default.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 = (0, node_path.extname)(filePath) || ".json"; try { if (fileExtension === ".json" || fileExtension === ".json5" || fileExtension === ".jsonc") return parseJSON5((0, node_fs.readFileSync)(filePath, "utf-8")); const moduleResultString = require_loadExternalFile_transpileTSToCJS.transpileTSToCJSSync((0, node_fs.readFileSync)(filePath, "utf-8"), filePath, options?.buildOptions); if (!moduleResultString) { require_logger.logger("File could not be loaded.", { level: "error" }); return; } const fileContent = require_loadExternalFile_parseFileContent.parseFileContent(moduleResultString, { projectRequire: options?.projectRequire, envVarOptions: options?.envVarOptions, additionalEnvVars: options?.additionalEnvVars, mocks: options?.mocks, aliases: options?.aliases }); if (typeof fileContent === "undefined") { require_logger.logger(`File could not be loaded. Path : ${filePath}`); return; } return fileContent; } catch (error) { require_logger.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 = (0, node_path.extname)(filePath); try { if (fileExtension === ".json" || fileExtension === ".json5" || fileExtension === ".jsonc") return parseJSON5(await (0, node_fs_promises.readFile)(filePath, "utf-8")); const moduleResultString = await require_loadExternalFile_transpileTSToCJS.transpileTSToCJS(await (0, node_fs_promises.readFile)(filePath, "utf-8"), filePath, options?.buildOptions); if (!moduleResultString) { require_logger.logger("File could not be loaded.", { level: "error" }); return; } const fileContent = withPreloadedGlobals(options?.preloadGlobals, () => require_loadExternalFile_parseFileContent.parseFileContent(moduleResultString, { projectRequire: options?.projectRequire, envVarOptions: options?.envVarOptions, additionalEnvVars: options?.additionalEnvVars, mocks: options?.mocks, aliases: options?.aliases })); if (typeof fileContent === "undefined") { require_logger.logger(`File could not be loaded. Path : ${require_logger.colorizePath(filePath)}`); return; } return fileContent; } catch (error) { if (options?.logError ?? true) require_logger.logger([`Error: ${error.message} - `, JSON.stringify(error.stack, null, 2)], { level: "error" }); } }; //#endregion exports.loadExternalFile = loadExternalFile; exports.loadExternalFileSync = loadExternalFileSync; //# sourceMappingURL=loadExternalFile.cjs.map