UNPKG

envx-cli

Version:

Environment file encryption and management tool

213 lines 7.33 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CliUtils = exports.ExecUtils = void 0; const chalk_1 = __importDefault(require("chalk")); const child_process_1 = require("child_process"); const fs_1 = require("fs"); const shelljs_1 = __importDefault(require("shelljs")); class ExecUtils { static exec(command, options = {}) { try { const { silent = false, cwd } = options; if (!silent) { console.log(chalk_1.default.blue(`Executing: ${command}`)); } const result = (0, child_process_1.execSync)(command, { cwd: cwd || process.cwd(), encoding: 'utf-8', stdio: silent ? 'pipe' : 'inherit', }); return { success: true, message: 'Command executed successfully', data: result, }; } catch (error) { const err = error; return { success: false, message: `Command failed: ${err.message}`, errors: [err.stderr || err.message], }; } } static encryptFile(filePath, passphrase) { const command = `gpg --passphrase "${passphrase}" --quiet --yes --batch -c "${filePath}"`; return this.exec(command, { silent: true }); } static decryptFile(encryptedPath, outputPath, passphrase) { const command = `gpg --passphrase "${passphrase}" --quiet --yes --batch -o "${outputPath}" -d "${encryptedPath}"`; return this.exec(command, { silent: true }); } static isGpgAvailable() { try { (0, child_process_1.execSync)('gpg --version', { stdio: 'pipe' }); return true; } catch { return false; } } static getCurrentDir() { return shelljs_1.default.pwd().toString(); } static dirExists(path) { return shelljs_1.default.test('-d', path); } static fileExists(path) { return shelljs_1.default.test('-f', path); } static ensureDir(path) { shelljs_1.default.mkdir('-p', path); } static copyFile(source, destination) { try { shelljs_1.default.cp(source, destination); return { success: true, message: `File copied from ${source} to ${destination}`, }; } catch (error) { return { success: false, message: `Failed to copy file: ${error}`, errors: [String(error)], }; } } static moveFile(source, destination) { try { shelljs_1.default.mv(source, destination); return { success: true, message: `File moved from ${source} to ${destination}`, }; } catch (error) { return { success: false, message: `Failed to move file: ${error}`, errors: [String(error)], }; } } static removeFile(path) { try { shelljs_1.default.rm(path); return { success: true, message: `File removed: ${path}`, }; } catch (error) { return { success: false, message: `Failed to remove file: ${error}`, errors: [String(error)], }; } } static testGpgOperation(passphrase) { const testContent = 'test-content-for-gpg'; const testFile = `/tmp/envx-test-${Date.now()}.txt`; const encryptedFile = `${testFile}.gpg`; try { (0, fs_1.writeFileSync)(testFile, testContent); const encryptResult = this.encryptFile(testFile, passphrase); if (!encryptResult.success) { shelljs_1.default.rm('-f', testFile); return encryptResult; } const decryptedFile = `/tmp/envx-test-decrypted-${Date.now()}.txt`; const decryptResult = this.decryptFile(encryptedFile, decryptedFile, passphrase); shelljs_1.default.rm('-f', testFile, encryptedFile, decryptedFile); if (!decryptResult.success) { return decryptResult; } return { success: true, message: 'GPG operations test passed', }; } catch (error) { shelljs_1.default.rm('-f', testFile, encryptedFile); return { success: false, message: `GPG test failed: ${error}`, errors: [String(error)], }; } } } exports.ExecUtils = ExecUtils; class CliUtils { static success(message) { console.log(chalk_1.default.green('✓'), message); } static error(message) { console.log(chalk_1.default.red('✗'), message); } static warning(message) { console.log(chalk_1.default.yellow('⚠'), message); } static info(message) { console.log(chalk_1.default.blue('ℹ'), message); } static header(message) { console.log(); console.log(chalk_1.default.bold.cyan(message)); console.log(chalk_1.default.cyan('='.repeat(message.length))); } static subheader(message) { console.log(); console.log(chalk_1.default.bold(message)); console.log('-'.repeat(message.length)); } static printFileOperation(result, operation) { if (result.success) { this.success(`${operation}: ${result.message}`); } else { this.error(`${operation}: ${result.message}`); if (result.errors) { result.errors.forEach(error => { console.log(chalk_1.default.red(` • ${error}`)); }); } } } static printTable(headers, rows) { const columnWidths = headers.map((header, index) => { const columnValues = [header, ...rows.map(row => row[index] || '')]; return Math.max(...columnValues.map(val => val.length)); }); const headerRow = headers .map((header, index) => header.padEnd(columnWidths[index])) .join(' | '); console.log(chalk_1.default.bold(headerRow)); console.log(columnWidths.map(width => '-'.repeat(width)).join('-|-')); rows.forEach(row => { const formattedRow = row .map((cell, index) => (cell || '').padEnd(columnWidths[index])) .join(' | '); console.log(formattedRow); }); } static formatPath(path, cwd) { const relativePath = path.replace(cwd, '.'); return chalk_1.default.cyan(relativePath); } static formatEnvironment(env) { return chalk_1.default.magenta(env); } static formatStatus(status, success = true) { return success ? chalk_1.default.green(status) : chalk_1.default.red(status); } } exports.CliUtils = CliUtils; //# sourceMappingURL=exec.js.map