This commit is contained in:
82
src/commands/export.ts
Normal file
82
src/commands/export.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
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';
|
||||
|
||||
interface ExportOptions {
|
||||
name: string;
|
||||
output?: string;
|
||||
format?: 'json' | 'yaml';
|
||||
}
|
||||
|
||||
export async function exportLayout(options: ExportOptions): Promise<void> {
|
||||
await fileUtils.initialize();
|
||||
|
||||
let layoutName = options.name;
|
||||
if (!layoutName) {
|
||||
const layouts = await fileUtils.listLayouts();
|
||||
if (layouts.length === 0) {
|
||||
throw new Error('No layouts found. Use "save" command first.');
|
||||
}
|
||||
|
||||
const { selectedLayout } = await inquirer.prompt([
|
||||
{
|
||||
type: 'list',
|
||||
name: 'selectedLayout',
|
||||
message: 'Select a layout to export:',
|
||||
choices: layouts,
|
||||
},
|
||||
]);
|
||||
layoutName = selectedLayout;
|
||||
}
|
||||
|
||||
let layout: unknown;
|
||||
try {
|
||||
layout = await fileUtils.readLayout(layoutName);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to read layout: ${(error as Error).message}`);
|
||||
}
|
||||
|
||||
let outputPath = options.output;
|
||||
if (!outputPath) {
|
||||
const defaultName = layoutName.replace(/\.(json|yaml|yml)$/, '');
|
||||
const format = options.format || 'json';
|
||||
const suggestedName = `${defaultName}-exported.${format}`;
|
||||
|
||||
const { filePath } = await inquirer.prompt([
|
||||
{
|
||||
type: 'input',
|
||||
name: 'filePath',
|
||||
message: 'Enter output file path:',
|
||||
default: suggestedName,
|
||||
},
|
||||
]);
|
||||
outputPath = filePath;
|
||||
}
|
||||
|
||||
let format = options.format || 'json';
|
||||
const ext = path.extname(outputPath || '').toLowerCase();
|
||||
if (ext === '.yaml' || ext === '.yml') {
|
||||
format = 'yaml';
|
||||
} else if (ext === '.json') {
|
||||
format = 'json';
|
||||
} else if (!outputPath) {
|
||||
format = 'json';
|
||||
}
|
||||
|
||||
try {
|
||||
if (format === 'yaml') {
|
||||
const yamlContent = yaml.dump(layout);
|
||||
fs.writeFileSync(outputPath!, yamlContent, 'utf-8');
|
||||
} else {
|
||||
fs.writeJsonSync(outputPath!, layout, { spaces: 2 });
|
||||
}
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to write file: ${(error as Error).message}`);
|
||||
}
|
||||
|
||||
console.log(`Layout exported to: ${outputPath}`);
|
||||
console.log(`Format: ${format}`);
|
||||
console.log(`Size: ${fs.statSync(outputPath!).size} bytes`);
|
||||
}
|
||||
Reference in New Issue
Block a user