prompt.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const { notEmpty } = require('./utils.js')
  2. const fs = require('fs')
  3. const path = require('path')
  4. function getFolder(path) {
  5. let components = []
  6. const files = fs.readdirSync(path)
  7. files.forEach(function (item) {
  8. let stat = fs.lstatSync(path + '/' + item)
  9. if (stat.isDirectory() === true && item != 'components') {
  10. components.push(path + '/' + item)
  11. components.push.apply(components, getFolder(path + '/' + item))
  12. }
  13. })
  14. return components
  15. }
  16. module.exports = {
  17. description: '创建前端模块',
  18. prompts: [
  19. {
  20. type: 'list',
  21. name: 'path',
  22. message: '请选择页面创建目录',
  23. choices: getFolder('src/views'),
  24. },
  25. {
  26. type: 'input',
  27. name: 'name',
  28. message: '请输入view名称',
  29. validate: notEmpty('name'),
  30. },
  31. {
  32. type: 'input',
  33. name: 'table',
  34. message: '请输入Table名称',
  35. validate: notEmpty('table'),
  36. },
  37. ],
  38. actions: (data) => {
  39. let moduleName = path.relative('src/views', data.path)
  40. const pathCaseName = '{{ pathCase name }}'
  41. const properCaseName = '{{ properCase name }}'
  42. const camelCaseName = '{{ camelCase name }}'
  43. return [
  44. {
  45. type: 'queryTableColumns',
  46. speed: 'slow',
  47. },
  48. {
  49. type: 'add',
  50. path: `src/views/${moduleName}/${pathCaseName}/index.vue`,
  51. templateFile: './template/index.hbs',
  52. force: true,
  53. data: {
  54. moduleName: moduleName,
  55. },
  56. },
  57. {
  58. type: 'add',
  59. path: `src/views/${moduleName}/${pathCaseName}/components/${properCaseName}Edit.vue`,
  60. templateFile: './template/edit.hbs',
  61. force: true,
  62. data: {
  63. moduleName: moduleName,
  64. },
  65. },
  66. {
  67. type: 'add',
  68. path: `src/api/${moduleName}/${camelCaseName}.js`,
  69. templateFile: './template/api.hbs',
  70. force: true,
  71. data: {
  72. moduleName: moduleName,
  73. },
  74. },
  75. ]
  76. },
  77. }