generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
68 lines (67 loc) • 2.19 kB
JavaScript
import logger from '../utils/objects/logger.js';
import * as parser from '../parsing/api.js';
import performJDLPostParsingTasks from '../parsing/jdl-post-parsing-tasks.js';
import { readFile, readFiles } from './file-reader.js';
export function parseFromFiles(files) {
checkFiles(files);
checkAllTheFilesAreJDLFiles(files);
return parse(getFilesContent(files));
}
export function parseFromContent(content) {
if (!content) {
throw new Error('A valid JDL content must be passed so as to be parsed.');
}
return parse(content);
}
export function getCstFromContent(content) {
return getCst(content);
}
function checkFiles(files) {
if (!files || files.length === 0) {
throw new Error('The files must be passed to be parsed.');
}
}
function getFilesContent(files) {
return files.length === 1 ? readFile(files[0]) : aggregateFiles(files);
}
function checkAllTheFilesAreJDLFiles(files) {
for (let i = 0; i < files.length; i++) {
checkFileIsJDLFile(files[i]);
}
}
function parse(content) {
const parsedContent = callApiMethod('parse', content);
return performJDLPostParsingTasks(parsedContent);
}
function getCst(content) {
return callApiMethod('getCst', content);
}
function callApiMethod(methodName, content) {
if (!content) {
throw new Error('File content must be passed, it is currently empty.');
}
try {
const processedInput = filterJDLDirectives(removeInternalJDLComments(content));
return parser[methodName](processedInput);
}
catch (error) {
if (error instanceof SyntaxError) {
logger.error(`Syntax error message:\n\t${error.message}`);
}
throw error;
}
}
function removeInternalJDLComments(content) {
return content.replace(/\/\/[^\n\r]*/gm, '');
}
function filterJDLDirectives(content) {
return content.replace(/^\u0023.*/gm, '');
}
export function checkFileIsJDLFile(file) {
if (!file.endsWith('.jh') && !file.endsWith('.jdl')) {
throw new Error(`The passed file '${file}' must end with '.jh' or '.jdl' to be valid.`);
}
}
function aggregateFiles(files) {
return readFiles(files).join('\n');
}