From 3ca6707a804c811378fc6e7c7bb0f5ca394bc0fe Mon Sep 17 00:00:00 2001 From: Kononnable Date: Wed, 24 May 2017 21:06:50 +0200 Subject: [PATCH] changed from mustache.js to handlebars.js --- package.json | 3 ++- src/Engine.ts | 8 +++----- src/entity.mst | 11 ++++++++++- src/index.ts | 3 --- test/utils/EntityFileToJson.ts | 8 ++++---- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 209a4f2..21f0a03 100644 --- a/package.json +++ b/package.json @@ -22,14 +22,15 @@ }, "homepage": "https://github.com/Kononnable/typeorm-model-generator#readme", "dependencies": { + "handlebars": "^4.0.10", "mssql": "^3.3.0", - "mustache": "^2.3.0", "reflect-metadata": "^0.1.10", "typeorm": "0.0.10", "typescript": "^2.3.2", "yargs": "^7.0.2" }, "devDependencies": { + "@types/handlebars": "^4.0.32", "@types/chai": "^3.5.2", "@types/chai-as-promised": "0.0.30", "@types/chai-subset": "^1.3.0", diff --git a/src/Engine.ts b/src/Engine.ts index 76b2c53..225e9ba 100644 --- a/src/Engine.ts +++ b/src/Engine.ts @@ -1,7 +1,7 @@ import { MssqlDriver } from './drivers/MssqlDriver' import { AbstractDriver } from "./drivers/AbstractDriver"; import { DatabaseModel } from './models/DatabaseModel' -import * as Mustache from 'mustache' +import * as Handlebars from 'handlebars' import fs = require('fs'); import path = require('path') /** @@ -32,15 +32,13 @@ export class Engine { fs.mkdirSync(resultPath); this.createTsConfigFile(resultPath) this.createTypeOrm(resultPath) - Mustache.escape = function (value) { - return value; - }; let entitesPath = path.resolve(resultPath, './entities') if (!fs.existsSync(entitesPath)) fs.mkdirSync(entitesPath); + let compliedTemplate = Handlebars.compile(template,{noEscape:true}) databaseModel.entities.forEach(element => { let resultFilePath = path.resolve(entitesPath, element.EntityName + '.ts'); - let rendered = Mustache.render(template, element); + let rendered =compliedTemplate(element) fs.writeFileSync(resultFilePath, rendered, { encoding: 'UTF-8', flag: 'w' }) }); } diff --git a/src/entity.mst b/src/entity.mst index 669bc34..a044d80 100644 --- a/src/entity.mst +++ b/src/entity.mst @@ -7,7 +7,16 @@ import {Index,Entity, PrimaryColumn, Column, OneToOne, OneToMany, ManyToOne, Joi {{#Columns}} - @Column("{{sql_type}}",{ {{#is_generated}}generated:true,{{/is_generated}}{{#is_nullable}}nullable:true,{{/is_nullable}}{{^is_nullable}}nullable:false,{{/is_nullable}}{{#char_max_lenght}}length:{{char_max_lenght}},{{/char_max_lenght}}{{#default}}default:"{{default}}",{{/default}}{{#numericPrecision}}precision:{{numericPrecision}},{{/numericPrecision}}{{#numericScale}}scale:{{numericScale}},{{/numericScale}}{{#isPrimary}}primary:{{isPrimary}},{{/isPrimary}}}){{#relations}} + @Column("{{sql_type}}",{ {{#is_generated}} + generated:true,{{/is_generated}}{{#is_nullable}} + nullable:true,{{/is_nullable}}{{^is_nullable}} + nullable:false,{{/is_nullable}}{{#char_max_lenght}} + length:{{.}},{{/char_max_lenght}}{{#default}} + default:"{{default}}",{{/default}}{{#numericPrecision}} + precision:{{numericPrecision}},{{/numericPrecision}}{{#numericScale}} + scale:{{numericScale}},{{/numericScale}}{{#isPrimary}} + primary:{{isPrimary}},{{/isPrimary}} + }){{#relations}} @{{relationType}}(type=>{{relatedTable}},x=>x.{{relatedColumn}}){{#isOwner}} @JoinTable(){{/isOwner}}{{/relations}} {{name}}:{{ts_type}}; diff --git a/src/index.ts b/src/index.ts index a11cd0f..0ff2378 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,8 @@ import { MssqlDriver } from './drivers/MssqlDriver'; -import * as Mustache from 'mustache' import { Engine } from './Engine' import * as Yargs from 'yargs' import { AbstractDriver } from "./drivers/AbstractDriver"; import path = require('path') -// var x = Mustache.render("{{a}}", { a: 'test' }); -// console.log(x); diff --git a/test/utils/EntityFileToJson.ts b/test/utils/EntityFileToJson.ts index 4a07685..8a522d7 100644 --- a/test/utils/EntityFileToJson.ts +++ b/test/utils/EntityFileToJson.ts @@ -6,8 +6,8 @@ export class EntityFileToJson { if (decoratorParameters.search(',') > 0) { col.columnType = decoratorParameters.substring(0, decoratorParameters.indexOf(',')).trim() let badJSON = decoratorParameters.substring(decoratorParameters.indexOf(',') + 1).trim() - if (badJSON.lastIndexOf(',') == badJSON.length - 2) { - badJSON = badJSON.slice(0, badJSON.length - 2) + badJSON[badJSON.length - 1] + if (badJSON.lastIndexOf(',') == badJSON.length - 3) { + badJSON = badJSON.slice(0, badJSON.length - 3) + badJSON[badJSON.length - 2] + badJSON[badJSON.length - 1] } col.columnOptions = JSON.parse(badJSON.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')) } else { @@ -15,8 +15,8 @@ export class EntityFileToJson { col.columnType = decoratorParameters } else { let badJSON = decoratorParameters.substring(decoratorParameters.indexOf(',') + 1).trim() - if (badJSON.lastIndexOf(',') == badJSON.length - 2) { - badJSON = badJSON.slice(0, badJSON.length - 2) + badJSON[badJSON.length - 1] + if (badJSON.lastIndexOf(',') == badJSON.length - 3) { + badJSON = badJSON.slice(0, badJSON.length - 3) + badJSON[badJSON.length - 2] + badJSON[badJSON.length - 1] } col.columnOptions = JSON.parse(badJSON.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')) }