209 lines
6.5 KiB
TypeScript
209 lines
6.5 KiB
TypeScript
import fs from "fs";
|
|
import {
|
|
BelongsToDef,
|
|
ColumnDef,
|
|
ColumnRef,
|
|
EndpointDef,
|
|
Filter,
|
|
ManyToManyDef,
|
|
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);
|
|
}
|
|
|
|
function lowercaseFirstLetter(input: string) {
|
|
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
}
|
|
|
|
function initializeComponentFile(sourceFile: string, destinationFile: string, component: string, outDir: string) {
|
|
return new Promise<void>((resolve, reject) => {
|
|
fs.rename(sourceFile, destinationFile, (err: any) => {
|
|
let fileContents = fs.readFileSync(destinationFile, 'utf8');
|
|
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 removeTemplateFolder(destinationFolder: string) {
|
|
return new Promise<void>((resolve, reject) => {
|
|
fs.rmdir(destinationFolder, { recursive: true }, (err) => {
|
|
if (err) {
|
|
reject(err);
|
|
} else {
|
|
resolve();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function removeTemplateFiles(destinationFolder: string) {
|
|
let regex = /\{\{component\}\}.*$/
|
|
fs.readdirSync(destinationFolder)
|
|
.filter(f => regex.test(f))
|
|
.map(f => fs.unlinkSync(path.join(destinationFolder, f)));
|
|
return Promise.resolve();
|
|
}
|
|
|
|
function insertServiceCode(view: EndpointDef, outDir: string): Promise<void> {
|
|
return new Promise<void>((resolve) => {
|
|
const separator: string = `// SYSTEM-BUILDER-${view.component}-service`;
|
|
let fileLocation = path.join(process.cwd(), outDir, 'src', 'components', view.component, `${view.component}Service.ts`);
|
|
let initServiceFile: string = fs.readFileSync(fileLocation, 'utf8');
|
|
let parts = initServiceFile.split(separator);
|
|
parts[0] = parts[0] + createServiceFunc(view);
|
|
let newServiceFile = parts.join(separator);
|
|
fs.writeFileSync(fileLocation, newServiceFile, 'utf8');
|
|
resolve();
|
|
});
|
|
}
|
|
|
|
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]},
|
|
`;
|
|
}
|
|
});
|
|
// TODO: get relations to add as properties
|
|
let storageTable: TableDef | undefined = systemDef.storage.tables.find((table: TableDef) => {
|
|
return table.name === view.component;
|
|
});
|
|
if (storageTable) {
|
|
storageTable.relations.forEach((rel: BelongsToDef) => {
|
|
out = out + `${rel.table}_id: '',
|
|
`;
|
|
});
|
|
}
|
|
systemDef.storage.relations.forEach((rel:ManyToManyDef) => {
|
|
if (rel.right === view.component) {
|
|
out = out + `${rel.left}_id: '',
|
|
`;
|
|
}
|
|
if (rel.left === view.component) {
|
|
out = out + `${rel.right}_id: '',
|
|
`;
|
|
}
|
|
});
|
|
|
|
return out;
|
|
}
|
|
|
|
function createDetailsTypeValues(view: EndpointDef, systemDef: SystemDef) {
|
|
let out = ``;
|
|
|
|
let storageTable = systemDef.storage.tables.find((table: TableDef) => {
|
|
return table.name === view.component;
|
|
});
|
|
let relations: string[] = [];
|
|
if (storageTable) {
|
|
storageTable.relations.map((rel: BelongsToDef) => {
|
|
relations.push(rel.table);
|
|
});
|
|
systemDef.storage.relations.forEach((rel: ManyToManyDef) => {
|
|
// @ts-ignore
|
|
if (rel.left === storageTable.name) {
|
|
relations.push(rel.right);
|
|
}
|
|
// @ts-ignore
|
|
if (rel.right === storageTable.name) {
|
|
relations.push(rel.left);
|
|
}
|
|
});
|
|
}
|
|
|
|
view.columns.forEach((column: ColumnRef) => {
|
|
let colDef: ColumnDef | undefined = findColumn(column, systemDef);
|
|
if (colDef) {
|
|
out = out + `${colDef.name}: ${TYPE_VALUE[colDef.type]};
|
|
`;
|
|
}
|
|
});
|
|
relations.forEach((rel: string) => {
|
|
out = out + `${rel}_id: string
|
|
`;
|
|
});
|
|
|
|
return out;
|
|
}
|
|
|
|
function componentDestination(systemDefName: string, componentName: string) {
|
|
return path.join(process.cwd(), `frontend-${systemDefName}`, 'src', 'components', componentName);
|
|
}
|
|
|
|
export {
|
|
componentDestination,
|
|
createDetailsInitValues,
|
|
createDetailsTypeValues,
|
|
findColumn,
|
|
initializeComponentFile,
|
|
insertServiceCode,
|
|
lowercaseFirstLetter,
|
|
makeSet,
|
|
removeTemplateFiles,
|
|
removeTemplateFolder,
|
|
uppercaseFirstLetter,
|
|
} |