From 10fac70aff752d65693a0afbef53a10a03e975c9 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 5 Feb 2026 19:31:55 +0000 Subject: [PATCH] Initial upload with CI/CD workflow --- src/parsers/iterm2Parser.ts | 141 ++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 src/parsers/iterm2Parser.ts diff --git a/src/parsers/iterm2Parser.ts b/src/parsers/iterm2Parser.ts new file mode 100644 index 0000000..d8b46fb --- /dev/null +++ b/src/parsers/iterm2Parser.ts @@ -0,0 +1,141 @@ +import * as fs from 'fs-extra'; +import * as path from 'path'; +import { osUtils } from '../utils/osUtils'; +import { Layout, Session, Window, ParserResult, TerminalType } from '../models/types'; + +interface iTerm2Profile { + name: string; + guid: string; + shortcut: string; + command: string; + working_directory: string; +} + +export class iTerm2Parser { + private configDir: string; + + constructor() { + this.configDir = path.join(osUtils.getHomeDir(), 'Library', 'Application Support', 'iTerm2'); + } + + async isAvailable(): Promise { + return osUtils.isMacOS() && fs.pathExistsSync(this.configDir); + } + + async listProfiles(): Promise { + const plistPath = path.join(this.configDir, 'DynamicProfiles', 'profiles.json'); + if (!fs.existsSync(plistPath)) { + return []; + } + + try { + const content = fs.readFileSync(plistPath, 'utf-8'); + const profiles = JSON.parse(content); + return Array.isArray(profiles) ? profiles.map((p: iTerm2Profile) => p.name) : []; + } catch { + return []; + } + } + + async captureCurrentLayout(): Promise> { + try { + if (!osUtils.isMacOS()) { + return { success: false, error: 'iTerm2 is only available on macOS' }; + } + + const layout = this.createDefaultLayout(); + return { success: true, data: layout }; + } catch (error) { + return { success: false, error: (error as Error).message }; + } + } + + private createDefaultLayout(): Layout { + const window: Window = { + id: 'w0', + index: 0, + name: 'iTerm2', + panes: [ + { + id: 'p0', + index: 0, + layout: { x: 0, y: 0, width: 80, height: 24 }, + cwd: osUtils.getHomeDir(), + }, + ], + activePaneIndex: 0, + }; + + const session: Session = { + id: 's-iterm2', + name: 'iterm2', + windows: [window], + activeWindowIndex: 0, + }; + + return { + version: '1.0.0', + terminalType: 'iterm2' as TerminalType, + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + session, + metadata: { + description: 'iTerm2 layout configuration', + }, + }; + } + + async generateRestoreScript(layout: Layout): Promise { + const commands: string[] = []; + + commands.push('#!/bin/bash'); + commands.push('# iTerm2 layout restoration script'); + commands.push('# Note: iTerm2 scripting requires AppleScript/JavaScript for Automation'); + + for (const window of layout.session.windows) { + for (const pane of window.panes) { + if (pane.cwd) { + commands.push(`osascript -e 'tell application "iTerm2" to tell current window to create pane with profile "Default" directory "${pane.cwd}"'`); + } else { + commands.push('osascript -e \'tell application "iTerm2" to tell current window to create pane with profile "Default"\''); + } + } + } + + return commands.join('\n'); + } + + async exportProfiles(outputPath: string): Promise> { + try { + const profilesPath = path.join(this.configDir, 'DynamicProfiles', 'profiles.json'); + if (!fs.existsSync(profilesPath)) { + return { success: false, error: 'iTerm2 profiles not found' }; + } + + const content = fs.readFileSync(profilesPath, 'utf-8'); + await fs.writeFile(outputPath, content, 'utf-8'); + + return { success: true, data: outputPath }; + } catch (error) { + return { success: false, error: (error as Error).message }; + } + } + + async importProfiles(inputPath: string): Promise> { + try { + const profilesPath = path.join(this.configDir, 'DynamicProfiles', 'profiles.json'); + const content = await fs.readFile(inputPath, 'utf-8'); + + JSON.parse(content); + + await fs.ensureDir(path.dirname(profilesPath)); + await fs.writeFile(profilesPath, content, 'utf-8'); + + return { success: true }; + } catch (error) { + return { success: false, error: (error as Error).message }; + } + } +} + +export const iterm2Parser = new iTerm2Parser();