UNPKG

build-in-public-bot

Version:

AI-powered CLI bot for automating build-in-public tweets with code screenshots

224 lines 8.41 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ShellDetector = void 0; const child_process_1 = require("child_process"); const fs = __importStar(require("fs")); const os = __importStar(require("os")); const path = __importStar(require("path")); const logger_1 = require("../utils/logger"); class ShellDetector { homeDir; constructor() { this.homeDir = os.homedir(); } detectCurrentShell() { try { const shellEnv = process.env.SHELL; if (shellEnv) { return this.getShellInfo(shellEnv); } if (process.platform !== 'win32') { const parentShell = (0, child_process_1.execSync)('ps -p $$ -o comm=', { encoding: 'utf-8' }).trim(); if (parentShell) { return this.getShellInfo(parentShell); } } return null; } catch (error) { logger_1.logger.debug('Failed to detect current shell:', error); return null; } } detectInstalledShells() { const shells = []; const shellChecks = [ { name: 'bash', paths: ['/bin/bash', '/usr/bin/bash', '/usr/local/bin/bash'] }, { name: 'zsh', paths: ['/bin/zsh', '/usr/bin/zsh', '/usr/local/bin/zsh'] }, { name: 'fish', paths: ['/usr/bin/fish', '/usr/local/bin/fish'] }, { name: 'sh', paths: ['/bin/sh', '/usr/bin/sh'] } ]; for (const check of shellChecks) { for (const shellPath of check.paths) { if (fs.existsSync(shellPath)) { const info = this.getShellInfo(shellPath); if (info && !shells.find(s => s.name === info.name)) { shells.push(info); } break; } } } return shells; } getShellInfo(shellPath) { const basename = path.basename(shellPath); switch (basename) { case 'bash': return { name: 'bash', version: this.getShellVersion(shellPath), configFile: path.join(this.homeDir, '.bashrc'), executable: shellPath }; case 'zsh': return { name: 'zsh', version: this.getShellVersion(shellPath), configFile: path.join(this.homeDir, '.zshrc'), executable: shellPath }; case 'fish': return { name: 'fish', version: this.getShellVersion(shellPath), configFile: path.join(this.homeDir, '.config/fish/config.fish'), executable: shellPath }; case 'sh': return { name: 'sh', configFile: path.join(this.homeDir, '.profile'), executable: shellPath }; default: return null; } } getShellVersion(shellPath) { try { const output = (0, child_process_1.execSync)(`${shellPath} --version`, { encoding: 'utf-8' }); const match = output.match(/(\d+\.\d+(\.\d+)?)/); return match ? match[1] : undefined; } catch { return undefined; } } checkAliasExists(shell, alias) { try { if (!fs.existsSync(shell.configFile)) { return false; } const content = fs.readFileSync(shell.configFile, 'utf-8'); const aliasPattern = new RegExp(`^\\s*alias\\s+${alias}=`, 'm'); if (shell.name === 'fish') { const fishPattern = new RegExp(`^\\s*alias\\s+${alias}\\s+`, 'm'); return fishPattern.test(content); } return aliasPattern.test(content); } catch { return false; } } installAlias(shell, config) { const aliasLine = this.formatAlias(shell, config); const configDir = path.dirname(shell.configFile); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } if (!fs.existsSync(shell.configFile)) { fs.writeFileSync(shell.configFile, ''); } let content = fs.readFileSync(shell.configFile, 'utf-8'); if (content.length > 0 && !content.endsWith('\n')) { content += '\n'; } const sectionMarker = '# Build-in-public bot aliases'; if (!content.includes(sectionMarker)) { content += `\n${sectionMarker}\n`; } content += `${aliasLine}\n`; fs.writeFileSync(shell.configFile, content); } formatAlias(shell, config) { const comment = config.description ? `# ${config.description}` : ''; switch (shell.name) { case 'fish': if (config.command.includes(' ')) { return `${comment}${comment ? '\n' : ''}alias ${config.alias} '${config.command}'`; } return `${comment}${comment ? '\n' : ''}alias ${config.alias} ${config.command}`; default: return `${comment}${comment ? '\n' : ''}alias ${config.alias}='${config.command}'`; } } removeAlias(shell, alias) { try { if (!fs.existsSync(shell.configFile)) { return false; } const content = fs.readFileSync(shell.configFile, 'utf-8'); const lines = content.split('\n'); const aliasPattern = shell.name === 'fish' ? new RegExp(`^\\s*alias\\s+${alias}\\s+`) : new RegExp(`^\\s*alias\\s+${alias}=`); const filteredLines = lines.filter(line => !aliasPattern.test(line)); if (filteredLines.length === lines.length) { return false; } fs.writeFileSync(shell.configFile, filteredLines.join('\n')); return true; } catch { return false; } } getAliases(shell) { const aliases = new Map(); try { if (!fs.existsSync(shell.configFile)) { return aliases; } const content = fs.readFileSync(shell.configFile, 'utf-8'); const lines = content.split('\n'); for (const line of lines) { const match = shell.name === 'fish' ? line.match(/^\s*alias\s+(\w+)\s+['"]?(.+?)['"]?\s*$/) : line.match(/^\s*alias\s+(\w+)=['"](.+?)['"]\s*$/); if (match) { aliases.set(match[1], match[2]); } } } catch { } return aliases; } } exports.ShellDetector = ShellDetector; //# sourceMappingURL=shell-detector.js.map