UNPKG

@jin7942/ray

Version:

Lightweight CI/CD deployment tool powered by Docker and Git

67 lines (66 loc) 3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadProjectConfig = loadProjectConfig; exports.loadAllProjects = loadAllProjects; const promises_1 = __importDefault(require("fs/promises")); const path_1 = __importDefault(require("path")); const logger_1 = require("../utils/logger"); const global_1 = require("./global"); /** * Loads the full config file and returns the project matching the given name. * * @param projectName - Name of the project to find. * @param configPath - Path to the JSON config file. Default: './ray.config.json'. * @returns A Promise resolving to the matching project's Config object. * @throws If the file is missing, invalid, or the project is not found. */ async function loadProjectConfig(projectName, configPath = './ray.config.json') { const absolutePath = path_1.default.resolve(configPath); try { const raw = await promises_1.default.readFile(absolutePath, 'utf-8'); const configFile = JSON.parse(raw); const project = configFile.projects.find((p) => p.name.trim() === projectName.trim()); if (!project) { logger_1.logger.error(`Project "${projectName}" not found in config file.`); throw new Error(`Project "${projectName}" does not exist in ${absolutePath}`); } (0, global_1.setGlobalConfig)(project); logger_1.logger.info(`Loaded config for project: ${projectName}`); return project; } catch (err) { if (err.code === 'ENOENT') { logger_1.logger.error(`Config file not found at: ${absolutePath}`); throw new Error(`Config file not found at ${absolutePath}`); } logger_1.logger.error(`Failed to load config: ${err.message}`); throw new Error(`Invalid config file: ${err.message}`); } } /** * Loads all projects from the config file. * * @param configPath - Path to the JSON config file. Default: './ray.config.json'. * @returns A Promise resolving to an array of all project configurations. * @throws If the file is missing or contains invalid JSON. */ async function loadAllProjects(configPath = './ray.config.json') { const absolutePath = path_1.default.resolve(configPath); try { const raw = await promises_1.default.readFile(absolutePath, 'utf-8'); const configFile = JSON.parse(raw); logger_1.logger.info(`Loaded all project configs (${configFile.projects.length})`); return configFile.projects; } catch (err) { if (err.code === 'ENOENT') { logger_1.logger.error(`Config file not found at: ${absolutePath}`); throw new Error(`Config file not found at ${absolutePath}`); } logger_1.logger.error(`Failed to load config file: ${err.message}`); throw new Error(`Invalid config file: ${err.message}`); } }