index.mjs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import request from '../utils/request.mjs';
  2. import { initFields } from '../utils/field.mjs';
  3. import btnList from '../example/btn.mjs';
  4. import allRoutes 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. routes: JSON.stringify(routes, (key, value) => {
  28. if (['plop'].includes(key)) {
  29. RoutePlopList.push(value)
  30. return undefined
  31. }
  32. return value
  33. }, 2).replaceAll(/"___|___"/g, '').replaceAll("~/pages", `~/pages/${rKey}`)
  34. }
  35. },
  36. ...RoutePlopList.map(RoutePlop => ({
  37. type: 'add',
  38. path: `src/pages/${rKey}/${RoutePlop.filePath}.vue`,
  39. templateFile: `plop/template/${RoutePlop.template}.vue.hbs`,
  40. data: async () => {
  41. console.log('\x1b[33m%s\x1b[0m', 'RoutePlop :>> ', RoutePlop.template, RoutePlop.filePath, RoutePlop.props);
  42. switch (RoutePlop.template) {
  43. case 'index': {
  44. const res = await request(RoutePlop.url + '/index')
  45. console.log('> request ', RoutePlop.url + '/index', ' status: ', res.data.code);
  46. const fields = initFields(res.data)
  47. return {
  48. url: RoutePlop.url,
  49. props: RoutePlop.props,
  50. ...btnList,
  51. ...fields,
  52. detailName: RoutePlop.routeName.replaceAll('INDEX', 'DETAIL'),
  53. // start from aid, then bid;cid....
  54. detailParam: String.fromCodePoint(97 + (RoutePlop.props?.length ?? 0)) + 'id'
  55. }
  56. }
  57. case 'tab': {
  58. console.log('tab_list :>> ', RoutePlop.children);
  59. return {
  60. tab_list: RoutePlop.children
  61. }
  62. }
  63. case 'detail': {
  64. const res = await request(RoutePlop.url + '/index')
  65. const fields = initFields(res.data)
  66. return {
  67. url: RoutePlop.url,
  68. props: RoutePlop.props,
  69. detailParam: RoutePlop.props.at(-1),
  70. ...fields
  71. }
  72. }
  73. default: {
  74. return {}
  75. }
  76. }
  77. }
  78. }))
  79. ]
  80. }))
  81. }
  82. }