redux-create-action-index
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -14,2 +14,10 @@ #! /usr/bin/env node | ||
}) | ||
.options({ | ||
suffix: { | ||
alias: 's', | ||
default: 'Actions', | ||
description: 'suffix to add to the import module name', | ||
type: 'string', | ||
} | ||
}) | ||
.help() | ||
@@ -16,0 +24,0 @@ .argv |
@@ -6,7 +6,9 @@ { | ||
}, | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Creates an ES6 ./index.js file within target directories that imports all action files and exports a single action object.", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"pretest:create": "rm -f ./test/actions/index.js", | ||
"test:create": "node ./bin/redux-create-action-index ./test/actions" | ||
}, | ||
@@ -13,0 +15,0 @@ "repository": { |
# redux-create-action-index | ||
Creates or updates an index.js file within supplied target directories that will import all sibling redux action files and export a single redux action object. | ||
CLI utility to creates or update a index.js file in supplied target directories. The generated index file will import all sibling redux action files and export a single redux action object. | ||
### Installation | ||
```js | ||
npm install --save-dev redux-create-action-index | ||
``` | ||
### Usage | ||
```js | ||
redux-create-action-index ./src/actions | ||
``` | ||
### Example | ||
Given the following directory structure: | ||
```sh | ||
> tree ./src/actions | ||
./ | ||
├── file1.js | ||
├── file2.js | ||
├── file3.js | ||
├── file4.js | ||
└── file5.js | ||
0 directories, 5 files | ||
``` | ||
Running ```js redux-create-action-index ./src/actions``` will generate an ```index.js``` file in ```./src/actions``` containing: | ||
```js | ||
import * as file1Actions from './file1.js'; | ||
import * as file2Actions from './file2.js'; | ||
import * as file3Actions from './file3.js'; | ||
import * as file4Actions from './file4.js'; | ||
import * as file5Actions from './file5.js'; | ||
export default Object.assign({}, | ||
file1Actions, | ||
file2Actions, | ||
file3Actions, | ||
file4Actions, | ||
file5Actions | ||
); | ||
``` | ||
These ActionCreators can now be accessed in any component using redux's connect via: | ||
```js | ||
import {connect} from 'react-redux'; | ||
import ActionCreators from '../actions'; | ||
... | ||
function mapDispatchToProps(dispatch) { | ||
return bindActionCreators(ActionCreators, dispatch); | ||
} | ||
... | ||
export default connect(mapStateToProps, mapDispatchToProps)(Component); | ||
``` | ||
### CLI Options | ||
```sh | ||
--indent, -i set number of spaces to indent [number] [default: 4] | ||
--suffix, -s suffix to add to the import module name [string] [default: "Actions"] | ||
--help Show help [boolean] | ||
``` |
module.exports = function log(msg) { | ||
console.log('[redux-create-action-index] => ' + msg); | ||
console.log('[redux-create-action-index] ' + msg); | ||
}; |
@@ -19,3 +19,3 @@ const path = require('path'); | ||
module.exports = function(dir, options) { | ||
console.log(options) | ||
log(`processing ${dir}`); | ||
const sourceDir = path.resolve(process.cwd(), dir); | ||
@@ -27,3 +27,3 @@ | ||
return { | ||
module: child.replace('.js', '') + 'Actions', | ||
module: child.replace('.js', options.suffix), | ||
filename: child, | ||
@@ -37,2 +37,3 @@ } | ||
fs.writeFileSync(path.resolve(sourceDir, 'index.js'), str); | ||
} | ||
log(`generated redux action index file in ${dir}`); | ||
} |
6217
69
17
67