page.config.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 templateVue = `
  41. <template>
  42. <div class="${v}"></div>
  43. </template>
  44. <script setup>
  45. // import { onMounted, reactive } from "vue";
  46. // import { isIpad, isIpod, isIphone } from "../../utils/isTerminal";
  47. /**
  48. * window.$originData.orginParames.title 页面标题
  49. * window.$originData.orginParames.parameters 固定参数值
  50. * window.$originData.urlParames url参数
  51. */
  52. console.log(window.$originData)
  53. </script>
  54. <style>
  55. .${v} {
  56. width: 100vw;
  57. height: 100vh;
  58. }
  59. </style>
  60. `;
  61. const templateJS = `
  62. import App from './index.vue'
  63. import "@/assets/js/common"
  64. import {createApp} from 'vue'
  65. import {getPageParameters, environment} from "../../config/pageConfig"
  66. // 判断环境
  67. environment();
  68. window.$originData = getPageParameters();
  69. document.title = window.$originData.orginParames.title || "";
  70. createApp(App).mount('#app');
  71. `;
  72. if (!isVue) fs.writeFileSync(nameVue, templateVue);
  73. if (!isJs) fs.writeFileSync(nameJs, templateJS);
  74. }
  75. }
  76. /**
  77. * 获取页面列表
  78. */
  79. getPagename(viewPath) {
  80. const dirList = viewPath ? fs.readdirSync(viewPath) : [];
  81. if (!dirList.length) return;
  82. for (let i = 0; i < dirList.length; i++) {
  83. const path = viewPath + "/" + dirList[i];
  84. const stat = fs.lstatSync(path);
  85. if (!pageJSON[dirList[i]] && stat.isDirectory()) {
  86. this.removePage(path);
  87. continue;
  88. }
  89. this.output[dirList[i]] = {
  90. entry: path + "/index.js",
  91. template: "public/index.html",
  92. path: dirList[i],
  93. config: pageJSON[dirList[i]],
  94. filename: dirList[i] + ".html",
  95. };
  96. }
  97. }
  98. /**
  99. * 删除文件夹以及内部文件
  100. */
  101. removePage(path) {
  102. const dirList = path ? fs.readdirSync(path) : [];
  103. for (let i = 0; i < dirList.length; i++) {
  104. const fileName = path + "/" + dirList[i];
  105. const stat = fs.lstatSync(fileName);
  106. if (stat.isDirectory()) this.removePage(fileName);
  107. else fs.unlinkSync(fileName);
  108. }
  109. fs.rmdirSync(path);
  110. }
  111. }
  112. module.exports = new pageConfig();