working frontend build

This commit is contained in:
2021-07-07 17:12:04 -05:00
parent be789ae882
commit 9869125965
11 changed files with 621 additions and 41 deletions

View File

@ -1,7 +1,26 @@
import fs from "fs";
import {EndpointDef} from "./systemGenService";
import {ColumnDef, ColumnRef, EndpointDef, Filter, SystemDef, TableDef} from "./systemGenService";
import path from "path";
import {createServiceFunc} from "./views/service-creator";
import pluralize from "pluralize";
const INIT_VALUE = {
"blob": "''",
"boolean": "false",
"date": "''",
"dateTIME": "''",
"number": "0",
"string": "''"
};
const TYPE_VALUE = {
"blob": "string",
"boolean": "boolean",
"date": "string",
"dateTIME": "string",
"number": "number",
"string": "string",
};
function uppercaseFirstLetter(input: string) {
return input.charAt(0).toUpperCase() + input.slice(1);
@ -15,16 +34,32 @@ function initializeComponentFile(sourceFile: string, destinationFile: string, co
return new Promise<void>((resolve, reject) => {
fs.rename(sourceFile, destinationFile, (err: any) => {
let fileContents = fs.readFileSync(destinationFile, 'utf8');
var uppercaseFirstLetterComponentName = uppercaseFirstLetter(component);
var lowercaseFirstLetterComponentName = lowercaseFirstLetter(component);
let uppercaseFirstLetterComponentName = uppercaseFirstLetter(component);
let lowercaseFirstLetterComponentName = lowercaseFirstLetter(component);
let lcPluralComponentName = lowercaseFirstLetter(pluralize(component));
let ucPluralComponentName = uppercaseFirstLetter(pluralize(component));
let newFileContents = fileContents.split('{{Component}}').join(uppercaseFirstLetterComponentName);
newFileContents = newFileContents.split('{{component}}').join(lowercaseFirstLetterComponentName);
newFileContents = newFileContents.split('{{components}}').join(lcPluralComponentName);
newFileContents = newFileContents.split('{{Components}}').join(ucPluralComponentName);
fs.writeFileSync(destinationFile, newFileContents, 'utf8');
resolve();
});
})
}
function removeTemplateFiles(destinationFolder: string) {
return new Promise<void>((resolve, reject) => {
fs.rmdir(destinationFolder, (err) => {
if (err) {
reject();
} else {
resolve();
}
});
});
}
function insertServiceCode(view: EndpointDef, outDir: string): Promise<void> {
return new Promise<void>((resolve) => {
const separator: string = `// SYSTEM-BUILDER-${view.component}-service`;
@ -38,9 +73,70 @@ function insertServiceCode(view: EndpointDef, outDir: string): Promise<void> {
});
}
function findColumn(col: ColumnRef, systemDef: SystemDef): ColumnDef | undefined {
let colDef: ColumnDef | undefined;
let table: TableDef | undefined = systemDef.storage.tables.find((td: TableDef) => {
return td.name === col.table;
});
if (table) {
colDef = table.columns.find((cd: ColumnDef) => {
return cd.name === col.name;
});
}
return colDef;
}
function makeSet<T extends {[id: string]: string}>(list: T[], id: string): T[] {
let typeObject: {[key: string]: T} = {};
for (let i in list) {
if (!typeObject[list[i][id]]) {
typeObject[list[i][id]] = list[i];
}
}
let typeSet: T[] = [];
for (let key in typeObject) {
typeSet.push(typeObject[key]);
}
return typeSet;
}
function createDetailsInitValues(view: EndpointDef, systemDef: SystemDef) {
let out = `${view.component}_id: '',
`;
view.columns.forEach((column: ColumnRef) => {
let colDef: ColumnDef | undefined = findColumn(column, systemDef);
if (colDef) {
out = out + `${colDef.name}: ${INIT_VALUE[colDef.type]},
`;
}
});
return out;
}
function createDetailsTypeValues(view: EndpointDef, systemDef: SystemDef) {
let out = ``;
view.columns.forEach((column: ColumnRef) => {
let colDef: ColumnDef | undefined = findColumn(column, systemDef);
if (colDef) {
out = out + `${colDef.name}: ${TYPE_VALUE[colDef.type]};
`;
}
});
return out;
}
export {
uppercaseFirstLetter,
lowercaseFirstLetter,
createDetailsInitValues,
createDetailsTypeValues,
findColumn,
initializeComponentFile,
insertServiceCode
insertServiceCode,
lowercaseFirstLetter,
makeSet,
removeTemplateFiles,
uppercaseFirstLetter,
}