add frontend frame and service logic
This commit is contained in:
94
src/frontend-services/fe-service-creator.ts
Normal file
94
src/frontend-services/fe-service-creator.ts
Normal file
@ -0,0 +1,94 @@
|
||||
import {ComponentDef, EndpointDef, SystemDef} from "../systemGenService";
|
||||
import path from "path";
|
||||
import {initializeComponentFile} from "../helpers";
|
||||
import fs from "fs";
|
||||
import {buildServiceFunctionName, getFuncParams} from "../views/service-creator";
|
||||
import {METHOD, URL} from "../views/routes-creator";
|
||||
const ncp = require('ncp').ncp;
|
||||
|
||||
export function createFrontend(systemDef: SystemDef) {
|
||||
let fePromises = [];
|
||||
for (let i in systemDef.components) {
|
||||
fePromises.push(createComponent(systemDef.components[i], systemDef));
|
||||
// TODO: add a vue view and add it to App.vue
|
||||
}
|
||||
return Promise.all(fePromises);
|
||||
}
|
||||
|
||||
function componentSource() {
|
||||
return path.join(process.cwd(), 'frontend-frame', 'src', 'components', '{{component}}');
|
||||
}
|
||||
|
||||
function componentDestination(systemDefName: string, componentName: string) {
|
||||
return path.join(process.cwd(), `frontend-${systemDefName}`, 'src', 'components', componentName);
|
||||
}
|
||||
|
||||
function createComponent(component: ComponentDef, systemDef: SystemDef) {
|
||||
return new Promise<void>((componentResolve, componentReject) => {
|
||||
ncp(componentSource(), componentDestination(systemDef.name, component.component), (err: any) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
componentReject();
|
||||
return;
|
||||
} else {
|
||||
let componentDest: string = componentDestination(systemDef.name, component.component);
|
||||
let servicePromise = initializeComponentFile(path.join(componentDest, '{{component}}Service.ets'), path.join(componentDest, `${component.component}Service.ts`), component.component, systemDef.name);
|
||||
for (let i in component.endpoints) {
|
||||
servicePromise = servicePromise.then(() => {
|
||||
return insertFEServiceCode(component.endpoints[i], componentDest);
|
||||
});
|
||||
// TODO: use templates for Details, Editor, List and Types view based on the endpoints provided
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function insertFEServiceCode(view: EndpointDef, outDir: string) {
|
||||
return new Promise<void>((resolve) => {
|
||||
const separator: string = `// SYSTEM-BUILDER-service-functions`;
|
||||
let fileLocation = path.join(outDir, `${view.component}Service.ts`)
|
||||
let initServiceFile: string = fs.readFileSync(fileLocation, 'utf8');
|
||||
let parts = initServiceFile.split(separator);
|
||||
parts[0] = parts[0] + createFEServiceFunc(view);
|
||||
let newServiceFile = parts.join(separator);
|
||||
fs.writeFileSync(fileLocation, newServiceFile, 'utf8');
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
|
||||
function createFEServiceFunc(view: EndpointDef): string {
|
||||
let func: string = '';
|
||||
let isListOrSearch: boolean = view.type === 'list' || view.type === 'search';
|
||||
let funcName: string = buildServiceFunctionName(view);
|
||||
let funcParams: string[] = getFuncParams(view, true);
|
||||
let columnParams: string[] = view.columns?.filter(column => column.param).map(column => { return '\n ' + column.param + ': ' + column.param; });
|
||||
let filterParams: string[] = view.filters?.filter(filter => filter.param).map(filter => { return '\n ' + filter.param + ': ' + filter.param; }) || [];
|
||||
|
||||
if (isListOrSearch) {
|
||||
funcParams.push('offset: number');
|
||||
funcParams.push('limit: number');
|
||||
filterParams.push('\n offset: offset');
|
||||
filterParams.push('\n limit: limit');
|
||||
}
|
||||
|
||||
let isGetOrDelete = view.type === 'item' || view.type === 'delete';
|
||||
let method = METHOD[view.type];
|
||||
if (method === 'delete') {
|
||||
method = 'del';
|
||||
}
|
||||
|
||||
func = `
|
||||
public ${funcName}(${funcParams.join(', ')}) {`;
|
||||
|
||||
if (!isGetOrDelete) {
|
||||
func = func + `
|
||||
let params = {${columnParams}${columnParams.length ? ',' : ''}${filterParams}
|
||||
};`;
|
||||
}
|
||||
func = func + `
|
||||
return ${method}('/api/v1/${URL[view.type](view)}${isGetOrDelete ? '/\' + ' + funcParams[0].split(":")[0] : '\', params'});
|
||||
}
|
||||
`;
|
||||
return func;
|
||||
}
|
Reference in New Issue
Block a user