Skip to content

Commit

Permalink
code cleanup, introducing tslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Kononnable committed Dec 8, 2018
1 parent 69a2362 commit a4592ff
Show file tree
Hide file tree
Showing 26 changed files with 818 additions and 631 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ codecov.yml
tsconfig.json
typings.json
dist/test/
src/tslint.json
117 changes: 117 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"remap-istanbul": "^0.12.0",
"rimraf": "^2.6.2",
"sinon": "^6.0.0",
"sinon-chai": "^3.0.0"
"sinon-chai": "^3.0.0",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.17.0"
}
}
8 changes: 4 additions & 4 deletions src/AbstractNamingStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { RelationInfo } from "./models/RelationInfo";
import { DatabaseModel } from "./models/DatabaseModel";
import { RelationInfo } from "./models/RelationInfo";

export abstract class AbstractNamingStrategy {
abstract relationName(
public abstract relationName(
columnName: string,
relation: RelationInfo,
dbModel: DatabaseModel
): string;

abstract entityName(entityName: string): string;
public abstract entityName(entityName: string): string;

abstract columnName(columnName: string): string;
public abstract columnName(columnName: string): string;
}
43 changes: 26 additions & 17 deletions src/Engine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { AbstractDriver } from "./drivers/AbstractDriver";
import { DatabaseModel } from "./models/DatabaseModel";
import * as Handlebars from "handlebars";
import changeCase = require("change-case");
import fs = require("fs");
import * as Handlebars from "handlebars";
import path = require("path");
import * as TomgUtils from "./Utils";
import changeCase = require("change-case");
import { AbstractNamingStrategy } from "./AbstractNamingStrategy";
import { AbstractDriver } from "./drivers/AbstractDriver";
import { DatabaseModel } from "./models/DatabaseModel";
import * as TomgUtils from "./Utils";

export class Engine {
constructor(
Expand All @@ -14,7 +14,7 @@ export class Engine {
) {}

public async createModelFromDatabase(): Promise<boolean> {
let dbModel = await this.getEntitiesInfo(
const dbModel = await this.getEntitiesInfo(
this.Options.databaseName,
this.Options.host,
this.Options.port,
Expand Down Expand Up @@ -60,18 +60,24 @@ export class Engine {
}
private createModelFromMetadata(databaseModel: DatabaseModel) {
this.createHandlebarsHelpers();
let templatePath = path.resolve(__dirname, "../../src/entity.mst");
let template = fs.readFileSync(templatePath, "UTF-8");
let resultPath = this.Options.resultsPath;
if (!fs.existsSync(resultPath)) fs.mkdirSync(resultPath);
const templatePath = path.resolve(__dirname, "../../src/entity.mst");
const template = fs.readFileSync(templatePath, "UTF-8");
const resultPath = this.Options.resultsPath;
if (!fs.existsSync(resultPath)) {
fs.mkdirSync(resultPath);
}
let entitesPath = resultPath;
if (!this.Options.noConfigs) {
this.createTsConfigFile(resultPath);
this.createTypeOrmConfig(resultPath);
entitesPath = path.resolve(resultPath, "./entities");
if (!fs.existsSync(entitesPath)) fs.mkdirSync(entitesPath);
if (!fs.existsSync(entitesPath)) {
fs.mkdirSync(entitesPath);
}
}
let compliedTemplate = Handlebars.compile(template, { noEscape: true });
const compliedTemplate = Handlebars.compile(template, {
noEscape: true
});
databaseModel.entities.forEach(element => {
element.Imports = [];
element.Columns.forEach(column => {
Expand Down Expand Up @@ -100,11 +106,11 @@ export class Engine {
casedFileName = element.EntityName;
break;
}
let resultFilePath = path.resolve(
const resultFilePath = path.resolve(
entitesPath,
casedFileName + ".ts"
);
let rendered = compliedTemplate(element);
const rendered = compliedTemplate(element);
fs.writeFileSync(resultFilePath, rendered, {
encoding: "UTF-8",
flag: "w"
Expand Down Expand Up @@ -173,8 +179,11 @@ export class Engine {
});
Handlebars.registerHelper("toLowerCase", str => str.toLowerCase());
Handlebars.registerHelper("toLazy", str => {
if (this.Options.lazy) return `Promise<${str}>`;
else return str;
if (this.Options.lazy) {
return `Promise<${str}>`;
} else {
return str;
}
});
Handlebars.registerHelper({
eq: (v1, v2) => v1 === v2,
Expand All @@ -188,7 +197,7 @@ export class Engine {
});
}

//TODO:Move to mustache template file
// TODO:Move to mustache template file
private createTsConfigFile(resultPath) {
fs.writeFileSync(
path.resolve(resultPath, "tsconfig.json"),
Expand Down
15 changes: 8 additions & 7 deletions src/NamingStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { AbstractNamingStrategy } from "./AbstractNamingStrategy";
import { RelationInfo } from "./models/RelationInfo";
import { DatabaseModel } from "./models/DatabaseModel";
import { RelationInfo } from "./models/RelationInfo";

export class NamingStrategy extends AbstractNamingStrategy {
relationName(
public relationName(
columnOldName: string,
relation: RelationInfo,
dbModel: DatabaseModel
): string {
let isRelationToMany = relation.isOneToMany || relation.isManyToMany;
let ownerEntity = dbModel.entities.find(
const isRelationToMany = relation.isOneToMany || relation.isManyToMany;
const ownerEntity = dbModel.entities.find(
v => v.EntityName == relation.ownerTable
)!;

Expand Down Expand Up @@ -51,20 +51,21 @@ export class NamingStrategy extends AbstractNamingStrategy {
v.tsName != columnName ||
columnName == columnOldName
)
)
) {
break;
}
}
}
}

return columnName;
}

entityName(entityName: string): string {
public entityName(entityName: string): string {
return entityName;
}

columnName(columnName: string): string {
public columnName(columnName: string): string {
return columnName;
}
}
12 changes: 8 additions & 4 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ export function LogError(
console.error(`${packageVersion()} node@${process.version}`);
console.error(
`If you think this is a bug please open an issue including this log on ${
(<any>packagejson).bugs.url
(packagejson as any).bugs.url
}`
);
if (isABug && !errObject) errObject = new Error().stack;
if (!!errObject) console.error(errObject);
if (isABug && !errObject) {
errObject = new Error().stack;
}
if (!!errObject) {
console.error(errObject);
}
}
export function packageVersion() {
return `${(<any>packagejson).name}@${(<any>packagejson).version}`;
return `${(packagejson as any).name}@${(packagejson as any).version}`;
}
Loading

0 comments on commit a4592ff

Please sign in to comment.