UNPKG

@papavault/cli

Version:

CLI tool for Papa Vault Secret Manager

106 lines (105 loc) 3.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getToken = getToken; exports.setToken = setToken; exports.removeToken = removeToken; exports.isAuthenticated = isAuthenticated; exports.getConfigFilePath = getConfigFilePath; exports.getProjectConfig = getProjectConfig; exports.setProjectConfig = setProjectConfig; exports.isProjectConfigured = isProjectConfigured; exports.getProjectId = getProjectId; exports.getStage = getStage; const conf_1 = __importDefault(require("conf")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); // Global configuration const config = new conf_1.default({ projectName: 'papavault', defaults: {} }); // Get the token from various sources function getToken() { // Priority: 1. Environment variable, 2. Local .env file, 3. Global config return process.env.PAPAVAULT_TOKEN || config.get('token'); } // Set the token in the global config function setToken(token) { config.set('token', token); } // Remove the token from the global config function removeToken() { config.delete('token'); } // Check if the user is authenticated function isAuthenticated() { return !!getToken(); } // Project configuration (.papavault.json) const PROJECT_CONFIG_FILE = '.papavault.json'; // Get the absolute path of the configuration file function getConfigFilePath() { try { // Start from the current working directory let currentDir = process.cwd(); // Traverse up the directory tree until we find the config file or reach the root while (currentDir !== path_1.default.parse(currentDir).root) { const configPath = path_1.default.join(currentDir, PROJECT_CONFIG_FILE); if (fs_1.default.existsSync(configPath)) { return configPath; } // Move up one directory currentDir = path_1.default.dirname(currentDir); } // Check the root directory as well const rootConfigPath = path_1.default.join(path_1.default.parse(process.cwd()).root, PROJECT_CONFIG_FILE); if (fs_1.default.existsSync(rootConfigPath)) { return rootConfigPath; } // If we get here, we couldn't find the config file return null; } catch (error) { console.error('Error finding configuration file:', error); return null; } } // Get the project configuration from the local file function getProjectConfig() { try { const configPath = getConfigFilePath(); if (configPath && fs_1.default.existsSync(configPath)) { const configData = fs_1.default.readFileSync(configPath, 'utf8'); return JSON.parse(configData); } } catch (error) { console.error('Error reading project configuration:', error); } return null; } // Set the project configuration in the local file function setProjectConfig(projectId, stage) { const projectConfig = { projectId, stage }; const configPath = path_1.default.join(process.cwd(), PROJECT_CONFIG_FILE); fs_1.default.writeFileSync(configPath, JSON.stringify(projectConfig, null, 2)); } // Check if the project is configured function isProjectConfigured() { return !!getProjectConfig(); } // Get the project ID from various sources function getProjectId() { // Priority: 1. Command line argument, 2. Local config file const projectConfig = getProjectConfig(); return projectConfig?.projectId; } // Get the stage from various sources function getStage() { // Priority: 1. Command line argument, 2. Local config file const projectConfig = getProjectConfig(); return projectConfig?.stage; }