@tripsnek/tmf
Version:
TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)
57 lines • 2.23 kB
JavaScript
import { EcoreParser } from '../ecore/ecoreparser';
import { TGeneratorMain } from './tgenerator-main';
import { Environment, safeDynamicImport } from '../utils/environment';
/**
* (1) Parses an ECore file.
* (2) Generates source code into destinationPath/src/lib
* (3) Generates barrel file (index.ts) into destinationPath/src
*
* If not specified, destinationPath will be that of the model file.
*
* @param ecorePath
* @param overwriteImpl
* @param destinationPath
*/
export async function generateFromEcore(ecorePath, overwriteImpl, destinationPath, attemptFormatWithPrettier) {
Environment.requireNodeEnvironment('File-based code generation');
// parse the package
const pkg = await new EcoreParser().parseAsync(ecorePath);
// auto-determine destination path from ecore location, if necessary
let destPath;
if (destinationPath) {
destPath = destinationPath;
}
else {
try {
const path = await safeDynamicImport('path');
destPath = path.dirname(ecorePath);
}
catch (error) {
throw new Error(`Path resolution failed: ${error.message}. This operation requires Node.js environment.`);
}
}
// generate the source
return generateFromEPackage(pkg, destPath, overwriteImpl, attemptFormatWithPrettier);
}
/**
* (1) Generates source code into destinationPath/src/lib
* (2) Generates barrel file (index.ts) into destinationPath/src
*
* @param pkg
* @param destPath
* @param overwriteImpl
*/
export async function generateFromEPackage(pkg, destPath, overwriteImpl, attemptInvokePrettier) {
Environment.requireNodeEnvironment('Code generation');
try {
const path = await safeDynamicImport('path');
const srcPath = path.resolve(destPath + '/src');
console.log('running TGeneratorMain, output to ' + srcPath);
new TGeneratorMain(pkg, path.resolve(destPath + '/src'), overwriteImpl, path.resolve(destPath + '/src')).generate(attemptInvokePrettier);
return srcPath;
}
catch (error) {
throw new Error(`Path resolution failed: ${error.message}. This operation requires Node.js environment.`);
}
}
//# sourceMappingURL=modelgenutils.js.map