prompt.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const { notEmpty } = require('../utils.js')
  2. module.exports = {
  3. description: 'generate store',
  4. prompts: [{
  5. type: 'input',
  6. name: 'name',
  7. message: 'store name please',
  8. validate: notEmpty('name')
  9. },
  10. {
  11. type: 'checkbox',
  12. name: 'blocks',
  13. message: 'Blocks:',
  14. choices: [{
  15. name: 'state',
  16. value: 'state',
  17. checked: true
  18. },
  19. {
  20. name: 'mutations',
  21. value: 'mutations',
  22. checked: true
  23. },
  24. {
  25. name: 'actions',
  26. value: 'actions',
  27. checked: true
  28. }
  29. ],
  30. validate(value) {
  31. if (!value.includes('state') || !value.includes('mutations')) {
  32. return 'store require at least state and mutations'
  33. }
  34. return true
  35. }
  36. }
  37. ],
  38. actions(data) {
  39. const name = '{{name}}'
  40. const { blocks } = data
  41. const options = ['state', 'mutations']
  42. const joinFlag = `,
  43. `
  44. if (blocks.length === 3) {
  45. options.push('actions')
  46. }
  47. const actions = [{
  48. type: 'add',
  49. path: `src/store/modules/${name}.js`,
  50. templateFile: 'plop-templates/store/index.hbs',
  51. data: {
  52. options: options.join(joinFlag),
  53. state: blocks.includes('state'),
  54. mutations: blocks.includes('mutations'),
  55. actions: blocks.includes('actions')
  56. }
  57. }]
  58. return actions
  59. }
  60. }