Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Add express-model command
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Lopes committed May 30, 2018
1 parent 3ec08c0 commit 51b95c1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions commands/express-add-model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('shelljs/global')
const rederer = require('../libs/renderer.js')

module.exports = function initCommand(program) {
program
.command('express-model')
.description('Generate express model')
.action((command) => {
rederer.renderModel()
})
}
20 changes: 20 additions & 0 deletions libs/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,25 @@ module.exports = {
log(chalk.yellow(e))
exit(1)
}
},
renderModel: () => {
try {
let template = fs.readFileSync('./templates/express/model.ejs', 'utf-8')
let js = ejs.render(template, {
data:{}
})
let targetDir = 'app/models'
let filename = 'Post.js'

dir.mkDirByPathSync(targetDir)

fs.writeFileSync('./' + targetDir + '/' + filename, js, 'utf8')

log(chalk.green.bold('Express Model Generated !'))
} catch (e) {
log(chalk.red.bold('Error while generating Express Model'))
log(chalk.yellow(e))
exit(1)
}
}
}
18 changes: 18 additions & 0 deletions templates/express/model.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require('mongoose')

module.exports = () => {
const schema = mongoose.Schema({
title: {
type: String,
required: true
},
body: {
type: String,
required: true
}
}, {
timestamps: true
})

return mongoose.model('Post', schema)
}

0 comments on commit 51b95c1

Please sign in to comment.