prompt.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import fs from 'node:fs'
  2. function getFolder(path) {
  3. const components = []
  4. const files = fs.readdirSync(path)
  5. files.forEach((item) => {
  6. const stat = fs.lstatSync(`${path}/${item}`)
  7. if (stat.isDirectory() === true && item !== 'components') {
  8. components.push(`${path}/${item}`)
  9. components.push(...getFolder(`${path}/${item}`))
  10. }
  11. })
  12. return components
  13. }
  14. export default {
  15. description: '创建组件',
  16. prompts: [
  17. {
  18. type: 'confirm',
  19. name: 'isGlobal',
  20. message: '是否为全局组件',
  21. default: false,
  22. },
  23. {
  24. type: 'list',
  25. name: 'path',
  26. message: '请选择组件创建目录',
  27. choices: getFolder('src/views'),
  28. when: (answers) => {
  29. return !answers.isGlobal
  30. },
  31. },
  32. {
  33. type: 'input',
  34. name: 'name',
  35. message: '请输入组件名称',
  36. validate: (v) => {
  37. if (!v || v.trim === '') {
  38. return '组件名称不能为空'
  39. }
  40. else {
  41. return true
  42. }
  43. },
  44. },
  45. ],
  46. actions: (data) => {
  47. let path = ''
  48. if (data.isGlobal) {
  49. path = 'src/components/{{properCase name}}/index.vue'
  50. }
  51. else {
  52. path = `${data.path}/components/{{properCase name}}/index.vue`
  53. }
  54. const actions = [
  55. {
  56. type: 'add',
  57. path,
  58. templateFile: 'plop-templates/component/index.hbs',
  59. },
  60. ]
  61. return actions
  62. },
  63. }