156 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-01-17 13:40:59 +01:00
/**
* Container Generator
*/
2017-07-31 23:53:20 +02:00
const path = require('path');
// Plugin identifier based on the package.json `name` value
const pluginPkg = require(path.resolve(process.cwd(), 'package.json'));
const pluginId = pluginPkg.name.replace(
/^strapi-plugin-/i,
''
);
2017-01-17 13:40:59 +01:00
const componentExists = require('../utils/componentExists');
module.exports = {
description: 'Add a container component',
prompts: [{
2017-08-22 15:53:22 +02:00
type: 'list',
name: 'type',
message: 'Select the base component type:',
default: 'Stateless Function',
choices: () => ['Stateless Function', 'React.PureComponent', 'React.Component'],
}, {
2017-01-17 13:40:59 +01:00
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: 'wantHeaders',
default: false,
message: 'Do you want headers?',
}, {
type: 'confirm',
name: 'wantActionsAndReducer',
default: true,
2017-08-22 15:53:22 +02:00
message: 'Do you want an actions/constants/selectors/reducer tuple for this container?',
2017-01-17 13:40:59 +01:00
}, {
type: 'confirm',
2017-08-22 15:53:22 +02:00
name: 'wantSaga',
2017-01-17 13:40:59 +01:00
default: true,
message: 'Do you want sagas for asynchronous flows? (e.g. fetching data)',
}],
actions: (data) => {
const dataFormatted = data;
2017-07-31 23:53:20 +02:00
// Expose `pluginId` value
dataFormatted.pluginId = pluginId;
2017-07-31 23:53:20 +02:00
2017-01-17 13:40:59 +01:00
// Generate index.js and index.test.js
2017-08-22 15:53:22 +02:00
var componentTemplate; // eslint-disable-line no-var
switch (data.type) {
case 'Stateless Function': {
componentTemplate = './container/stateless.js.hbs';
break;
}
default: {
componentTemplate = './container/class.js.hbs';
}
}
2017-01-17 13:40:59 +01:00
const actions = [{
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/index.js',
2017-08-22 15:53:22 +02:00
templateFile: componentTemplate,
2017-01-17 13:40:59 +01:00
abortOnFail: true,
}, {
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/tests/index.test.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/test.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
2017-08-22 15:53:22 +02:00
if (data.wantActionsAndReducer) {
2017-01-17 13:40:59 +01:00
// Actions
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/actions.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/actions.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/tests/actions.test.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/actions.test.js.hbs',
abortOnFail: true,
});
// Constants
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/constants.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/constants.js.hbs',
abortOnFail: true,
});
// Selectors
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/selectors.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/selectors.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/tests/selectors.test.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/selectors.test.js.hbs',
abortOnFail: true,
});
// Reducer
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/reducer.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/reducer.js.hbs',
abortOnFail: true,
});
actions.push({
type: 'add',
2017-06-08 17:16:20 +01:00
path: '../../../../../admin/src/containers/{{properCase name}}/tests/reducer.test.js',
2017-01-17 13:40:59 +01:00
templateFile: './container/reducer.test.js.hbs',
abortOnFail: true,
});
}
// Sagas
2017-08-22 15:53:22 +02:00
if (data.wantSaga) {
2017-01-17 13:40:59 +01:00
actions.push({
type: 'add',
2017-08-22 15:53:22 +02:00
path: '../../../../../admin/src/containers/{{properCase name}}/saga.js',
templateFile: './container/saga.js.hbs',
2017-01-17 13:40:59 +01:00
abortOnFail: true,
});
actions.push({
type: 'add',
2017-08-22 15:53:22 +02:00
path: '../../../../../admin/src/containers/{{properCase name}}/tests/saga.test.js',
templateFile: './container/saga.test.js.hbs',
2017-01-17 13:40:59 +01:00
abortOnFail: true,
});
}
return actions;
},
};