Skip to content

Commit

Permalink
Merge pull request #16 from andela-iamao/ft-inquire-on-init-8
Browse files Browse the repository at this point in the history
#8 Asks setup information on start up
  • Loading branch information
andela-iamao committed Jun 11, 2017
2 parents 81395ea + 2285d00 commit 46c2800
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 14 deletions.
8 changes: 4 additions & 4 deletions index.js
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();
62 changes: 62 additions & 0 deletions lib/Inquire.js
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;
79 changes: 79 additions & 0 deletions lib/ReactRaise.js
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;
20 changes: 18 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "a react cli starter kit that bootstraps a react application",
"main": "index.js",
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha --require test/helper test/*.spec.js",
"test": "NODE_ENV=test nyc --reporter=html --reporter=text mocha --require test/helper test/**/*.spec.js",
"coverage": "nyc report --reporter=text-lcov | coveralls"
},
"repository": {
Expand All @@ -26,15 +26,31 @@
"bugs": {
"url": "https://github.com/andela-iamao/react-raise/issues"
},
"nyc": {
"exclude": [
"index.js"
]
},
"homepage": "https://github.com/andela-iamao/react-raise#readme",
"devDependencies": {
"babel-eslint": "^7.2.3",
"bdd-stdin": "^0.2.0",
"chai": "^4.0.2",
"coveralls": "^2.13.1",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-plugin-import": "^2.3.0",
"mocha": "^3.4.2",
"nyc": "^11.0.2"
"nyc": "^11.0.2",
"sinon": "^2.3.4"
},
"dependencies": {
"chalk": "^1.1.3",
"clear": "0.0.1",
"clui": "^0.3.1",
"figlet": "^1.2.0",
"inquirer": "^3.1.0",
"lodash": "^4.17.4",
"touch": "^1.0.0"
}
}
File renamed without changes.
28 changes: 28 additions & 0 deletions sample/folder-schema.js
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'
]
}
];
4 changes: 3 additions & 1 deletion test/helper.js
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;
7 changes: 0 additions & 7 deletions test/index.spec.js

This file was deleted.

51 changes: 51 additions & 0 deletions test/lib/inquire.spec.js
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;
});
});
});
});
32 changes: 32 additions & 0 deletions test/lib/reactraise.spec.js
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 removed test/mocha.opts
Empty file.

0 comments on commit 46c2800

Please sign in to comment.