普兆健康管家后端代码仓库
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.

57 lines
2.2 KiB

  1. /**
  2. *
  3. */
  4. import { GLOB_CONFIG_FILE_NAME, OUTPUT_DIR } from '../constant';
  5. import fs, { writeFileSync } from 'fs-extra';
  6. import colors from 'picocolors';
  7. import { getEnvConfig, getRootPath } from '../utils';
  8. import { getConfigFileName } from '../getConfigFileName';
  9. import pkg from '../../package.json';
  10. interface CreateConfigParams {
  11. configName: string;
  12. config: any;
  13. configFileName?: string;
  14. }
  15. function createConfig(params: CreateConfigParams) {
  16. const { configName, config, configFileName } = params;
  17. try {
  18. const windowConf = `window.${configName}`;
  19. // Ensure that the variable will not be modified
  20. let configStr = `${windowConf}=${JSON.stringify(config)};`;
  21. configStr += `
  22. Object.freeze(${windowConf});
  23. Object.defineProperty(window, "${configName}", {
  24. configurable: false,
  25. writable: false,
  26. });
  27. `.replace(/\s/g, '');
  28. fs.mkdirp(getRootPath(OUTPUT_DIR));
  29. writeFileSync(getRootPath(`${OUTPUT_DIR}/${configFileName}`), configStr);
  30. console.log(colors.cyan(`✨ [${pkg.name}]`) + ` - configuration file is build successfully:`);
  31. console.log(colors.gray(OUTPUT_DIR + '/' + colors.green(configFileName)) + '\n');
  32. // update-begin--author:sunjianlei---date:20250423---for:【QQYUN-9685】构建 electron 桌面应用
  33. // 如果是 Electron 环境,还需要将配置文件写入到 JSON 文件中
  34. if (config.VITE_GLOB_RUN_PLATFORM === 'electron') {
  35. writeFileSync(getRootPath(`${OUTPUT_DIR}/electron/env.json`), JSON.stringify(config));
  36. console.log(colors.cyan(`✨ [${pkg.name}]`) + ` - electron env file is build successfully:`);
  37. console.log(colors.gray(OUTPUT_DIR + '/' + colors.green('electron/env.json')) + '\n');
  38. }
  39. // update-end----author:sunjianlei---date:20250423---for:【QQYUN-9685】构建 electron 桌面应用
  40. } catch (error) {
  41. console.log(colors.red('configuration file configuration file failed to package:\n' + error));
  42. }
  43. }
  44. export function runBuildConfig() {
  45. const config = getEnvConfig();
  46. const configFileName = getConfigFileName(config);
  47. createConfig({ config, configName: configFileName, configFileName: GLOB_CONFIG_FILE_NAME });
  48. }