Redux Scaffold Generator
Install
$ npm install -g redux-scfld
Requirements
- redux
- redux-base
- redux-thunk
Convention
Redux Scaffold works with actions
, reducers
, initial states
and types
through the concept of
Redux Scaffold Entity (RSE). That's meaning all RSE grouped by RSE Namespace.
For a every single RSE exists a single action
and a single type
and for every RSE Namespace exists single initial state
and reducer
.
Redux Scaffold generates RSE from RSE Full Name of following format {RSENamespace}:{RSEName}
.
For example:
recently-posts:load-page
or recentlyPosts:loadPage
has recentlyPost
as RSE Namespace and loadPage
as RSE Name
.
Through this approach will be generated action
, reducer
, type
and initial state
.
Redux Scaffold action with parameter type
has also parameter status
which can be
STATUS_PROCESS
, STATUS_SUCCESS
and STATUS_FAILURE
in compare of usual behavior of Redux.
Let's scaffold
npm install -g redux-scfldcd ~/projects/my-projectredux init --path src/redux
Config
Inside of ~/projects/my-project
you will find a file .reduxrc
Here is property useCamelCasedPaths
affects naming behavior between camelCase
and dash-case
.
As template engine used doT
.
Structure
Let's create an action, reducer, type and initial state of RSE.
redux add config:load config:save posts:fetchPage
After this command will be generated following files
+---src
|
\---redux
| createStore.js
|
|
+---actions
| | index.js
| |
| +---config
| | load.js
| | save.js
| \---posts
| fetchPage.js
|
|
+---state
| index.js
| config.js
| posts.js
|
|
+---templates
| store.dot
| action.dot
| action-index.dot
| state.dot
| state-index.dot
| types.dot
|
|
\---types
index.js
Actions
src/redux/actions/posts/fetchPage.js
, src/redux/actions/config/load.js
, src/redux/actions/posts/save.js
will contain
;;NAMESPACE_CONFIG CONFIG_LOAD {/** Action code HERE */};
app/actions/index.js
contains
;;;
IMPORTANT: Instead of original Redux actions, Redux Scaffold Actions has only one type
, but one namespace
and 3 statuses
: STATUS_PROCESS
, STATUS_SUCCESS
, STATUS_FAILURE
IMPORTANT: You should avoid editing of generated index files. See templates generation
Types
// Statusesconst STATUS_PROCESS = 'STATUS_PROCESS';const STATUS_SUCCESS = 'STATUS_SUCCESS';const STATUS_FAILURE = 'STATUS_FAILURE';// Generated Namespacesconst NAMESPACE_CONFIG = 'config';const NAMESPACE_POSTS = 'posts';const NAMESPACES =NAMESPACE_CONFIGNAMESPACE_POSTS;// Generated typesconst CONFIG_LOAD = 'CONFIG_LOAD';const CONFIG_SAVE = 'CONFIG_SAVE';const POSTS_FETCH_PAGE = 'POSTS_FETCH_PAGE';
IMPORTANT: You should avoid editing of generated index files. See templates generation
Initial states
State is represented by separate files for each namespace and will be generated automatically for each namespace.
Store
Also will be generated src/redux/createStore.js
file with basic store setup.
You can edit this file for own purposes.
/*! Generated by redux-scfld */;;;;;const loggerMiddleware =;const reducer = ;const middleware =thunkMiddlewareloggerMiddleware;{const store =;store {return Promiseallactions;};store {forconst action of actionsawait store;};return store;}
Action dispatching
'use strict';const store = ;store;
Templates
Template files are using tiny and fast doT.js template engine.
From template you can access to it.entity
and it.entities
variables, see default templates for example.
Commands
$ redux init src/redux
- Generate Redux-Scfld config file .reduxrc
and save templates to src/redux/templates
$ redux add <entities>
- Adds entities separated by whitespace with Action, Type and Reducer and generates their indexes
$ redux del <entities>
- Deletes entities separated by whitespace with Action, Type and Reducer or all namespaces and generates their indexes
$ redux config
- Display current config
$ redux sync
- Sync indexes of Actions, Types and Reducers (does not affect already generated not indexes files)
$ redux list
- List of entities
$ redux namespaces
- List of namespaces
$ redux types
- List of types
$ redux --help
- Display help information
Demo project
(Powered by redux-scfld: React-Redux demo)[https://github.com/3axap4eHko/react-redux-demo]
License
The MIT License Copyright (c) 2016-2018 Ivan Zakharchenko