Skip to content

Commit

Permalink
Optionally include redux-thunk, resolve stylesuxx#73
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoScabbiolo committed Mar 24, 2017
1 parent 27ce27f commit c847b19
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
25 changes: 22 additions & 3 deletions generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ class AppGenerator extends Generator {
this.option('skip-install');
}

prompting() {
return this.prompt([{
type: 'confirm',
name: 'thunk',
message: 'Would you like to include redux-thunk?',
default: false,
store: true,
}]).then(answers => {
this.thunk = answers.thunk;
});
}

install() {
if(!this.options['skip-install']) {
this.installDependencies({ bower: false });
Expand All @@ -29,11 +41,18 @@ class AppGenerator extends Generator {

// Run the create root method
this.composeWith('react-webpack-redux:root', {
args: ['Root']
args: ['Root'],
thunk: this.thunk,
});

// Install redux and react bindings as requirement
this.npmInstall(['redux', 'react-redux'], { 'save': true });
// Install redux and react bindings as requirement and redux-thunk optionally
let packages = ['redux', 'react-redux'];

if (this.thunk) {
packages.push('redux-thunk');
}

this.npmInstall(packages, { 'save': true });
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions generators/root/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
const Generator = require('yeoman-generator');
const fs = require('fs');

class RootGenerator extends Generator {
writing() {
Expand All @@ -12,7 +11,7 @@ class RootGenerator extends Generator {

// Copy the store
this.fs.copy(
this.templatePath('store.js'),
this.options.thunk ? this.templatePath('store-thunk.js') : this.templatePath('store.js'),
this.destinationPath('src/stores/index.js')
);

Expand Down Expand Up @@ -52,6 +51,7 @@ class RootGenerator extends Generator {
this.destinationPath('.eslintrc')
);
}

};

module.exports = RootGenerator;
25 changes: 25 additions & 0 deletions generators/root/templates/store-thunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import { createStore, compose, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import reducers from '../reducers';

function reduxStore(initialState) {
const store = createStore(reducers, initialState, compose(
applyMiddleware(thunk),
window.devToolsExtension ? window.devToolsExtension() : f => f
));

if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', function () {
// We need to require for hot reloading to work properly.
const nextReducer = require('../reducers'); // eslint-disable-line global-require

store.replaceReducer(nextReducer);
});
}

return store;
}

export default reduxStore;

0 comments on commit c847b19

Please sign in to comment.