index.js 2.14 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
/* eslint strict: ["off"] */

'use strict';

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

module.exports = {
  description: 'Add an unconnected component',
  prompts: [
    {
      type: 'input',
      name: 'name',
      message: 'What should it be called?',
      default: 'Button',
      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: 'wantMessages',
      default: true,
      message: 'Do you want i18n messages (i.e. will this component use text)?',
    },
    {
      type: 'confirm',
      name: 'wantLoadable',
      default: false,
      message: 'Do you want to load the component asynchronously?',
    },
  ],
  actions: data => {
    // Generate index.js and index.test.js
    const actions = [
      {
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
49
        path: '../../src/components/{{properCase name}}/index.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
50 51 52 53 54
        templateFile: './component/index.js.hbs',
        abortOnFail: true,
      },
      {
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
55
        path: '../../src/components/{{properCase name}}/tests/index.test.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
56 57 58 59 60 61 62 63 64
        templateFile: './component/test.js.hbs',
        abortOnFail: true,
      },
    ];

    // If the user wants i18n messages
    if (data.wantMessages) {
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
65
        path: '../../src/components/{{properCase name}}/messages.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
66 67 68 69 70 71 72 73 74
        templateFile: './component/messages.js.hbs',
        abortOnFail: true,
      });
    }

    // If the user wants Loadable.js to load the component asynchronously
    if (data.wantLoadable) {
      actions.push({
        type: 'add',
dengxiaofeng's avatar
dengxiaofeng committed
75
        path: '../../src/components/{{properCase name}}/Loadable.js',
Julien Benchetrit's avatar
Julien Benchetrit committed
76 77 78 79 80 81 82 83 84 85 86 87 88
        templateFile: './component/loadable.js.hbs',
        abortOnFail: true,
      });
    }

    actions.push({
      type: 'prettify',
      path: '/components/',
    });

    return actions;
  },
};