index.js 4.59 KB
Newer Older
Julien Benchetrit's avatar
Julien Benchetrit committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
/**
 * Container Generator
 */

const componentExists = require('../utils/componentExists');

module.exports = {
  description: 'Add a container component',
  prompts: [
    {
      type: 'input',
      name: 'name',
      message: 'What should it be called?',
      default: 'Form',
      validate: value => {
        if (/.+/.test(value)) {
          return componentExists(value)
            ? 'A component or container with this name already exists'
            : true;
        }

        return 'The name is required';
      },
    },
    {
      type: 'confirm',
      name: 'memo',
      default: false,
      message: 'Do you want to wrap your component in React.memo?',
    },
    {
      type: 'confirm',
      name: 'wantHeaders',
      default: false,
      message: 'Do you want headers?',
    },
    {
      type: 'confirm',
      name: 'wantActionsAndReducer',
      default: true,
      message:
        'Do you want an actions/constants/selectors/reducer tuple for this container?',
    },
    {
      type: 'confirm',
      name: 'wantSaga',
      default: true,
      message: 'Do you want sagas for asynchronous flows? (e.g. fetching data)',
    },
    {
      type: 'confirm',
      name: 'wantMessages',
      default: true,
      message: 'Do you want i18n messages (i.e. will this component use text)?',
    },
    {
      type: 'confirm',
      name: 'wantLoadable',
      default: true,
      message: 'Do you want to load resources asynchronously?',
    },
  ],
  actions: data => {
    // Generate index.js and index.test.js
    const actions = [
      {
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
68
        path: '../../src/pages/{{properCase name}}/index.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
69 70 71 72 73
        templateFile: './container/index.js.hbs',
        abortOnFail: true,
      },
      {
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
74
        path: '../../src/pages/{{properCase name}}/tests/index.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
75 76 77 78 79 80 81 82 83
        templateFile: './container/test.js.hbs',
        abortOnFail: true,
      },
    ];

    // If component wants messages
    if (data.wantMessages) {
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
84
        path: '../../src/pages/{{properCase name}}/messages.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
85 86 87 88 89 90 91 92 93 94 95
        templateFile: './container/messages.js.hbs',
        abortOnFail: true,
      });
    }

    // If they want actions and a reducer, generate actions.js, constants.js,
    // reducer.js and the corresponding tests for actions and the reducer
    if (data.wantActionsAndReducer) {
      // Actions
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
96
        path: '../../src/pages/{{properCase name}}/actions.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
97 98 99 100 101
        templateFile: './container/actions.js.hbs',
        abortOnFail: true,
      });
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
102
        path: '../../src/pages/{{properCase name}}/tests/actions.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
103 104 105 106 107 108 109
        templateFile: './container/actions.test.js.hbs',
        abortOnFail: true,
      });

      // Constants
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
110
        path: '../../src/pages/{{properCase name}}/constants.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
111 112 113 114 115 116 117
        templateFile: './container/constants.js.hbs',
        abortOnFail: true,
      });

      // Selectors
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
118
        path: '../../src/pages/{{properCase name}}/selectors.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
119 120 121 122 123 124
        templateFile: './container/selectors.js.hbs',
        abortOnFail: true,
      });
      actions.push({
        type: 'add',
        path:
dengxiaofeng's avatar
dengxiaofeng committed
125
          '../../src/pages/{{properCase name}}/tests/selectors.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
126 127 128 129 130 131 132
        templateFile: './container/selectors.test.js.hbs',
        abortOnFail: true,
      });

      // Reducer
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
133
        path: '../../src/pages/{{properCase name}}/reducer.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
134 135 136 137 138
        templateFile: './container/reducer.js.hbs',
        abortOnFail: true,
      });
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
139
        path: '../../src/pages/{{properCase name}}/tests/reducer.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
140 141 142 143 144 145 146 147 148
        templateFile: './container/reducer.test.js.hbs',
        abortOnFail: true,
      });
    }

    // Sagas
    if (data.wantSaga) {
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
149
        path: '../../src/pages/{{properCase name}}/saga.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
150 151 152 153 154
        templateFile: './container/saga.js.hbs',
        abortOnFail: true,
      });
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
155
        path: '../../src/pages/{{properCase name}}/tests/saga.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
156 157 158 159 160 161 162 163
        templateFile: './container/saga.test.js.hbs',
        abortOnFail: true,
      });
    }

    if (data.wantLoadable) {
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
164
        path: '../..src/pages/{{properCase name}}/Loadable.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
165 166 167 168 169 170 171
        templateFile: './component/loadable.js.hbs',
        abortOnFail: true,
      });
    }

    actions.push({
      type: 'prettify',
dengxiaofeng's avatar
dengxiaofeng committed
172
      path: '/pages/',
Julien Benchetrit's avatar
Julien Benchetrit committed
173 174 175 176 177
    });

    return actions;
  },
};