lsh-framework
Version: 
A powerful, extensible shell with advanced job management, database persistence, and modern CLI features
178 lines (175 loc) • 6.02 kB
JavaScript
/**
 * .lshrc Initialization
 * Creates and manages the user's .lshrc configuration file
 */
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class LshrcManager {
    lshrcPath;
    constructor(lshrcPath) {
        this.lshrcPath = lshrcPath || path.join(os.homedir(), '.lshrc');
    }
    /**
     * Initialize .lshrc if it doesn't exist
     */
    initialize(options = {}) {
        try {
            // Check if .lshrc already exists
            if (fs.existsSync(this.lshrcPath)) {
                return false; // Already exists, no action needed
            }
            if (!options.createIfMissing) {
                return false; // Don't create if not requested
            }
            // Copy template to user's home directory
            const templatePath = path.join(__dirname, '../../templates/.lshrc.template');
            if (!fs.existsSync(templatePath)) {
                // Template not found, create basic .lshrc
                this.createBasicLshrc(options);
                return true;
            }
            // Copy template
            let content = fs.readFileSync(templatePath, 'utf8');
            // Enable auto-import if requested
            if (options.autoImportZsh) {
                const importCmd = `zsh-source${options.importOptions ? ' ' + options.importOptions.join(' ') : ''}`;
                content = content.replace('# zsh-source', importCmd);
            }
            fs.writeFileSync(this.lshrcPath, content, 'utf8');
            console.log(`✅ Created ${this.lshrcPath}`);
            return true;
        }
        catch (error) {
            console.error(`Failed to initialize .lshrc: ${error.message}`);
            return false;
        }
    }
    /**
     * Create basic .lshrc without template
     */
    createBasicLshrc(options) {
        const content = `# LSH Configuration File
# Location: ${this.lshrcPath}
${options.autoImportZsh ? `# Auto-import ZSH configurations
zsh-source${options.importOptions ? ' ' + options.importOptions.join(' ') : ''}
` : '# Uncomment to auto-import ZSH configurations\n# zsh-source\n'}
# Add your aliases, functions, and environment variables here
`;
        fs.writeFileSync(this.lshrcPath, content, 'utf8');
        console.log(`✅ Created ${this.lshrcPath}`);
    }
    /**
     * Check if .lshrc exists
     */
    exists() {
        return fs.existsSync(this.lshrcPath);
    }
    /**
     * Source .lshrc commands
     */
    async source(_executor) {
        if (!this.exists()) {
            return [];
        }
        try {
            const content = fs.readFileSync(this.lshrcPath, 'utf8');
            const commands = [];
            for (const line of content.split('\n')) {
                const trimmed = line.trim();
                // Skip comments and empty lines
                if (trimmed.startsWith('#') || trimmed === '') {
                    continue;
                }
                commands.push(trimmed);
            }
            return commands;
        }
        catch (error) {
            console.error(`Failed to source .lshrc: ${error.message}`);
            return [];
        }
    }
    /**
     * Enable auto-import in existing .lshrc
     */
    enableAutoImport(options = []) {
        try {
            if (!this.exists()) {
                // Create new .lshrc with auto-import enabled
                this.initialize({ autoImportZsh: true, importOptions: options, createIfMissing: true });
                return true;
            }
            let content = fs.readFileSync(this.lshrcPath, 'utf8');
            // Check if auto-import is already enabled
            if (content.includes('zsh-source') && !content.match(/^#.*zsh-source/m)) {
                console.log('Auto-import is already enabled in .lshrc');
                return false;
            }
            // Add auto-import configuration
            const autoImportBlock = `
# ZSH Auto-Import (added by LSH)
zsh-source${options.length > 0 ? ' ' + options.join(' ') : ''}
`;
            // Find the ZSH import section or add at the top
            if (content.includes('# ZSH IMPORT CONFIGURATION')) {
                content = content.replace(/# zsh-source/, `zsh-source${options.length > 0 ? ' ' + options.join(' ') : ''}`);
            }
            else {
                content = autoImportBlock + '\n' + content;
            }
            fs.writeFileSync(this.lshrcPath, content, 'utf8');
            console.log('✅ Auto-import enabled in .lshrc');
            return true;
        }
        catch (error) {
            console.error(`Failed to enable auto-import: ${error.message}`);
            return false;
        }
    }
    /**
     * Disable auto-import in .lshrc
     */
    disableAutoImport() {
        try {
            if (!this.exists()) {
                return false;
            }
            let content = fs.readFileSync(this.lshrcPath, 'utf8');
            // Comment out zsh-source lines
            content = content.replace(/^(zsh-source.*)$/gm, '# $1');
            // Remove auto-import blocks
            content = content.replace(/# ZSH Auto-Import[\s\S]*?(?=\n#|\n\n|$)/g, '');
            fs.writeFileSync(this.lshrcPath, content, 'utf8');
            console.log('✅ Auto-import disabled in .lshrc');
            return true;
        }
        catch (error) {
            console.error(`Failed to disable auto-import: ${error.message}`);
            return false;
        }
    }
    /**
     * Get .lshrc path
     */
    getPath() {
        return this.lshrcPath;
    }
}
/**
 * Initialize .lshrc on first run
 */
export function initializeLshrc(options = {}) {
    const manager = new LshrcManager();
    return manager.initialize(options);
}
/**
 * Check if .lshrc exists
 */
export function lshrcExists() {
    const manager = new LshrcManager();
    return manager.exists();
}