prompt.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const path = require('path')
  2. const fs = require('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. module.exports = {
  16. description: '创建标准模块 Mock',
  17. prompts: [
  18. {
  19. type: 'list',
  20. name: 'path',
  21. message: '请选择模块目录',
  22. choices: getFolder('src/views'),
  23. },
  24. ],
  25. actions: (data) => {
  26. const pathArr = path.relative('src/views', data.path).split('\\')
  27. const moduleName = pathArr.pop()
  28. const relativePath = pathArr.join('/')
  29. const actions = []
  30. actions.push({
  31. type: 'add',
  32. path: pathArr.length === 0 ? 'src/mock/{{moduleName}}.ts' : `src/mock/${pathArr.join('.')}.{{moduleName}}.ts`,
  33. templateFile: 'plop-templates/mock/mock.hbs',
  34. data: {
  35. relativePath,
  36. moduleName,
  37. },
  38. })
  39. return actions
  40. },
  41. }