plopfile.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // plop 的入口文件
  2. // 需要导出一个函数,函数接收一个plop对象,用于创建生成器任务
  3. import path from 'path'
  4. import fs from 'fs'
  5. function getFolder(path) {
  6. let components = []
  7. const files = fs.readdirSync(path)
  8. files.forEach(function(item) {
  9. let stat = fs.lstatSync(path + '/' + item)
  10. if (stat.isDirectory() === true && item !== 'components') {
  11. components.push(path + '/' + item)
  12. components.push.apply(components, getFolder(path + '/' + item))
  13. }
  14. })
  15. return components
  16. }
  17. module.exports = plop => {
  18. plop.setWelcomeMessage('请选择需要创建的模式:')
  19. // setGenerator可以设置一个生成器,每个生成器都可用于生成特定的文件
  20. // 接收两个参数,生成器的名称和配置选项
  21. plop.setGenerator('default-page', {
  22. // 生成器的描述
  23. description: '创建一个默认页面',
  24. // 命令行提示
  25. prompts: [
  26. {
  27. type: 'list',
  28. name: 'path',
  29. message: '请选择页面创建目录',
  30. choices: getFolder('src/views')
  31. },
  32. {
  33. type: 'input',
  34. name: 'name', // 接收变量的参数
  35. message: '请输入页面文件名称',
  36. validate: v => {
  37. if (!v || v.trim === '') {
  38. return '文件名不能为空'
  39. } else {
  40. return true
  41. }
  42. }
  43. }
  44. ],
  45. // 完成命令行后执行的操作,每个对象都是动作对象
  46. actions: data => {
  47. let relativePath = path.relative('src/views', data.path)
  48. const actions = [
  49. {
  50. type: 'add', // 动作类型
  51. path: `${data.path}/{{name}}.vue`, // 生成文件的输出路径
  52. templateFile: 'plop-templates/default-page.hbs', // template 模板的文件路径,目录下的文件遵循hbs的语法规则
  53. data: {
  54. componentName: `${relativePath} ${data.name}`
  55. }
  56. }
  57. ]
  58. return actions
  59. }
  60. })
  61. plop.setGenerator('table-page', {
  62. description: '创建一个表格页面',
  63. prompts: [
  64. {
  65. type: 'list',
  66. name: 'path',
  67. message: '请选择页面创建目录',
  68. choices: getFolder('src/views')
  69. },
  70. {
  71. type: 'input',
  72. name: 'name',
  73. message: '请输入页面文件名称',
  74. validate: v => {
  75. if (!v || v.trim === '') {
  76. return '文件名不能为空'
  77. } else {
  78. return true
  79. }
  80. }
  81. }
  82. ],
  83. actions: data => {
  84. let relativePath = path.relative('src/views', data.path)
  85. const actions = [
  86. {
  87. type: 'add',
  88. path: `${data.path}/{{name}}.vue`,
  89. templateFile: 'plop-templates/table-page.hbs',
  90. data: {
  91. componentName: `${relativePath} ${data.name}`
  92. }
  93. }
  94. ]
  95. return actions
  96. }
  97. })
  98. plop.setGenerator('component', {
  99. description: '创建组件',
  100. prompts: [
  101. {
  102. type: 'confirm',
  103. name: 'isGlobal',
  104. message: '是否为全局组件',
  105. default: true
  106. },
  107. {
  108. type: 'list',
  109. name: 'path',
  110. message: '请选择组件创建目录',
  111. choices: getFolder('src/views'),
  112. when: answers => {
  113. return !answers.isGlobal
  114. }
  115. },
  116. {
  117. type: 'input',
  118. name: 'name',
  119. message: '请输入组件名称',
  120. validate: v => {
  121. if (!v || v.trim === '') {
  122. return '组件名称不能为空'
  123. } else {
  124. return true
  125. }
  126. }
  127. }
  128. ],
  129. actions: data => {
  130. let path = ''
  131. if (data.isGlobal) {
  132. path = 'src/components/{{properCase name}}/index.vue'
  133. } else {
  134. path = `${data.path}/components/{{properCase name}}/index.vue`
  135. }
  136. const actions = [
  137. {
  138. type: 'add',
  139. path: path,
  140. templateFile: 'plop-templates/component.hbs'
  141. }
  142. ]
  143. return actions
  144. }
  145. })
  146. }