UNPKG

@labnex/cli

Version:

CLI for Labnex, an AI-Powered Testing Automation Platform

78 lines 2.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initConfig = initConfig; exports.loadConfig = loadConfig; exports.saveConfig = saveConfig; exports.updateConfig = updateConfig; exports.clearConfig = clearConfig; exports.getConfigPath = getConfigPath; const fs_1 = require("fs"); const path_1 = require("path"); const os_1 = require("os"); const chalk_1 = __importDefault(require("chalk")); const CONFIG_DIR = (0, path_1.join)((0, os_1.homedir)(), '.labnex'); const CONFIG_FILE = (0, path_1.join)(CONFIG_DIR, 'config.json'); const DEFAULT_API_URL = 'https://labnex-backend.onrender.com/api'; // Allow override via env if power-user sets LABNEX_API_URL const DEFAULT_CONFIG = { apiUrl: process.env.LABNEX_API_URL || DEFAULT_API_URL, verbose: false, }; async function initConfig() { try { // Ensure config directory exists if (!(0, fs_1.existsSync)(CONFIG_DIR)) { (0, fs_1.mkdirSync)(CONFIG_DIR, { recursive: true }); } // Create default config if it doesn't exist if (!(0, fs_1.existsSync)(CONFIG_FILE)) { await saveConfig(DEFAULT_CONFIG); console.log(chalk_1.default.gray(`Initialized config at ${CONFIG_FILE}`)); } } catch (error) { console.error(chalk_1.default.red('Failed to initialize config:'), error); } } async function loadConfig() { try { if (!(0, fs_1.existsSync)(CONFIG_FILE)) { return DEFAULT_CONFIG; } const configData = (0, fs_1.readFileSync)(CONFIG_FILE, 'utf8'); const config = JSON.parse(configData); // Merge with defaults to ensure all properties exist return { ...DEFAULT_CONFIG, ...config }; } catch (error) { console.error(chalk_1.default.yellow('Warning: Failed to load config, using defaults')); return DEFAULT_CONFIG; } } async function saveConfig(config) { try { const configJson = JSON.stringify(config, null, 2); (0, fs_1.writeFileSync)(CONFIG_FILE, configJson, 'utf8'); } catch (error) { console.error(chalk_1.default.red('Failed to save config:'), error); throw error; } } async function updateConfig(updates) { const currentConfig = await loadConfig(); const newConfig = { ...currentConfig, ...updates }; await saveConfig(newConfig); return newConfig; } async function clearConfig() { await saveConfig(DEFAULT_CONFIG); console.log(chalk_1.default.green('Configuration cleared')); } function getConfigPath() { return CONFIG_FILE; } //# sourceMappingURL=config.js.map