index.mjs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import request from '../utils/request.mjs';
  2. import { initFields } from '../utils/field.mjs';
  3. import btnList from '../example/btn.mjs';
  4. import allRoutes, { RoutesTitle } from '../example/routes.mjs';
  5. import fs from 'fs';
  6. export default {
  7. prompts: [],
  8. actions: (prompts) => {
  9. return [
  10. // function () {
  11. // fs.rmSync(`src/router/routes`, { recursive: true, force: true })
  12. // return `-- \\src\\router\\routes`
  13. // },
  14. // function () {
  15. // fs.rmSync(`src/pages`, { recursive: true, force: true })
  16. // return `-- \\src\\pages`
  17. // },
  18. ].concat(...Object.keys(allRoutes).map(rKey => {
  19. const routes = allRoutes[rKey]
  20. const RoutePlopList = []
  21. return [
  22. {
  23. type: 'add',
  24. path: `src/router/routes/${rKey}.ts`,
  25. templateFile: 'plop/template/routes.ts.hbs',
  26. data: {
  27. title: RoutesTitle[rKey],
  28. routes: JSON.stringify(routes, (key, value) => {
  29. if (['plop'].includes(key)) {
  30. RoutePlopList.push(value)
  31. return undefined
  32. }
  33. return value
  34. }, 2).replaceAll(/"___|___"/g, '').replaceAll("~/pages", `~/pages/${rKey}`)
  35. }
  36. },
  37. ...RoutePlopList.map(RoutePlop => ({
  38. type: 'add',
  39. path: `src/pages/${rKey}/${RoutePlop.filePath}.vue`,
  40. templateFile: `plop/template/${RoutePlop.template}.vue.hbs`,
  41. data: async () => {
  42. console.log('\x1b[33m%s\x1b[0m', 'RoutePlop :>> ', RoutePlop.template, RoutePlop.filePath, RoutePlop.props);
  43. switch (RoutePlop.template) {
  44. case 'index': {
  45. const res = await request(RoutePlop.url + '/index')
  46. console.log('> request ', RoutePlop.url + '/index', ' status: ', res.data.code);
  47. const fields = initFields(res.data)
  48. return {
  49. url: RoutePlop.url,
  50. props: RoutePlop.props,
  51. ...btnList,
  52. ...fields,
  53. detailName: RoutePlop.routeName.replaceAll('INDEX', 'DETAIL'),
  54. // start from aid, then bid;cid....
  55. detailParam: String.fromCodePoint(97 + (RoutePlop.props?.length ?? 0)) + 'id'
  56. }
  57. }
  58. case 'tab': {
  59. console.log('tab_list :>> ', RoutePlop.children);
  60. return {
  61. tab_list: RoutePlop.children
  62. }
  63. }
  64. case 'detail': {
  65. const res = await request(RoutePlop.url + '/index')
  66. const fields = initFields(res.data)
  67. return {
  68. url: RoutePlop.url,
  69. props: RoutePlop.props,
  70. detailParam: RoutePlop.props.at(-1),
  71. ...fields
  72. }
  73. }
  74. default: {
  75. return {}
  76. }
  77. }
  78. }
  79. }))
  80. ]
  81. }))
  82. }
  83. }