Initial upload with CI/CD workflow
Some checks failed
CI / test (push) Has been cancelled

This commit is contained in:
2026-02-05 19:31:40 +00:00
parent bb92093808
commit 990ddccd27

77
src/commands/import.ts Normal file
View File

@@ -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<void> {
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');
}
}