Skip to content

Commit

Permalink
update the cli for promises and factor out questions
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenPioneer committed Jun 11, 2016
1 parent 22ea2c7 commit 845c02e
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 130 deletions.
151 changes: 21 additions & 130 deletions commands/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var chalk = require('chalk')
var _ = require('lodash')
var mongoose = require('mongoose')
var bcrypt = require('bcrypt-nodejs')
var questions = require('./questions.js')
var settings = require('../configs/settings.js').get()
var fs = require('fs')
var path = require('path')
Expand Down Expand Up @@ -85,116 +86,6 @@ userSchema.pre('save', function (next) {
})
var User = mongoose.model('User', userSchema)

var introQuestions = [
{
type: 'list',
name: 'intro',
message: 'What do you want to do?',
choices: [

new inquirer.Separator('Module Creation:'),
'Create Schema',
'Create Frontend Module',
'Create Backend Module',
'Create Frontend & Backend Module',

new inquirer.Separator('User Management:'),
// 'Create User',
'Change Password',
'Change User Roles',
'View User',
'Exit'
]
}
]
var rolesQuestions = [
{
type: 'list',
name: 'role',
message: 'What do you want to do with user roles?',
choices: [
'Add Role',
'Remove Role'
]
}
]
var userQuestions = [
{
type: 'input',
name: 'email',
message: 'Email of the User',
default: function () { return '[email protected]' }
}
]
var moduleQuestions = [
{
type: 'input',
name: 'module',
message: 'Name of the Module',
default: function () { return 'example' }
}
]
var schemaOutput = []

var schemaPreQuestion = [
{
type: 'confirm',
name: 'askAgain',
message: 'Do you want to custom your schema (just hit enter for YES)?',
default: true
}
]
var schemaQuestions = [
{
type: 'input',
name: 'field',
message: 'Name of the field',
default: function () { return 'Example' }
},
{
type: 'list',
name: 'type',
message: "What's the type",
choices: [
'String',
'Number',
'Date',
'Buffer',
'Boolean',
'Mixed',
'Objectid',
'Array'
],
default: function () { return 'String' }
},
{
type: 'input',
name: 'default',
message: "What's the default value(just hit enter for NULL)",
default: function () { return null }
},
{
type: 'confirm',
name: 'askAgain',
message: 'Want to add another field (just hit enter for YES)?',
default: true
}
]
var updatePasswords = [
{
type: 'password',
name: 'password',
message: 'New Password of the User'
}
]
var updateRoles = [
{
type: 'input',
name: 'role',
message: 'Name of the Role'
}
]

function emptyDirectory (url, callback) {
fs.readdir('./' + url, function (err, files) {
if (err && err.code !== 'ENOENT') throw new Error(err)
Expand Down Expand Up @@ -302,8 +193,8 @@ function buildBack (data, cb) {
}

function moreSchemaQuestioning (cb) {
inquirer.prompt(schemaQuestions, function (answers) {
schemaOutput.push(answers)
inquirer.prompt(questions.schema).then(function (answers) {
questions.schemaOutput.push(answers)
if (answers.askAgain) {
moreSchemaQuestioning(cb)
} else {
Expand All @@ -314,15 +205,15 @@ var __name__Schema = mongoose.Schema({
*/
})

_.forEach(schemaOutput, function (n, k) {
_.forEach(questions.schemaOutput, function (n, k) {
n.default = n.default || '""'
schema[_.camelCase(n.field)] = {
field: _.camelCase(n.field),
type: n.type,
default: n.default,
string: _.camelCase(n.field) + ':{type:' + n.type + ',default:' + n.default + '}'
}
if (schemaOutput.length === k + 1) {
if (questions.schemaOutput.length === k + 1) {
modelFile += '\n' + _.camelCase(n.field) + ':{ \n type:' + n.type + ', \n default:' + n.default + '\n} \n'
} else {
modelFile += '\n' + _.camelCase(n.field) + ':{ \n type:' + n.type + ', \n default:' + n.default + '\n},'
Expand All @@ -341,15 +232,15 @@ module.exports = {
schema: schema,
created: true
})
schemaOutput = []
questions.schemaOutput = []
}
})
}
function buildSchema (cb) {
inquirer.prompt(schemaPreQuestion, function (answers) {
inquirer.prompt(questions.schemaPreQuestion).then(function (answers) {
if (answers.askAgain) {
inquirer.prompt(schemaQuestions, function (answers) {
schemaOutput.push(answers)
inquirer.prompt(questions.schema).then(function (answers) {
questions.schemaOutput.push(answers)
if (answers.askAgain) {
moreSchemaQuestioning(cb)
} else {
Expand All @@ -360,15 +251,15 @@ var __name__Schema = mongoose.Schema({
*/
})

_.forEach(schemaOutput, function (n, k) {
_.forEach(questions.schemaOutput, function (n, k) {
n.default = n.default || '""'
schema[_.camelCase(n.field)] = {
field: _.camelCase(n.field),
type: n.type,
default: n.default,
string: _.camelCase(n.field) + ':{type:' + n.type + ',default:' + n.default + '}'
}
if (schemaOutput.length === k + 1) {
if (questions.schemaOutput.length === k + 1) {
modelFile += '\n' + _.camelCase(n.field) + ':{ \n type:' + n.type + ', \n default:' + n.default + '\n} \n'
} else {
modelFile += '\n' + _.camelCase(n.field) + ':{ \n type:' + n.type + ', \n default:' + n.default + '\n},'
Expand All @@ -387,7 +278,7 @@ module.exports = {
schema: schema,
created: true
})
schemaOutput = []
questions.schemaOutput = []
}
})
} else {
Expand All @@ -401,7 +292,7 @@ module.exports = {
}

function findUser (cb) {
inquirer.prompt(userQuestions, function (answers) {
inquirer.prompt(questions.user).then(function (answers) {
User.findOne({ email: answers.email }, function (err, existingUser) {
cb(err, existingUser)
})
Expand All @@ -426,7 +317,7 @@ function updateUser (answers, cb) {
})
}
function updatePassword (user, cb) {
inquirer.prompt(updatePasswords, function (answers) {
inquirer.prompt(questions.updatePasswords).then(function (answers) {
updateUser({
_id: user._id,
user: {
Expand All @@ -442,7 +333,7 @@ function updatePassword (user, cb) {
}

function addRoles (user, cb) {
inquirer.prompt(updateRoles, function (answers) {
inquirer.prompt(questions.updateRoles).then(function (answers) {
user.roles.push(answers.role)
user.roles = _.uniq(user.roles)
updateUser({
Expand All @@ -459,7 +350,7 @@ function addRoles (user, cb) {
})
}
function removeRoles (user, cb) {
inquirer.prompt(updateRoles, function (answers) {
inquirer.prompt(questions.updateRoles).then(function (answers) {
_.forEach(user.roles, function (n, i) {
if (n === answers.role) {
user.roles.splice(i, 1)
Expand All @@ -479,10 +370,10 @@ function removeRoles (user, cb) {
})
}
function ask () {
inquirer.prompt(introQuestions, function (answers) {
inquirer.prompt(questions.intro).then(function (answers) {
switch (answers.intro) {
case 'Create Frontend Module':
inquirer.prompt(moduleQuestions, function (modules) {
inquirer.prompt(questions.module).then(function (modules) {
buildFront({
name: modules.module
}, function (err) {
Expand All @@ -492,7 +383,7 @@ function ask () {
})
break
case 'Create Backend Module':
inquirer.prompt(moduleQuestions, function (modules) {
inquirer.prompt(questions.module).then(function (modules) {
buildSchema(function (data) {
buildBack({
name: modules.module,
Expand All @@ -506,7 +397,7 @@ function ask () {

break
case 'Create Frontend & Backend Module':
inquirer.prompt(moduleQuestions, function (modules) {
inquirer.prompt(questions.module).then(function (modules) {
buildSchema(function (data) {
buildFront({
name: modules.module
Expand Down Expand Up @@ -586,7 +477,7 @@ function ask () {
console.log(chalk.red('No User Found Under That Email'))
ask()
} else {
inquirer.prompt(rolesQuestions, function (answers) {
inquirer.prompt(questions.roles, function (answers) {
if (answers.role === 'Add Role') {
addRoles(user, function (err, data) {
if (err) {
Expand Down
111 changes: 111 additions & 0 deletions commands/questions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
var inquirer = require('inquirer')
module.exports = {
intro: [
{
type: 'list',
name: 'intro',
message: 'What do you want to do?',
choices: [

new inquirer.Separator('Module Creation:'),
'Create Schema',
'Create Frontend Module',
'Create Backend Module',
'Create Frontend & Backend Module',

new inquirer.Separator('User Management:'),
// 'Create User',
'Change Password',
'Change User Roles',
'View User',
'Exit'
]
}
],
roles: [
{
type: 'list',
name: 'role',
message: 'What do you want to do with user roles?',
choices: [
'Add Role',
'Remove Role'
]
}
],
user: [
{
type: 'input',
name: 'email',
message: 'Email of the User',
default: function () { return '[email protected]' }
}
],
module: [
{
type: 'input',
name: 'module',
message: 'Name of the Module',
default: function () { return 'example' }
}
],
schemaOutput: [],
schemaPreQuestion: [
{
type: 'confirm',
name: 'askAgain',
message: 'Do you want to custom your schema (just hit enter for YES)?',
default: true
}
],
schema: [
{
type: 'input',
name: 'field',
message: 'Name of the field',
default: function () { return 'Example' }
},
{
type: 'list',
name: 'type',
message: "What's the type",
choices: [
'String',
'Number',
'Date',
'Buffer',
'Boolean',
'Mixed',
'Objectid',
'Array'
],
default: function () { return 'String' }
},
{
type: 'input',
name: 'default',
message: "What's the default value(just hit enter for NULL)",
default: function () { return null }
},
{
type: 'confirm',
name: 'askAgain',
message: 'Want to add another field (just hit enter for YES)?',
default: true
}
],
updatePasswords: [
{
type: 'password',
name: 'password',
message: 'New Password of the User'
}
],
updateRoles: [
{
type: 'input',
name: 'role',
message: 'Name of the Role'
}
]
}

0 comments on commit 845c02e

Please sign in to comment.