utils.ts 2.6 KB

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