fixing code //TODO issues
This commit is contained in:
parent
9762f37ab5
commit
e9e9ed8805
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,4 +5,4 @@ ormconfig.json
|
||||
typings/
|
||||
**/*.js
|
||||
**/*.js.map
|
||||
results/*.*
|
||||
output/*.*
|
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
||||
});
|
||||
|
13
src/index.ts
13
src/index.ts
@ -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.`)
|
||||
})
|
||||
})
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user