prompt.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import path from 'node:path'
  2. import fs from 'node:fs'
  3. function getFolder(path) {
  4. const components = []
  5. const files = fs.readdirSync(path)
  6. files.forEach((item) => {
  7. const stat = fs.lstatSync(`${path}/${item}`)
  8. if (stat.isDirectory() === true && item !== 'components') {
  9. components.push(`${path}/${item}`)
  10. components.push(...getFolder(`${path}/${item}`))
  11. }
  12. })
  13. return components
  14. }
  15. export default {
  16. description: '创建页面',
  17. prompts: [
  18. {
  19. type: 'list',
  20. name: 'path',
  21. message: '请选择页面创建目录',
  22. choices: getFolder('src/views'),
  23. },
  24. {
  25. type: 'input',
  26. name: 'name',
  27. message: '请输入文件名',
  28. validate: (v) => {
  29. if (!v || v.trim === '') {
  30. return '文件名不能为空'
  31. }
  32. else {
  33. return true
  34. }
  35. },
  36. },
  37. ],
  38. actions: (data) => {
  39. const relativePath = path.relative('src/views', data.path)
  40. const actions = [
  41. {
  42. type: 'add',
  43. path: `${data.path}/{{dotCase name}}.vue`,
  44. templateFile: 'plop-templates/page/index.hbs',
  45. data: {
  46. componentName: `${relativePath} ${data.name}`,
  47. },
  48. },
  49. ]
  50. return actions
  51. },
  52. }