@yuxilabs/gptp-core
Version:
Core validation, formatting and execution logic for the GPTP file format.
30 lines (29 loc) • 898 B
JavaScript
// src/engine/fs/index.ts
import fs from 'fs';
import path from 'path';
const GPTP_EXTENSION = '.gptp';
export function isGptpFile(filePath) {
return path.extname(filePath) === GPTP_EXTENSION;
}
export function readGptpFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`GPTP file not found: ${filePath}`);
}
if (!isGptpFile(filePath)) {
throw new Error(`Expected a .gptp file: ${filePath}`);
}
const raw = fs.readFileSync(filePath, 'utf8');
try {
return JSON.parse(raw);
}
catch (err) {
throw new Error(`Invalid JSON in ${filePath}: ${err.message}`);
}
}
export function writeGptpFile(filePath, data) {
if (!isGptpFile(filePath)) {
throw new Error(`Expected a .gptp file: ${filePath}`);
}
const formatted = JSON.stringify(data, null, 2);
fs.writeFileSync(filePath, formatted, 'utf8');
}