UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

111 lines 5.69 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerLogoutCommand = registerLogoutCommand; const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const fs_1 = __importDefault(require("fs")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const auth_1 = require("../../utils/auth"); function registerLogoutCommand(program) { program .command('logout [nestbox-domain]') .description('Logout from Nestbox platform') .action((domain) => __awaiter(this, void 0, void 0, function* () { try { const authToken = (0, auth_1.getAuthToken)(domain); if (!authToken) { console.log(chalk_1.default.yellow('No authentication token found. Please log in first.')); return; } // Function to remove all credential files for a domain const removeCredentialFiles = (domain) => { try { const configDir = path_1.default.join(os_1.default.homedir(), '.config', '.nestbox'); if (!fs_1.default.existsSync(configDir)) { return false; } // Sanitize domain for file matching // Replace characters that are problematic in filenames const sanitizedDomain = domain.replace(/:/g, '_'); // Get all files in the directory const files = fs_1.default.readdirSync(configDir); // Find and remove all files that match the domain let removedCount = 0; for (const file of files) { // Check if the file matches any of the possible domain formats if (file.endsWith(`_${domain}.json`) || file.endsWith(`_${sanitizedDomain}.json`)) { fs_1.default.unlinkSync(path_1.default.join(configDir, file)); removedCount++; } } return removedCount > 0; } catch (error) { console.warn(chalk_1.default.yellow(`Warning: Could not remove credential files. ${error instanceof Error ? error.message : ''}`)); return false; } }; if (domain) { // Logout from specific domain // Remove credentials using utility function const removed = (0, auth_1.removeCredentials)(domain); // Also remove all credential files for this domain const filesRemoved = removeCredentialFiles(domain); if (removed || filesRemoved) { console.log(chalk_1.default.green(`Successfully logged out from ${domain}`)); } else { console.log(chalk_1.default.yellow(`No credentials found for ${domain}`)); } } else { // Ask which domain to logout from const credentials = (0, auth_1.listCredentials)(); if (credentials.length === 0) { console.log(chalk_1.default.yellow('No credentials found')); return; } // Group credentials by domain const domains = Array.from(new Set(credentials.map(cred => cred.domain))); const domainChoices = domains.map(domain => { const accounts = credentials.filter(cred => cred.domain === domain); return `${domain} (${accounts.length} account${accounts.length > 1 ? 's' : ''})`; }); const { selected } = yield inquirer_1.default.prompt([ { type: 'list', name: 'selected', message: 'Select domain to logout from:', choices: domainChoices, } ]); // Extract domain from the selected choice const selectedDomain = selected.split(' ')[0]; // Remove credentials using utility function (0, auth_1.removeCredentials)(selectedDomain); // Also remove all credential files for this domain removeCredentialFiles(selectedDomain); console.log(chalk_1.default.green(`Successfully logged out from ${selectedDomain}`)); } } catch (error) { const err = error; console.error(chalk_1.default.red('Error:'), err.message); } })); } //# sourceMappingURL=logout.js.map