You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB

  1. import dayjs from "dayjs";
  2. import { readdir, stat } from "node:fs";
  3. import { fileURLToPath } from "node:url";
  4. import { dirname, resolve } from "node:path";
  5. import { sum, formatBytes } from "@pureadmin/utils";
  6. import {
  7. name,
  8. version,
  9. engines,
  10. dependencies,
  11. devDependencies
  12. } from "../package.json";
  13. /** 启动`node`进程时所在工作目录的绝对路径 */
  14. const root: string = process.cwd();
  15. /**
  16. * @description
  17. * @param dir `build`
  18. * @param metaUrl `url``build``import.meta.url`
  19. */
  20. const pathResolve = (dir = ".", metaUrl = import.meta.url) => {
  21. // 当前文件目录的绝对路径
  22. const currentFileDir = dirname(fileURLToPath(metaUrl));
  23. // build 目录的绝对路径
  24. const buildDir = resolve(currentFileDir, "build");
  25. // 解析的绝对路径
  26. const resolvedPath = resolve(currentFileDir, dir);
  27. // 检查解析的绝对路径是否在 build 目录内
  28. if (resolvedPath.startsWith(buildDir)) {
  29. // 在 build 目录内,返回当前文件路径
  30. return fileURLToPath(metaUrl);
  31. }
  32. // 不在 build 目录内,返回解析后的绝对路径
  33. return resolvedPath;
  34. };
  35. /** 设置别名 */
  36. const alias: Record<string, string> = {
  37. "@": pathResolve("../src"),
  38. "@build": pathResolve()
  39. };
  40. /** 平台的名称、版本、运行所需的`node`和`pnpm`版本、依赖、最后构建时间的类型提示 */
  41. const __APP_INFO__ = {
  42. pkg: { name, version, engines, dependencies, devDependencies },
  43. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  44. };
  45. /** 处理环境变量 */
  46. const warpperEnv = (envConf: Recordable): ViteEnv => {
  47. // 默认值
  48. const ret: ViteEnv = {
  49. VITE_PORT: 8848,
  50. VITE_PUBLIC_PATH: "",
  51. VITE_ROUTER_HISTORY: "",
  52. VITE_CDN: false,
  53. VITE_HIDE_HOME: "false",
  54. VITE_COMPRESSION: "none"
  55. };
  56. for (const envName of Object.keys(envConf)) {
  57. let realName = envConf[envName].replace(/\\n/g, "\n");
  58. realName =
  59. realName === "true" ? true : realName === "false" ? false : realName;
  60. if (envName === "VITE_PORT") {
  61. realName = Number(realName);
  62. }
  63. ret[envName] = realName;
  64. if (typeof realName === "string") {
  65. process.env[envName] = realName;
  66. } else if (typeof realName === "object") {
  67. process.env[envName] = JSON.stringify(realName);
  68. }
  69. }
  70. return ret;
  71. };
  72. const fileListTotal: number[] = [];
  73. /** 获取指定文件夹中所有文件的总大小 */
  74. const getPackageSize = options => {
  75. const { folder = "dist", callback, format = true } = options;
  76. readdir(folder, (err, files: string[]) => {
  77. if (err) throw err;
  78. let count = 0;
  79. const checkEnd = () => {
  80. ++count == files.length &&
  81. callback(format ? formatBytes(sum(fileListTotal)) : sum(fileListTotal));
  82. };
  83. files.forEach((item: string) => {
  84. stat(`${folder}/${item}`, async (err, stats) => {
  85. if (err) throw err;
  86. if (stats.isFile()) {
  87. fileListTotal.push(stats.size);
  88. checkEnd();
  89. } else if (stats.isDirectory()) {
  90. getPackageSize({
  91. folder: `${folder}/${item}/`,
  92. callback: checkEnd
  93. });
  94. }
  95. });
  96. });
  97. files.length === 0 && callback(0);
  98. });
  99. };
  100. export { root, pathResolve, alias, __APP_INFO__, warpperEnv, getPackageSize };