All Projects → ArchivedRepos → Redux Boilerplate Helpers

ArchivedRepos / Redux Boilerplate Helpers

AST-based tool for automating Redux boilerplate

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redux Boilerplate Helpers

Forge Node App
🛠📦🎉 Generate Node.js boilerplate with optional libraries & tools
Stars: ✭ 90 (-4.26%)
Mutual labels:  boilerplate
Ts Type Info
TypeScript AST and code generator [Deprecated]
Stars: ✭ 90 (-4.26%)
Mutual labels:  ast
Gorm2sql
auto generate sql from gorm model struct
Stars: ✭ 92 (-2.13%)
Mutual labels:  ast
Jsx Ast Utils
AST utility module for statically analyzing JSX
Stars: ✭ 89 (-5.32%)
Mutual labels:  ast
Boilerplate App
A curated directory of boilerplates to help you start your projects!
Stars: ✭ 90 (-4.26%)
Mutual labels:  boilerplate
Babel Plugin React Persist
Automatically useCallback() & useMemo(); memoize inline functions
Stars: ✭ 91 (-3.19%)
Mutual labels:  ast
React Boilerplate
This project is deprecated. Please use CRA instead.
Stars: ✭ 88 (-6.38%)
Mutual labels:  boilerplate
Pyast64
Compile a subset of the Python AST to x64-64 assembler
Stars: ✭ 93 (-1.06%)
Mutual labels:  ast
Testing Hapi
Hapi style guide compliant boilerplate (updated to v17!)
Stars: ✭ 90 (-4.26%)
Mutual labels:  boilerplate
Libdparse
Library for lexing and parsing D source code
Stars: ✭ 91 (-3.19%)
Mutual labels:  ast
Astq
Abstract Syntax Tree (AST) Query Engine
Stars: ✭ 89 (-5.32%)
Mutual labels:  ast
Ant Design Pro Plus
✨ 基于 ant-design-pro 做一些微小的工作。
Stars: ✭ 88 (-6.38%)
Mutual labels:  boilerplate
Meteor Apollo Starter Kit
Meteor, Apollo, React, PWA, Styled-Components boilerplate
Stars: ✭ 91 (-3.19%)
Mutual labels:  boilerplate
Diffsitter
A tree-sitter based AST difftool to get meaningful semantic diffs
Stars: ✭ 89 (-5.32%)
Mutual labels:  ast
Pug Sass Boilerplate Starter Kit
A Front-end template for building web apps or sites using Pug(Jade) and Sass
Stars: ✭ 92 (-2.13%)
Mutual labels:  boilerplate
Angular Boilerplate
An opinionated boilerplate project for AngularJS applications, crafted with best practices in mind.
Stars: ✭ 88 (-6.38%)
Mutual labels:  boilerplate
Rust graphql api boilerplate
A Boilerplate of GraphQL API built in Rust + Warp + Juniper + Diesel
Stars: ✭ 91 (-3.19%)
Mutual labels:  boilerplate
Projecttemplate
个人项目模板
Stars: ✭ 93 (-1.06%)
Mutual labels:  boilerplate
Rest Nestjs Postgres
CrudJS implemented as a REST API, using Nest.js and Postgres
Stars: ✭ 93 (-1.06%)
Mutual labels:  boilerplate
Nice Parser
Nice parsers in OCaml without the boilerplate
Stars: ✭ 91 (-3.19%)
Mutual labels:  boilerplate

redux-boilerplate-helpers

Build Status Coverage Status npm Greenkeeper badge styled with prettier

Table of Contents

A commandline tool and small helper library that helps you cut down on the amount of typing you need to do while writing Redux boilerplate.

Currently, the existing tools used for automating Redux Boilerplate only create new files from templates and are unable to modify existing files. redux-boilerplate-helpers's tool parses your code into an AST (Abstract Syntax Tree) to automate adding Redux boilerplate code into your existing files. If you already have existing* action creators, constants, or a reducer in a file, it will ensure the code you want to add is put in the correct place. And if you are missing any of the files, the tool will create new ones for you.

* Provided your code follows the following format. That being said, this tool should flexible enough to be modified to handle most setups (See Roadmap for more info).

Demo

Try out the online interactive demo here.

Tool

Let's start out with the following three files which already have content in them: Demo/constants.js, Demo/actions.js, Demo/reducer.js

// Demo/constants.js
export const SOME_ACTION = 'Demo/SOME_ACTION';
export const ANOTHER_ACTION = 'Demo/ANOTHER_ACTION';

// Demo/actions.js
import { createAction } from 'redux-boilerplate-helpers'; // see `Lib` section
import { SOME_ACTION, ANOTHER_ACTION } from './constants';

// this is equivalent to `export const someAction = payload => ({ type: SOME_ACTION, payload });
export const someAction = createAction(SOME_ACTION);
export const anotherAction = createAction(ANOTHER_ACTION);
// Demo/reducer.js
import { SOME_ACTION, ANOTHER_ACTION } from './constants';

const initialState = {};

function demoReducer (state = initialState, action) {
  switch(action.type) {
    case SOME_ACTION:
      return { ...state, id: action.payload.id, result: action.payload.result };
    case ANOTHER_ACTION:
      return { ...state, something: action.payload };
    default:
      return state;
  }
}

export default demoReducer;

If we want to add three more actions: GET_SOMETHING, GET_SOMETHING_SUCCESS, and GET_SOMETHING_ERROR, we can simply run

rdxh --dir ./Demo getSomething get_something_success getSomethingError --single-quote --trailing-comma all

We can use any combination of camelCase, PascalCase, snake_case, or CONSTANT_CASE to specify the action names. Formatting options are passed directly to prettier—which is what this tool uses to format the finalized output:

// Demo/constants.js
export const SOME_ACTION = 'Demo/SOME_ACTION';
export const ANOTHER_ACTION = 'Demo/ANOTHER_ACTION';
export const GET_SOMETHING = 'Demo/GET_SOMETHING';
export const GET_SOMETHING_SUCCESS = 'Demo/GET_SOMETHING_SUCCESS';
export const GET_SOMETHING_ERROR = 'Demo/GET_SOMETHING_ERROR';

// Demo/actions.js
import { createAction } from 'redux-boilerplate-helpers'; // see `Lib` section
import {
  SOME_ACTION,
  ANOTHER_ACTION,
  GET_SOMETHING,
  GET_SOMETHING_SUCCESS,
  GET_SOMETHING_ERROR,
} from './constants';

// this is equivalent to `export const someAction = payload => ({ type: SOME_ACTION, payload });
export const someAction = createAction(SOME_ACTION);
export const anotherAction = createAction(ANOTHER_ACTION);
export const getSomething = createAction(GET_SOMETHING);
export const getSomethingSuccess = createAction(GET_SOMETHING_SUCCESS);
export const getSomethingError = createAction(GET_SOMETHING_ERROR);
// Demo/reducer.js
import {
  SOME_ACTION,
  ANOTHER_ACTION,
  GET_SOMETHING,
  GET_SOMETHING_SUCCESS,
  GET_SOMETHING_ERROR,
} from './constants';
const initialState = {};

function demoReducer(state = initialState, action) {
  switch (action.type) {
    case SOME_ACTION:
      return { ...state, id: action.payload.id, result: action.payload.result };
    case ANOTHER_ACTION:
      return { ...state, something: action.payload };
    case GET_SOMETHING:
      return {
        ...state,
      };
    case GET_SOMETHING_SUCCESS:
      return {
        ...state,
      };
    case GET_SOMETHING_ERROR:
      return {
        ...state,
      };
    default:
      return state;
  }
}

export default demoReducer;

The tool will also make sure it doesn't add duplicate entries if parts of the code are already in place (e.g. if you define a constant and create an action for it, if you run the tool with that constant name as a paramter, the tool will only modify the reducers.js file).

And if the constants.js, actions.js, or reducer.js file is not present in the directory, the tool will create them for you.

ActionCreator

createAction(
  type: string,
  payloadCreator: function = identity,
  { name: string = 'payload', meta: function },
);

This little util is inspired by redux actions. This tool is a little simpler and less prescriptive and does not enforce the FSA pattern. It also does not replicate the other helper functions redux actions provides.

A lot actions often wind up looking similar, so this helper cuts down on some code for the most basic case (similar to redux-actions).

In it's simplest form, you just call createAction() with your constant, and it will return an action creator with an identity function.

const ACTION = 'ACTION';
const act = createAction(ACTION);

expect(act('test')).toEqual({ type: ACTION, payload: 42 });

The name field allows you to override the named of the member that the payloadCreator funcion returns to (defaults to payload). The meta field is an optional function that creates metadata for the payload. It receives the same arguments as the payload creator, but its result becomes the meta field of the resulting action. If metaCreator is undefined or not a function, the meta field is omitted.

I'm still not entirely sure how useful this helper function is and will likely end up updating the tool to directly add an action creator (without a helper function) and put adding the helper library behind a flag.

Installation

Project Specific

First run:

npm i redux-boilerplate-helpers
# or yarn add redux-boilerplate-helpers

then add the following to the scripts secion of your package.json

{
  ...
  "scripts": {
    "rdxh": "rdxh"
  },
  ...
}

Globally

npm i -g redux-boilerplate-helpers
npm i redux-boilerplate-helpers

# or yarn global add redux-boilerplate-helpers
# yarn add redux-boilerplate-helpers

Usage

Project Specific

Note that the --dir option requires a path relative to your project root. You can use pwd if you want to run the tool in the directory you are currently in. This limitation is due to how npm scripts work. If you'd like to use relative paths, the tool will need to be installed globally (see next section).

npm run rdxh -- --dir path/to/folder actionName anotherAction --trailing-comma all --single-quote
# if you prefer to run it in the path you are currently in, you need to run
npm run rdxh -- --dir `pwd` actionName anotherAction --trailing-comma all --single-quote

Globally

Installing the tool globaly allows you to use a relative path for the --dir option.

rdxh --dir ./ actionName anotherAction --trailing-comma all -single-quote

Roadmap

I created this to help with my specific use case. However, there are many different ways people structure their code. With some changes, this tool should be able to support a majority of use cases. A few quick future changes which could be useful include (but are not limited to):

  • Ability to specify the exact location of each file (this includes support for people using the ducks pattern).

  • Add an option to insert the new code into the files without running it through prettier. This would basically mean passing the formatting options directly to the recast print function.

  • Provide a way to customize the default ActionCreators.

  • Additional configuration options (TBD).

Contributing

To set up the environment, clone the repository, install the dependencies and run the tests:

yarn
yarn test

Before you make a PR, please make sure you run:

yarn run format

This command runs eslint and prettier to esure that the PR conforms to the existing codebase.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].