-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from andela-iamao/ft-inquire-on-init-8
#8 Asks setup information on start up
- Loading branch information
Showing
11 changed files
with
277 additions
and
14 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
function index () { | ||
return true; | ||
} | ||
const ReactRaise = require('./lib/ReactRaise'); | ||
|
||
module.exports = index; | ||
const app = new ReactRaise(); | ||
|
||
app.init(); |
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,62 @@ | ||
const inquirer = require('inquirer'); | ||
|
||
/** | ||
* Construct questions and ask then using the inquirer module | ||
* @class Inquire | ||
*/ | ||
class Inquire { | ||
|
||
/** | ||
* Creates an instance of Inquire. | ||
* @memberOf Inquire | ||
*/ | ||
constructor() { | ||
this.questions = []; | ||
this.validate = { | ||
notEmpty(value) { | ||
return (value && value.length && value.length > 0) ? true : 'Please enter a value'; | ||
}, | ||
yesNo(value) { | ||
return (value && ['y', 'n', 'yes', 'no'].indexOf(value.toLowerCase()) !== -1) ? | ||
true : 'Invalid input to yes/no question'; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Add a question to the questions property | ||
* @param {String} name - name of the question | ||
* @param {String} type - type of input | ||
* @param {String} message - body of the message | ||
* @param {String} validate - validation method to use | ||
* @returns {Void} - returns nothing | ||
* | ||
* @memberOf Inquire | ||
*/ | ||
question(name, type, message, validate) { | ||
const details = { | ||
name, | ||
type, | ||
message, | ||
validate: (!validate) ? undefined : this.validate[validate] | ||
}; | ||
this.questions.push(details); | ||
} | ||
|
||
/** | ||
* Combine and ask all the questions in the questions property | ||
* @param {Function} callback - function to run after user is done with questions | ||
* @returns {Void} - returns nothing | ||
* @memberOf Inquire | ||
*/ | ||
ask(callback) { | ||
return new Promise((resolve) => { | ||
inquirer.prompt(this.questions).then((answers) => { | ||
callback(answers); | ||
resolve(answers); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Inquire; |
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,79 @@ | ||
const Inquire = require('./Inquire'); | ||
|
||
/** | ||
* ReactRaise class | ||
* @class ReactRaise | ||
*/ | ||
class ReactRaise { | ||
|
||
/** | ||
* Creates an instance of ReactRaise. | ||
* @memberOf ReactRaise | ||
*/ | ||
constructor() { | ||
this.privateProps = new WeakMap(); | ||
this.privateProps.set(this, { | ||
setupInfo: {}, | ||
}); | ||
} | ||
|
||
/** | ||
* set or alter the private properties; | ||
* @param {String} key - key of item to set | ||
* @param {any} value - value to store inside private property | ||
* @returns {object} set private properties | ||
* @memberOf ReactRaise | ||
*/ | ||
setProp(key, value) { | ||
return this.privateProps.set( | ||
this, | ||
Object.assign({}, this.privateProps.get(this), { [key]: value }) | ||
); | ||
} | ||
|
||
/** | ||
* get the private properties; | ||
* @param {String} key - key of item to get | ||
* @returns {object} set private properties | ||
* @memberOf ReactRaise | ||
*/ | ||
getProp(key) { | ||
return this.privateProps.get(this)[key]; | ||
} | ||
|
||
/** | ||
* initialize command and ask setup questions | ||
* @param {Function} callback - function to execute after answers has been given | ||
* @returns {Void} returns nothing | ||
* @memberOf ReactRaise | ||
*/ | ||
init(callback) { | ||
const startInquire = new Inquire(); | ||
startInquire.question('description', 'input', 'Can you describe your app[optional]'); | ||
startInquire.question( | ||
'main', | ||
'input', | ||
'What is the main entry file of your app[Default: index.js]'); | ||
startInquire.question('author', 'input', 'What is your name[optional]'); | ||
startInquire.question( | ||
'license', | ||
'input', | ||
'What is license is your app under[Default: MIT]' | ||
); | ||
startInquire.question( | ||
'express', | ||
'input', | ||
'Do you want to configure an express server with this app(y/n)', | ||
'yesNo' | ||
); | ||
|
||
startInquire.ask((information) => { | ||
this.setProp('setupInfo', information); | ||
if (callback) { | ||
callback(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ReactRaise; |
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
File renamed without changes.
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,28 @@ | ||
module.exports = [ | ||
{ | ||
src: [ | ||
{ | ||
components: [ | ||
{ common: ['Button.component.jsx'] }, | ||
{ static: ['About.component.jsx'] }, | ||
'Routes.component.jsx', | ||
'Home.component.jsx' | ||
] | ||
}, | ||
{ actions: ['actionTypes.js'] }, | ||
{ reducers: ['root.reducer.js'] }, | ||
{ utils: ['helper.js'] }, | ||
'index.jsx', | ||
'store.js' | ||
] | ||
}, | ||
{ | ||
dist: [ | ||
{ css: ['style.css'] }, | ||
{ js: ['script.js'] }, | ||
{ vendor: ['vendor.txt'] }, | ||
{ images: ['images.txt'] }, | ||
'index.html' | ||
] | ||
} | ||
]; |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
const expect = require('chai').expect; | ||
const sinon = require('sinon'); | ||
|
||
global.expect = expect; | ||
global.expect = expect; | ||
global.sinon = sinon; |
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
const Inquire = require('../../lib/Inquire'); | ||
const bddStdin = require('bdd-stdin'); | ||
|
||
describe('Inquire', () => { | ||
let inquire; | ||
beforeEach(() => { | ||
inquire = new Inquire(); | ||
}); | ||
|
||
it('should create an instance of the Inquire class', () => { | ||
inquire = new Inquire(); | ||
expect(inquire.questions).to.eql([]); | ||
expect(inquire.validate).to.have.property('notEmpty'); | ||
expect(inquire.validate).to.have.property('yesNo'); | ||
}); | ||
|
||
describe('validate', () => { | ||
it('should validate user inputs for empty fields', () => { | ||
expect(inquire.validate.notEmpty('')).to.eql('Please enter a value'); | ||
expect(inquire.validate.notEmpty()).to.eql('Please enter a value'); | ||
expect(inquire.validate.notEmpty(true)).to.eql('Please enter a value'); | ||
expect(inquire.validate.notEmpty(null)).to.eql('Please enter a value'); | ||
expect(inquire.validate.notEmpty('input')).to.eql(true); | ||
}); | ||
it('should validate for yes or no questions', () => { | ||
expect(inquire.validate.yesNo()).to.eql('Invalid input to yes/no question'); | ||
expect(inquire.validate.yesNo('yeah')).to.eql('Invalid input to yes/no question'); | ||
expect(inquire.validate.yesNo('nope')).to.eql('Invalid input to yes/no question'); | ||
expect(inquire.validate.yesNo('yes')).to.eql(true); | ||
}); | ||
}); | ||
|
||
describe('question', () => { | ||
it('should add a question to the list of existing questions', () => { | ||
inquire.question('new', 'input', 'Is this a new question?'); | ||
inquire.question('new2', 'input', 'Is this another new question?', 'notEmpty'); | ||
expect(inquire.questions.length).to.eql(2); | ||
}); | ||
}); | ||
|
||
describe('ask', () => { | ||
it('should ask all the stored questions and execute a callback when done', () => { | ||
inquire.question('new', 'input', 'Is this a new question?'); | ||
bddStdin('answer\n'); | ||
const spy = sinon.spy(); | ||
return inquire.ask(spy).then(() => { | ||
expect(spy.calledOnce).to.be.true; | ||
}); | ||
}); | ||
}); | ||
}); |
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,32 @@ | ||
const bddStdin = require('bdd-stdin'); | ||
const ReactRaise = require('../../lib/ReactRaise'); | ||
|
||
describe('Index', () => { | ||
let reactRaise; | ||
beforeEach(() => { | ||
reactRaise = new ReactRaise(); | ||
}); | ||
it('should create an instance of ReactRaise', () => { | ||
reactRaise = new ReactRaise(); | ||
expect(reactRaise).to.be.instanceOf(ReactRaise); | ||
}); | ||
|
||
describe('init', () => { | ||
it('should start the commnd with a set of questions', (done) => { | ||
const currentProps = Object.assign({}, reactRaise.privateProps); | ||
bddStdin('m','\n', 'm', '\n', 'a', '\n', 'I', '\n', 'y', '\n'); | ||
reactRaise.init(() => { | ||
expect(currentProps).to.not.eql(reactRaise.privateProps); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('setProp and getProp', () => { | ||
it('should set value on the privateProp', () => { | ||
reactRaise.setProp('test', [1, 2,4]); | ||
const privateProp = reactRaise.getProp('test'); | ||
expect(privateProp).to.eql([1, 2,4]); | ||
}); | ||
}); | ||
}); |
Empty file.