refactorings in parsing
This commit is contained in:
29
src/application/Parser/ApplicationParser.ts
Normal file
29
src/application/Parser/ApplicationParser.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Category } from '../../domain/Category';
|
||||
import { Application } from '../../domain/Application';
|
||||
import { Script } from '@/domain/Script';
|
||||
import applicationFile from 'js-yaml-loader!./../application.yaml';
|
||||
import { parseCategory } from './CategoryParser';
|
||||
|
||||
interface ApplicationResult {
|
||||
readonly application: Application;
|
||||
readonly selectedScripts: Script[];
|
||||
}
|
||||
|
||||
export function buildApplication(): ApplicationResult {
|
||||
const name = applicationFile.name as string;
|
||||
const version = applicationFile.version as number;
|
||||
const categories = new Array<Category>();
|
||||
const selectedScripts = new Array<Script>();
|
||||
if (!applicationFile.actions || applicationFile.actions.length <= 0) {
|
||||
throw new Error('Application does not define any action');
|
||||
}
|
||||
for (const action of applicationFile.actions) {
|
||||
const category = parseCategory({
|
||||
category: action,
|
||||
selectedScripts,
|
||||
});
|
||||
categories.push(category);
|
||||
}
|
||||
const app = new Application(name, version, categories);
|
||||
return { application: app, selectedScripts };
|
||||
}
|
||||
70
src/application/Parser/CategoryParser.ts
Normal file
70
src/application/Parser/CategoryParser.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { YamlCategory, YamlScript } from 'js-yaml-loader!./application.yaml';
|
||||
import { Script } from '@/domain/Script';
|
||||
import { Category } from '../../domain/Category';
|
||||
import { parseDocUrls } from './DocumentationParser';
|
||||
|
||||
let categoryIdCounter: number = 0;
|
||||
|
||||
interface IParsingContext {
|
||||
category: YamlCategory;
|
||||
selectedScripts: Script[];
|
||||
}
|
||||
|
||||
interface ICategoryChildren {
|
||||
subCategories: Category[];
|
||||
subScripts: Script[];
|
||||
}
|
||||
|
||||
export function parseCategory(context: IParsingContext): Category {
|
||||
if (!context.category.children || context.category.children.length <= 0) {
|
||||
throw Error('Category has no children');
|
||||
}
|
||||
const children: ICategoryChildren = {
|
||||
subCategories: new Array<Category>(),
|
||||
subScripts: new Array<Script>(),
|
||||
};
|
||||
for (const categoryOrScript of context.category.children) {
|
||||
parseCategoryChild(categoryOrScript, children, context);
|
||||
}
|
||||
return new Category(
|
||||
/*id*/ categoryIdCounter++,
|
||||
/*name*/ context.category.category,
|
||||
/*docs*/ parseDocUrls(context.category),
|
||||
/*categories*/ children.subCategories,
|
||||
/*scripts*/ children.subScripts,
|
||||
);
|
||||
}
|
||||
|
||||
function parseCategoryChild(
|
||||
categoryOrScript: any, children: ICategoryChildren, parent: IParsingContext) {
|
||||
if (isCategory(categoryOrScript)) {
|
||||
const subCategory = parseCategory(
|
||||
{
|
||||
category: categoryOrScript as YamlCategory,
|
||||
selectedScripts: parent.selectedScripts,
|
||||
});
|
||||
children.subCategories.push(subCategory);
|
||||
} else if (isScript(categoryOrScript)) {
|
||||
const yamlScript = categoryOrScript as YamlScript;
|
||||
const script = new Script(
|
||||
/* name */ yamlScript.name,
|
||||
/* code */ yamlScript.code,
|
||||
/* docs */ parseDocUrls(yamlScript));
|
||||
children.subScripts.push(script);
|
||||
if (yamlScript.default === true) {
|
||||
parent.selectedScripts.push(script);
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Child element is neither a category or a script.
|
||||
Parent: ${parent.category.category}, element: ${categoryOrScript}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function isScript(categoryOrScript: any): boolean {
|
||||
return categoryOrScript.code && categoryOrScript.code.length > 0;
|
||||
}
|
||||
|
||||
function isCategory(categoryOrScript: any): boolean {
|
||||
return categoryOrScript.category && categoryOrScript.category.length > 0;
|
||||
}
|
||||
49
src/application/Parser/DocumentationParser.ts
Normal file
49
src/application/Parser/DocumentationParser.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { YamlDocumentable } from 'js-yaml-loader!./application.yaml';
|
||||
|
||||
export function parseDocUrls(documentable: YamlDocumentable): ReadonlyArray<string> {
|
||||
const docs = documentable.docs;
|
||||
if (!docs) {
|
||||
return [];
|
||||
}
|
||||
const result = new DocumentationUrls();
|
||||
if (docs instanceof Array) {
|
||||
for (const doc of docs) {
|
||||
if (typeof doc !== 'string') {
|
||||
throw new Error('Docs field (documentation url) must be an array of strings');
|
||||
}
|
||||
result.add(doc);
|
||||
}
|
||||
} else if (typeof docs === 'string') {
|
||||
result.add(docs);
|
||||
} else {
|
||||
throw new Error('Docs field (documentation url) must a string or array of strings');
|
||||
}
|
||||
return result.getAll();
|
||||
}
|
||||
|
||||
class DocumentationUrls {
|
||||
private readonly urls = new Array<string>();
|
||||
|
||||
public add(url: string) {
|
||||
validateUrl(url);
|
||||
this.urls.push(url);
|
||||
}
|
||||
|
||||
public getAll(): ReadonlyArray<string> {
|
||||
return this.urls;
|
||||
}
|
||||
}
|
||||
|
||||
function validateUrl(docUrl: string): void {
|
||||
if (!docUrl) {
|
||||
throw new Error('Documentation url is null or empty');
|
||||
}
|
||||
if (docUrl.includes('\n')) {
|
||||
throw new Error('Documentation url cannot be multi-lined.');
|
||||
}
|
||||
const res = docUrl.match(
|
||||
/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
|
||||
if (res == null) {
|
||||
throw new Error(`Invalid documentation url: ${docUrl}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user