fixing code //TODO issues

This commit is contained in:
Kononnable 2017-04-29 15:56:54 +02:00
parent 9762f37ab5
commit e9e9ed8805
4 changed files with 53 additions and 18 deletions

2
.gitignore vendored
View File

@ -5,4 +5,4 @@ ormconfig.json
typings/
**/*.js
**/*.js.map
results/*.*
output/*.*

View File

@ -27,9 +27,24 @@ export class Engine {
private createModelFromMetadata(databaseModel: DatabaseModel) {
let templatePath = path.resolve(__dirname, 'entity.mst')
let template = fs.readFileSync(templatePath, 'UTF-8');
//TODO:get results path to argvs, check if dir exists before
let resultPath = path.resolve(__dirname, '../results')
//TODO:Refactor to new method
let resultPath = path.resolve(__dirname, '../'+this.Options.resultsPath)
if (!fs.existsSync(resultPath))
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);
databaseModel.entities.forEach(element => {
let resultFilePath = path.resolve(entitesPath, element.EntityName + '.ts');
let rendered = Mustache.render(template, element);
fs.writeFileSync(resultFilePath, rendered, { encoding: 'UTF-8', flag: 'w' })
});
}
private createTsConfigFile(resultPath) {
fs.writeFileSync(path.resolve(resultPath, 'tsconfig.json'), `{"compilerOptions": {
"lib": ["es5", "es6"],
"target": "es6",
@ -39,15 +54,24 @@ export class Engine {
"experimentalDecorators": true,
"sourceMap": true
}}`, { encoding: 'UTF-8', flag: 'w' });
//TODO:Create ormconfig file
Mustache.escape = function (value) {
return value;
};
databaseModel.entities.forEach(element => {
let resultFilePath = path.resolve(resultPath, element.EntityName + '.ts');
let rendered = Mustache.render(template, element);
fs.writeFileSync(resultFilePath, rendered, { encoding: 'UTF-8', flag: 'w' })
});
}
private createTypeOrm(resultPath) {
fs.writeFileSync(path.resolve(resultPath, 'ormconfig.json'), `[
{
"name": "default",
"driver": {
"type": "${this.Options.databaseType}",
"host": "${this.Options.host}",
"port": ${this.Options.port},
"username": "${this.Options.user}",
"password": "${this.Options.password}",
"database": "${this.Options.databaseName}"
},
"entities": [
"entities/*.js"
]
}
]`, { encoding: 'UTF-8', flag: 'w' });
}
}
export interface EngineOptions {
@ -55,5 +79,7 @@ export interface EngineOptions {
port: number,
databaseName: string,
user: string,
password: string
password: string,
resultsPath: string,
databaseType: string
}

View File

@ -325,7 +325,9 @@ order by
resolve(true)
}
else {
//TODO:Report errors
console.error('Error connecting to MSSQL Server.')
console.error(err)
process.abort()
reject(err)
}
});

View File

@ -40,6 +40,11 @@ var argv = Yargs
choices: ['mssql'],
default: 'mssql'
})
.option('o', {
alias: 'output',
describe: 'Where to place generated models.',
default: './output'
})
.argv;
@ -62,11 +67,13 @@ let engine = new Engine(
port: parseInt(argv.p) || standardPort,
databaseName: argv.d,
user: argv.u,
password: argv.x
password: argv.x,
databaseType:argv.e,
resultsPath:argv.o
});
console.log(`[${new Date().toLocaleTimeString()}] Starting creation of model classes.`);
engine.createModelFromDatabase().then( ()=>{
// process.abort();
console.info(`[${new Date().toLocaleTimeString()}] Typeorm model classes created.`)
})
})