@tripsnek/tmf
Version:
TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)
115 lines • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EcoreWriter = void 0;
const ecore_string_writer_1 = require("./ecore-string-writer");
const environment_1 = require("../utils/environment");
/**
* Writes an EPackage metamodel to an XML Ecore file.
*/
class EcoreWriter {
stringWriter = new ecore_string_writer_1.EcoreStringWriter();
/**
* Converts an EPackage to Ecore XML string format.
* @param ePackage The root package to serialize
* @returns The XML string representation
*/
writeToString(ePackage) {
return this.stringWriter.writeToString(ePackage);
}
/**
* Writes an EPackage to an Ecore XML file.
* @param ePackage The root package to serialize
* @param filePath The path where the .ecore file will be written
* @deprecated Use writeToFileAsync instead for bundle-safe operation
*/
async writeToFile(ePackage, filePath) {
return this.writeToFileAsync(ePackage, filePath);
}
/**
* Async version of writeToFile for better error handling
*/
async writeToFileAsync(ePackage, filePath) {
environment_1.Environment.requireNodeEnvironment('File writing');
const xmlContent = this.writeToString(ePackage);
try {
const fs = await (0, environment_1.safeDynamicImport)('fs');
const path = await (0, environment_1.safeDynamicImport)('path');
fs.writeFileSync(path.resolve(filePath), xmlContent, 'utf8');
}
catch (error) {
throw new Error(`File system access failed: ${error.message}. This operation requires Node.js environment.`);
}
}
/**
* Browser-compatible file saving via download
* @param ePackage The root package to serialize
* @param filename The filename for download
*/
saveAsDownload(ePackage, filename) {
if (!environment_1.Environment.isBrowser) {
throw new Error('Download functionality only available in browser environment');
}
// try {
// const xmlContent = this.writeToString(ePackage);
// const blob = new Blob([xmlContent], { type: 'application/xml' });
// const url = URL.createObjectURL(blob);
// // Create temporary download link
// const a = document.createElement('a');
// a.href = url;
// a.download = filename.endsWith('.ecore') ? filename : `${filename}.ecore`;
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a);
// URL.revokeObjectURL(url);
// } catch (error) {
// throw new Error(`Browser download failed: ${(error as Error).message}`);
// }
}
/**
* Universal save method - chooses appropriate strategy based on environment
*/
async save(ePackage, pathOrFilename) {
if (environment_1.Environment.isNode) {
await this.writeToFileAsync(ePackage, pathOrFilename);
}
else if (environment_1.Environment.isBrowser) {
this.saveAsDownload(ePackage, pathOrFilename);
}
else {
throw new Error('File saving not supported in this environment');
}
}
/**
* Universal async save method
*/
async saveAsync(ePackage, pathOrFilename) {
return this.save(ePackage, pathOrFilename);
}
/**
* Check if file writing is supported in current environment
*/
static isFileWritingSupported() {
return environment_1.Environment.isNode;
}
/**
* Check if download saving is supported in current environment
*/
static isDownloadSupported() {
return environment_1.Environment.isBrowser;
}
/**
* Get supported operations in current environment
*/
static getSupportedOperations() {
const operations = ['writeToString'];
if (this.isFileWritingSupported()) {
operations.push('writeToFile', 'writeToFileAsync', 'save', 'saveAsync');
}
if (this.isDownloadSupported()) {
operations.push('saveAsDownload', 'save', 'saveAsync');
}
return operations;
}
}
exports.EcoreWriter = EcoreWriter;
//# sourceMappingURL=ecorewriter.js.map