-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update the cli for promises and factor out questions
- Loading branch information
1 parent
22ea2c7
commit 845c02e
Showing
2 changed files
with
132 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
|
@@ -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) | ||
|
@@ -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 { | ||
|
@@ -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},' | ||
|
@@ -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 { | ||
|
@@ -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},' | ||
|
@@ -387,7 +278,7 @@ module.exports = { | |
schema: schema, | ||
created: true | ||
}) | ||
schemaOutput = [] | ||
questions.schemaOutput = [] | ||
} | ||
}) | ||
} else { | ||
|
@@ -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) | ||
}) | ||
|
@@ -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: { | ||
|
@@ -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({ | ||
|
@@ -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) | ||
|
@@ -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) { | ||
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
] | ||
} |