UNPKG

brek

Version:
113 lines 5.74 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadConfig = exports.getConfig = exports.BREK_CONFIG_JSON_PATH = exports.BREK_LOADERS_FILE_PATH = exports.BREK_WRITE_DIR = exports.BREK_CONFIG_DIR = void 0; const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const writeConfJson_1 = require("./writeConfJson"); const toResult_1 = require("./lib/toResult"); const getEnvArguments_1 = require("./getEnvArguments"); const loadConfFromFiles_1 = require("./loadConfFromFiles"); const mergeConfs_1 = require("./mergeConfs"); const resolveConf_1 = require("./resolveConf"); const debug_1 = require("./debug"); exports.BREK_CONFIG_DIR = (_a = process.env.BREK_CONFIG_DIR) !== null && _a !== void 0 ? _a : (0, node_path_1.resolve)('config'); // The directory to write the resolved configuration to. On read-only file // systems such as AWS Lambda, this should be set to a writable directory, ie // /tmp. exports.BREK_WRITE_DIR = process.env.BREK_WRITE_DIR ? (0, node_path_1.resolve)(process.env.BREK_WRITE_DIR) : exports.BREK_CONFIG_DIR; exports.BREK_LOADERS_FILE_PATH = process.env.BREK_LOADERS_FILE_PATH ? (0, node_path_1.resolve)(process.env.BREK_LOADERS_FILE_PATH) : (0, node_path_1.resolve)('brek.loaders.js'); exports.BREK_CONFIG_JSON_PATH = (0, node_path_1.resolve)(exports.BREK_WRITE_DIR, 'config.json'); let resolvedConf = null; let attempts = 0; function getConfig() { // 1) Load from memory cache if available if (resolvedConf) return resolvedConf; // 2) Load from config.json const [, confRaw] = (0, toResult_1.toResult)(() => (0, node_fs_1.readFileSync)(exports.BREK_CONFIG_JSON_PATH, 'utf8')); if (confRaw) { const [jsonParseErr, conf] = (0, toResult_1.toResult)(() => JSON.parse(confRaw)); if (jsonParseErr) throw new Error(`Error parsing config.json: ${jsonParseErr}`); if (!conf) throw new Error('Error parsing config.json: empty object'); resolvedConf = conf; return resolvedConf; } // 3) Load from project's config files and environment variables synchronously // and write the resolved configuration to config.json. Since loaders could be // async, we need to run this in a child process. const cliPath = (0, node_path_1.resolve)(__dirname, '../bin/cli.js'); const cmd = `node ${cliPath} load-config`; (0, debug_1.debug)(`No config.json found. Running ${cmd} to generate one...`); try { // Set encoding to 'utf8' so execSync returns a string instead of a Buffer. const output = (0, node_child_process_1.execSync)(cmd, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }); console.log(output); } catch (error) { console.log(error.stdout); console.error(error.stderr); throw error; } if (attempts > 2) { throw new Error('Failed to load configuration after multiple attempts.'); } attempts++; return getConfig(); } exports.getConfig = getConfig; /** * Load the configuration from the project's config files and environment variables, * and write the resolved configuration to config.json. */ function loadConfig() { return __awaiter(this, void 0, void 0, function* () { const env = (0, getEnvArguments_1.getEnvArguments)(); const confFromFiles = (0, loadConfFromFiles_1.loadConfFromFiles)(env); const mergedConf = (0, mergeConfs_1.mergeConfs)(Object.assign(Object.assign({}, confFromFiles), { overrides: env.overrides })); let loaders = {}; try { // Load the loaders from the provided file path. // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires const mod = require(exports.BREK_LOADERS_FILE_PATH); loaders = mod.default || mod; console.log(`Loaded ${Object.keys(loaders).length} loaders from ${exports.BREK_LOADERS_FILE_PATH}.`); } catch (e) { if (e.code === 'MODULE_NOT_FOUND') { console.log(`No loaders found at ${exports.BREK_LOADERS_FILE_PATH}.`); } else { throw e; } } (0, debug_1.debug)('env:', env); (0, debug_1.debug)('confFromFiles:', confFromFiles); (0, debug_1.debug)('merged:', mergedConf); (0, debug_1.debug)('loaders:', loaders ? Object.keys(loaders) : 'none'); if (Object.keys(loaders)) { // Resolve the configuration with the provided loaders. const conf = yield (0, resolveConf_1.resolveConf)(mergedConf, loaders); // Persist the resolved configuration to config.json (0, writeConfJson_1.writeConfJson)(conf); } else { // I guess there are no loaders to resolve (0, writeConfJson_1.writeConfJson)(mergedConf); } }); } exports.loadConfig = loadConfig; //# sourceMappingURL=index.js.map