code cleanup

This commit is contained in:
Kononnable 2019-10-13 17:20:17 +02:00
parent 5f33127b4a
commit e15efe75e7
7 changed files with 32 additions and 29 deletions

View File

@ -28,7 +28,6 @@ module.exports = {
"no-plusplus": ["error", { allowForLoopAfterthoughts: true }],
"@typescript-eslint/no-non-null-assertion": ["off"],
"@typescript-eslint/no-object-literal-type-assertion": ["off"],
"no-param-reassign": ["off"],
"@typescript-eslint/no-explicit-any": ["off"],

View File

@ -10,6 +10,7 @@ import OracleDriver from "./drivers/OracleDriver";
import SqliteDriver from "./drivers/SqliteDriver";
import modelCustomizationPhase from "./ModelCustomization";
import modelGenerationPhase from "./ModelGeneration";
import { Entity } from "./models/Entity";
export function createDriver(driverName: string): AbstractDriver {
switch (driverName) {
@ -35,7 +36,7 @@ export async function createModelFromDatabase(
driver: AbstractDriver,
connectionOptions: IConnectionOptions,
generationOptions: IGenerationOptions
) {
): Promise<void> {
let dbModel = await dataCollectionPhase(
driver,
connectionOptions,
@ -59,6 +60,6 @@ export async function dataCollectionPhase(
driver: AbstractDriver,
connectionOptions: IConnectionOptions,
generationOptions: IGenerationOptions
) {
): Promise<Entity[]> {
return driver.GetDataFromServer(connectionOptions, generationOptions);
}

View File

@ -9,7 +9,7 @@ export default function modelCustomizationPhase(
dbModel: Entity[],
generationOptions: IGenerationOptions,
defaultValues: DataTypeDefaults
) {
): Entity[] {
let namingStrategy: AbstractNamingStrategy;
if (
generationOptions.customNamingStrategyPath &&
@ -29,7 +29,7 @@ export default function modelCustomizationPhase(
function removeColumnDefaultProperties(
dbModel: Entity[],
defaultValues: DataTypeDefaults
) {
): Entity[] {
if (!defaultValues) {
return dbModel;
}
@ -70,7 +70,7 @@ function removeColumnDefaultProperties(
function addImportsAndGenerationOptions(
dbModel: Entity[],
generationOptions: IGenerationOptions
) {
): Entity[] {
dbModel.forEach(entity => {
entity.relations.forEach(relation => {
if (generationOptions.lazy) {
@ -91,14 +91,14 @@ function addImportsAndGenerationOptions(
function applyNamingStrategy(
namingStrategy: AbstractNamingStrategy,
dbModel: Entity[]
) {
): Entity[] {
let retVal = changeRelationNames(dbModel);
retVal = changeRelationIdNames(retVal);
retVal = changeEntityNames(retVal);
retVal = changeColumnNames(retVal);
return retVal;
function changeRelationIdNames(model: Entity[]) {
function changeRelationIdNames(model: Entity[]): Entity[] {
model.forEach(entity => {
entity.relationIds.forEach(relationId => {
const oldName = relationId.fieldName;
@ -127,7 +127,7 @@ function applyNamingStrategy(
return dbModel;
}
function changeRelationNames(model: Entity[]) {
function changeRelationNames(model: Entity[]): Entity[] {
model.forEach(entity => {
entity.relations.forEach(relation => {
const oldName = relation.fieldName;
@ -166,7 +166,7 @@ function applyNamingStrategy(
return dbModel;
}
function changeColumnNames(model: Entity[]) {
function changeColumnNames(model: Entity[]): Entity[] {
model.forEach(entity => {
entity.columns.forEach(column => {
const oldName = column.tscName;
@ -187,7 +187,7 @@ function applyNamingStrategy(
});
return model;
}
function changeEntityNames(entities: Entity[]) {
function changeEntityNames(entities: Entity[]): Entity[] {
entities.forEach(entity => {
const newName = namingStrategy.entityName(entity.tscName, entity);
entities.forEach(entity2 => {

View File

@ -12,7 +12,7 @@ export default function modelGenerationPhase(
connectionOptions: IConnectionOptions,
generationOptions: IGenerationOptions,
databaseModel: Entity[]
) {
): void {
createHandlebarsHelpers(generationOptions);
const templatePath = path.resolve(__dirname, "templates", "entity.mst");
const template = fs.readFileSync(templatePath, "UTF-8");
@ -63,7 +63,7 @@ export default function modelGenerationPhase(
});
}
function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
function createHandlebarsHelpers(generationOptions: IGenerationOptions): void {
Handlebars.registerHelper("json", context => {
const json = JSON.stringify(context);
const withoutQuotes = json.replace(/"([^(")"]+)":/g, "$1:");
@ -158,7 +158,7 @@ function createHandlebarsHelpers(generationOptions: IGenerationOptions) {
});
}
function createTsConfigFile(outputPath: string) {
function createTsConfigFile(outputPath: string): void {
const templatePath = path.resolve(__dirname, "templates", "tsconfig.mst");
const template = fs.readFileSync(templatePath, "UTF-8");
const compliedTemplate = Handlebars.compile(template, {
@ -175,7 +175,7 @@ function createTsConfigFile(outputPath: string) {
function createTypeOrmConfig(
outputPath: string,
connectionOptions: IConnectionOptions
) {
): void {
const templatePath = path.resolve(__dirname, "templates", "ormconfig.mst");
const template = fs.readFileSync(templatePath, "UTF-8");
const compliedTemplate = Handlebars.compile(template, {

View File

@ -5,10 +5,10 @@ export function LogError(
errText: string,
isABug = true,
passedError?: string | ErrorConstructor
) {
): void {
let errObject = passedError;
console.error(errText);
console.error(`Error occured in typeorm-model-generator.`);
console.error(`Error occurred in typeorm-model-generator.`);
console.error(`${packageVersion()} node@${process.version}`);
console.error(
`If you think this is a bug please open an issue including this log on ${packagejson.bugs.url}`
@ -20,14 +20,14 @@ export function LogError(
console.error(errObject);
}
}
export function packageVersion() {
export function packageVersion(): string {
return `${packagejson.name}@${packagejson.version}`;
}
export function findNameForNewField(
_fieldName: string,
entity: Entity,
columnOldName = ""
) {
): string {
let fieldName = _fieldName;
const validNameCondition = () =>
(entity.columns.every(

View File

@ -2,7 +2,7 @@
class EntityJson {
public entityName: string;
public entityOptions: any = {};
public entityOptions: { [key: string]: string | boolean } = {};
public columns: EntityColumn[] = [] as EntityColumn[];
@ -13,7 +13,7 @@ class EntityColumn {
public columnTypes: string[] = [];
public columnOptions: any = {};
public columnOptions: { [key: string]: string | boolean } = {};
public relationType: "OneToOne" | "OneToMany" | "ManyToOne" | "ManyToMany";
@ -28,7 +28,7 @@ class EntityIndex {
}
export default class EntityFileToJson {
public static getEntityOptions(trimmedLine: string, ent: EntityJson) {
public static getEntityOptions(trimmedLine: string, ent: EntityJson): void {
const decoratorParameters = trimmedLine.slice(
trimmedLine.indexOf("(") + 1,
trimmedLine.lastIndexOf(")")
@ -57,7 +57,7 @@ export default class EntityFileToJson {
public static getColumnOptionsAndType(
trimmedLine: string,
col: EntityColumn
) {
): void {
const decoratorParameters = trimmedLine.slice(
trimmedLine.indexOf("(") + 1,
trimmedLine.lastIndexOf(")")
@ -100,8 +100,8 @@ export default class EntityFileToJson {
} else {
let badJSON = !primaryGeneratedColumn
? decoratorParameters.substring(
decoratorParameters.indexOf(",") + 1
)
decoratorParameters.indexOf(",") + 1
)
: decoratorParameters;
badJSON = badJSON.trim();
if (badJSON.lastIndexOf(",") === badJSON.length - 3) {
@ -117,7 +117,10 @@ export default class EntityFileToJson {
}
}
public static getRelationOptions(trimmedLine: string, col: EntityColumn) {
public static getRelationOptions(
trimmedLine: string,
col: EntityColumn
): void {
const decoratorParameters = trimmedLine.slice(
trimmedLine.indexOf("(") + 1,
trimmedLine.lastIndexOf(")")
@ -146,7 +149,7 @@ export default class EntityFileToJson {
}
}
public static getIndexOptions(trimmedLine: string, ind: EntityIndex) {
public static getIndexOptions(trimmedLine: string, ind: EntityIndex): void {
const decoratorParameters = trimmedLine.slice(
trimmedLine.indexOf("(") + 1,
trimmedLine.lastIndexOf(")")
@ -499,7 +502,7 @@ export default class EntityFileToJson {
return retVal;
}
public static isPartOfMultilineStatement(statement: string) {
public static isPartOfMultilineStatement(statement: string): boolean {
const matchStarting =
statement.split("(").length + statement.split("{").length;
const matchEnding =

View File

@ -324,7 +324,7 @@ export function compileTsFiles(
return compiledWithoutErrors;
}
export function getEnabledDbDrivers() {
export function getEnabledDbDrivers():string[] {
const dbDrivers: string[] = [];
if (process.env.SQLITE_Skip === "0") {
dbDrivers.push("sqlite");