UNPKG

niledatabase

Version:

Command line interface for Nile databases

285 lines (284 loc) 9.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const fs = __importStar(require("fs")); const path_1 = __importDefault(require("path")); const os_1 = __importDefault(require("os")); const colors_1 = require("./colors"); class ConfigManager { constructor(globalOptions) { this.config = {}; this.credentials = {}; this.globalOptions = globalOptions; const configDir = path_1.default.join(os_1.default.homedir(), '.nile'); this.configPath = path_1.default.join(configDir, 'config.json'); this.credentialsPath = path_1.default.join(configDir, 'credentials.json'); this.loadConfig(); if (globalOptions.authUrl) { this.config.authUrl = globalOptions.authUrl; } if (globalOptions.debug !== undefined) { this.config.debug = globalOptions.debug; } } loadConfig() { try { // Ensure .nile directory exists const configDir = path_1.default.dirname(this.configPath); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } // Load config if it exists if (fs.existsSync(this.configPath)) { const configContent = fs.readFileSync(this.configPath, 'utf-8'); this.config = JSON.parse(configContent); } // Load credentials this.loadCredentials(); } catch (error) { console.error('Error loading config:', error); } } saveConfig() { try { fs.writeFileSync(this.configPath, JSON.stringify(this.config, null, 2)); } catch (error) { console.error('Error saving config:', error); throw new Error('Failed to save config'); } } saveCredentials() { try { const configDir = path_1.default.dirname(this.credentialsPath); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } if (Object.keys(this.credentials).length > 0) { fs.writeFileSync(this.credentialsPath, JSON.stringify(this.credentials, null, 2)); // Reload credentials after saving this.loadCredentials(); } else { // If no credentials, remove the credentials file if (fs.existsSync(this.credentialsPath)) { fs.unlinkSync(this.credentialsPath); } } } catch (error) { console.error('Error saving credentials:', error); throw new Error('Failed to save credentials'); } } loadCredentials() { try { if (fs.existsSync(this.credentialsPath)) { const credentialsContent = fs.readFileSync(this.credentialsPath, 'utf-8'); this.credentials = JSON.parse(credentialsContent); } } catch (error) { console.error('Error loading credentials:', error); } } resetConfig() { this.config = {}; this.credentials = {}; this.saveConfig(); this.saveCredentials(); } // Token management methods setToken(token) { if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Setting token in credentials...')); } this.credentials.token = token; this.saveCredentials(); // Ensure credentials are reloaded this.loadCredentials(); if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Token set and credentials reloaded')); } } getToken() { // First check for API key (highest priority) const apiKey = this.getApiKey(); if (apiKey) { if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Using API key for authentication')); } return apiKey; } // If no API key, check credentials file const token = this.credentials.token; if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Using stored token for authentication')); } return token; } removeToken() { if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Removing token from credentials...')); } delete this.credentials.token; this.saveCredentials(); // Ensure credentials are reloaded this.loadCredentials(); if (this.globalOptions.debug) { console.log(colors_1.theme.dim('Token removed and credentials reloaded')); } } setApiKey(apiKey) { this.config.apiKey = apiKey; this.saveConfig(); return this.config; } getApiKey() { // 1. Command line argument has highest priority if (this.globalOptions.apiKey) { return this.globalOptions.apiKey; } // 2. Config file const configApiKey = this.config?.apiKey; if (configApiKey) { return configApiKey; } // 3. Environment variable return process.env.NILE_API_KEY; } setWorkspace(workspace) { this.config.workspace = workspace; this.saveConfig(); return this.config; } getWorkspace() { // 1. Command line argument has highest priority if (this.globalOptions.workspace) { return this.globalOptions.workspace; } // 2. Config file const configWorkspace = this.config?.workspace; if (configWorkspace) { return configWorkspace; } // 3. Environment variable return process.env.NILE_WORKSPACE; } setDbHost(dbHost) { this.config.dbHost = dbHost; this.saveConfig(); return this.config; } getDbHost() { // 1. Command line argument has highest priority if (this.globalOptions.dbHost) { return this.globalOptions.dbHost; } // 2. Config file const configDbHost = this.config?.dbHost; if (configDbHost) { return configDbHost; } // 3. Environment variable return process.env.NILE_DB_HOST || 'db.thenile.dev'; } setGlobalHost(globalHost) { this.config.globalHost = globalHost; this.saveConfig(); return this.config; } getGlobalHost() { // 1. Command line argument has highest priority if (this.globalOptions.globalHost) { return this.globalOptions.globalHost; } // 2. Config file const configGlobalHost = this.config?.globalHost; if (configGlobalHost) { return configGlobalHost; } // 3. Environment variable return process.env.NILE_GLOBAL_HOST || 'global.thenile.dev'; } setDatabase(database) { this.config.database = database; this.saveConfig(); return this.config; } getDatabase() { // 1. Command line argument has highest priority if (this.globalOptions.db) { return this.globalOptions.db; } // 2. Config file const configDb = this.config?.database; if (configDb) { return configDb; } // 3. Environment variable return process.env.NILE_DB; } setAuthUrl(authUrl) { this.config.authUrl = authUrl; this.saveConfig(); return this.config; } getAuthUrl() { // 1. Command line argument has highest priority if (this.globalOptions.authUrl) { return this.globalOptions.authUrl; } // 2. Config file const configAuthUrl = this.config?.authUrl; if (configAuthUrl) { return configAuthUrl; } // 3. Environment variable return process.env.NILE_AUTH_URL || 'console.thenile.dev'; } getAllConfig() { return { ...this.config }; } getDebug() { return this.config.debug || false; } } exports.ConfigManager = ConfigManager;