prompt.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import fs from 'node:fs'
  2. import path from 'node:path'
  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. type: 'confirm',
  39. name: 'isFilesystem',
  40. message: '是否为基于文件系统的路由页面',
  41. default: false,
  42. },
  43. ],
  44. actions: (data) => {
  45. const relativePath = path.relative('src/views', data.path)
  46. const actions = [
  47. {
  48. type: 'add',
  49. path: `${data.path}/{{dotCase name}}.vue`,
  50. templateFile: 'plop-templates/page/index.hbs',
  51. data: {
  52. componentName: `${relativePath} ${data.name}`,
  53. },
  54. },
  55. ]
  56. return actions
  57. },
  58. }