page.config.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const fs = require("fs");
  2. const pageJSON = require("./src/config/page.json");
  3. /**
  4. * 生成page所有页面
  5. */
  6. class pageConfig {
  7. constructor() {
  8. this.config = {
  9. path: process.cwd().split("\\").join("/"),
  10. parameters: this.getArgv(),
  11. };
  12. this.output = {};
  13. this.isOrNot(); // 页面是否存在
  14. this.getPagename(this.config.path + "/src/view");
  15. }
  16. getArgv() {
  17. const argv = {};
  18. process.argv.splice(2, process.argv.length).map(v => {
  19. const k = v.split("=");
  20. argv[k[0]] = k[1];
  21. });
  22. return argv;
  23. }
  24. /**
  25. * 判断page.json中的页面是否存在,如果不存在则创建该文件
  26. */
  27. isOrNot() {
  28. const keys = Object.keys(pageJSON);
  29. const baseUrl = this.config.path + "/src/view";
  30. for (let i = 0; i < keys.length; i++) {
  31. const v = keys[i];
  32. const baseU = baseUrl + "/" + v;
  33. const nameVue = baseU + "/index.vue";
  34. const nameJs = baseU + "/index.js";
  35. const isDir = fs.existsSync(baseU);
  36. if (!isDir) fs.mkdirSync(baseU);
  37. const isVue = fs.existsSync(nameVue);
  38. const isJs = fs.existsSync(nameJs);
  39. if (isVue && isJs) continue;
  40. const conf = pageJSON[v]['conf'] ? JSON.stringify(pageJSON[v]['conf']) : ''
  41. const templateVue = `
  42. <template>
  43. <van-swipe class="my-swipe" indicator-color="white">
  44. <template v-for="(item, index) in page.hoversList" :key="index">
  45. <van-swipe-item>
  46. <div class="pageItem" :style="{background: item.background_url}">
  47. <component
  48. v-for="(v, i) in item.components"
  49. :key="'son' + index + i"
  50. :is="components[v.type]"
  51. :item="v"
  52. ></component>
  53. </div>
  54. </van-swipe-item>
  55. </template>
  56. </van-swipe>
  57. </template>
  58. <script setup>
  59. // import { onMounted, reactive } from "vue";
  60. import { ref } from 'vue';
  61. // import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
  62. import localImage from '../../components/image.vue';
  63. import localParagraph from '../../components/paragraph.vue';
  64. import localFromComponent from '../../components/fromComponent.vue';
  65. /**
  66. * window.$originData.orginParames.title 页面标题
  67. * window.$originData.orginParames.parameters 固定参数值
  68. * window.$originData.urlParames url参数
  69. */
  70. console.log(window.$originData);
  71. const page = ref(${conf});
  72. const components = {
  73. image: localImage,
  74. paragraph: localParagraph,
  75. fromComponent: localFromComponent,
  76. };
  77. document.title = page.value.title;
  78. </script>
  79. <style>
  80. .page {
  81. width: 100vw;
  82. height: 100vh;
  83. }
  84. .pageItem{
  85. width: 100vw;
  86. height: 100vh;
  87. overflow-y: auto;
  88. }
  89. </style>
  90. `;
  91. const templateJS = `
  92. import App from './index.vue'
  93. import "@/assets/js/common"
  94. import {createApp} from 'vue'
  95. import {getPageParameters, environment} from "../../config/pageConfig"
  96. // 判断环境
  97. environment();
  98. window.$originData = getPageParameters();
  99. document.title = window.$originData.orginParames.title || "";
  100. createApp(App).mount('#app');
  101. `;
  102. if (!isVue) fs.writeFileSync(nameVue, templateVue);
  103. if (!isJs) fs.writeFileSync(nameJs, templateJS);
  104. }
  105. }
  106. /**
  107. * 获取页面列表
  108. */
  109. getPagename(viewPath) {
  110. const dirList = viewPath ? fs.readdirSync(viewPath) : [];
  111. if (!dirList.length) return;
  112. for (let i = 0; i < dirList.length; i++) {
  113. const path = viewPath + "/" + dirList[i];
  114. const stat = fs.lstatSync(path);
  115. if (!pageJSON[dirList[i]] && stat.isDirectory()) {
  116. this.removePage(path);
  117. continue;
  118. }
  119. this.output[dirList[i]] = {
  120. entry: path + "/index.js",
  121. template: "public/index.html",
  122. path: dirList[i],
  123. config: pageJSON[dirList[i]],
  124. filename: dirList[i] + ".html",
  125. };
  126. }
  127. }
  128. /**
  129. * 删除文件夹以及内部文件
  130. */
  131. removePage(path) {
  132. const dirList = path ? fs.readdirSync(path) : [];
  133. for (let i = 0; i < dirList.length; i++) {
  134. const fileName = path + "/" + dirList[i];
  135. const stat = fs.lstatSync(fileName);
  136. if (stat.isDirectory()) this.removePage(fileName);
  137. else fs.unlinkSync(fileName);
  138. }
  139. fs.rmdirSync(path);
  140. }
  141. }
  142. module.exports = new pageConfig();