utils.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import fs from 'node:fs';
  2. import path from 'node:path';
  3. const STORE_PATH_NAME = 'store';
  4. const TEMP_PATH_NAME = 'temp';
  5. export function getResourcesDir() {
  6. return process.env.NODE_ENV === 'development'
  7. ? path.join(__dirname, '../../resources/')
  8. : path.join(__dirname, '../../../app.asar.unpacked/resources/');
  9. }
  10. export function getRootDir() {
  11. return process.env.NODE_ENV === 'development'
  12. ? path.join(__dirname, '../../')
  13. : path.join(__dirname, '../../../../');
  14. }
  15. export function getDatabasePath() {
  16. return path.join(getRootDir(), 'database', 'database.sqlite');
  17. }
  18. export function getStoreDir() {
  19. return path.join(getRootDir(), STORE_PATH_NAME);
  20. }
  21. export function getTempPath() {
  22. return path.join(getStoreDir(), TEMP_PATH_NAME);
  23. }
  24. export function getImagicPath() {
  25. return path.join(getResourcesDir(), './imagemagick-7.1.1-11/');
  26. }
  27. export function getGmFontPath() {
  28. // return path.join(getResourcesDir(), './font/STHeiti Medium.ttc');
  29. return path.join(getResourcesDir(), './font/simsun.ttf');
  30. }
  31. export function makeDirSync(pathContent: string) {
  32. const mkPathList: string[] = [];
  33. let curPath = pathContent;
  34. while (!fs.existsSync(curPath)) {
  35. mkPathList.unshift(curPath);
  36. curPath = path.dirname(curPath);
  37. }
  38. mkPathList.forEach((mpath) => {
  39. fs.mkdirSync(mpath);
  40. });
  41. }
  42. makeDirSync(getTempPath());
  43. // 系统配置
  44. interface ConfigSetType {
  45. downloadProcessCount: number;
  46. }
  47. type PartConfigSetType = Partial<ConfigSetType>;
  48. type ConfigSetTypeEnum = keyof ConfigSetType;
  49. const configSets = [
  50. {
  51. field: 'downloadProcessCount',
  52. validate(val: any) {
  53. return [1, 2, 3].includes(val);
  54. },
  55. default: 1,
  56. },
  57. ];
  58. function getConfig(datas: PartConfigSetType) {
  59. const configData = {} as ConfigSetType;
  60. configSets.forEach((config) => {
  61. const field = config.field as ConfigSetTypeEnum;
  62. if (config.validate(datas[field])) {
  63. configData[field] = datas[field] as ConfigSetType[ConfigSetTypeEnum];
  64. } else {
  65. configData[field] = config.default;
  66. }
  67. });
  68. return configData;
  69. }
  70. export function getConfigData(data: PartConfigSetType) {
  71. let configData = getConfig(data);
  72. if (process.env.NODE_ENV === 'development') return configData;
  73. const configPath = path.join(getRootDir(), 'config.json');
  74. if (fs.existsSync(configPath)) {
  75. try {
  76. const configFileData = JSON.parse(
  77. fs.readFileSync(configPath, 'utf8')
  78. ) as PartConfigSetType;
  79. configData = getConfig(configFileData);
  80. } catch (error) {
  81. console.log(error);
  82. }
  83. }
  84. return configData;
  85. }