prompt.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. }