From 990ddccd277083544ff19a0cb6f172a2a39121e2 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 5 Feb 2026 19:31:40 +0000 Subject: [PATCH] Initial upload with CI/CD workflow --- src/commands/import.ts | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/commands/import.ts diff --git a/src/commands/import.ts b/src/commands/import.ts new file mode 100644 index 0000000..e92e127 --- /dev/null +++ b/src/commands/import.ts @@ -0,0 +1,77 @@ +import * as path from 'path'; +import * as fs from 'fs-extra'; +import * as yaml from 'js-yaml'; +import inquirer from 'inquirer'; +import { fileUtils } from '../utils/fileUtils'; +import { Layout } from '../models/types'; + +interface ImportOptions { + file: string; + name?: string; +} + +export async function importLayout(options: ImportOptions): Promise { + await fileUtils.initialize(); + + if (!fs.existsSync(options.file)) { + throw new Error(`File not found: ${options.file}`); + } + + let layout: Layout; + const fileExt = path.extname(options.file).toLowerCase(); + + try { + if (fileExt === '.yaml' || fileExt === '.yml') { + const content = fs.readFileSync(options.file, 'utf-8'); + layout = yaml.load(content) as Layout; + } else { + layout = fs.readJsonSync(options.file) as Layout; + } + } catch (error) { + throw new Error(`Failed to parse layout file: ${(error as Error).message}`); + } + + validateLayout(layout); + + let name = options.name; + if (!name) { + const baseName = path.basename(options.file).replace(/\.(json|yaml|yml)$/, ''); + const existingLayouts = await fileUtils.listLayouts(); + const availableName = existingLayouts.includes(`${baseName}.json`) + ? `${baseName}-imported` + : baseName; + + const { layoutName } = await inquirer.prompt([ + { + type: 'input', + name: 'layoutName', + message: 'Enter a name for the imported layout:', + default: availableName, + validate: (input: string) => input.trim() !== '' || 'Name cannot be empty', + }, + ]); + name = layoutName; + } + + layout.updatedAt = new Date().toISOString(); + + const fileName = name || 'imported-layout'; + const filePath = await fileUtils.saveLayout(fileName, layout, 'json'); + console.log(`Layout imported successfully: ${filePath}`); + console.log(`Terminal type: ${layout.terminalType}`); + console.log(`Windows: ${layout.session.windows.length}`); + console.log(`Description: ${layout.metadata?.description || 'N/A'}`); +} + +function validateLayout(layout: Layout): void { + const required = ['version', 'terminalType', 'createdAt', 'session']; + for (const field of required) { + if (!(field in layout)) { + throw new Error(`Invalid layout: missing required field "${field}"`); + } + } + + if (!layout.session || !layout.session.windows) { + throw new Error('Invalid layout: missing session.windows'); + } +}