env.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { objAssign } from "./utils";
  2. const path = window.nodeRequire("path");
  3. const fs = window.nodeRequire("fs");
  4. const electron = window.nodeRequire("electron");
  5. const isDevelopment = process.env.NODE_ENV !== "production";
  6. const appPath = electron.remote.app.getAppPath();
  7. const homePath = isDevelopment
  8. ? path.join(appPath, "../")
  9. : path.join(appPath, "../../");
  10. const storePath = path.join(homePath, "stores");
  11. const extraPath = path.join(homePath, "extra");
  12. function initPath() {
  13. const paths = [
  14. storePath,
  15. getInputDir(),
  16. getStageDir(),
  17. getStoresDir("out"),
  18. getOutputDir("origin"),
  19. getTmpDir(),
  20. ];
  21. // console.log(paths);
  22. paths.forEach((path) => {
  23. if (!fs.existsSync(path)) fs.mkdirSync(path);
  24. });
  25. }
  26. initPath();
  27. // base
  28. function getHomeDir(name) {
  29. return path.join(homePath, name);
  30. }
  31. function getStoresDir(name) {
  32. return path.join(storePath, name);
  33. }
  34. function getExtraDir(name) {
  35. return path.join(extraPath, name);
  36. }
  37. function getImagicTool() {
  38. return path.join(getExtraDir("ImageMagick"), "/");
  39. }
  40. function getImgDecodeTool() {
  41. return path.join(getExtraDir("zxingA"), "zxing.exe");
  42. }
  43. // stores
  44. function getInputDir() {
  45. return getStoresDir("in");
  46. }
  47. function getStageDir() {
  48. return getStoresDir("stage");
  49. }
  50. function getOutputDir(type) {
  51. return path.join(getStoresDir("out"), type);
  52. }
  53. function getTmpDir() {
  54. return getStoresDir("tmp");
  55. }
  56. // extra
  57. function getDatabaseDir() {
  58. return getExtraDir("database");
  59. }
  60. function getScanExePath() {
  61. return path.join(extraPath, "scan", "scan.exe");
  62. }
  63. /**
  64. *
  65. * @param {String} pathContent 目录路径
  66. */
  67. function makeDirSync(pathContent) {
  68. let mkPathList = [];
  69. let curPath = pathContent;
  70. while (!fs.existsSync(curPath)) {
  71. mkPathList.unshift(curPath);
  72. curPath = path.dirname(curPath);
  73. }
  74. mkPathList.forEach((path) => {
  75. fs.mkdirSync(path);
  76. });
  77. }
  78. // function formatNum(num, [min, max]) {
  79. // return !num || num > max || num < min ? max : num;
  80. // }
  81. const configSets = [
  82. {
  83. field: "input",
  84. validate(val) {
  85. return !!val;
  86. },
  87. default: getInputDir(),
  88. },
  89. {
  90. field: "delayMode",
  91. validate(val) {
  92. return [0, 1].includes(val);
  93. },
  94. default: 0,
  95. },
  96. ];
  97. function getConfig(datas) {
  98. const configData = {};
  99. configSets.forEach((config) => {
  100. if (config.validate(datas[config.field])) {
  101. configData[config.field] = datas[config.field];
  102. } else {
  103. configData[config.field] = config.default;
  104. }
  105. });
  106. return configData;
  107. }
  108. function initConfigData(data) {
  109. let configData = getConfig(data);
  110. if (process.env.NODE_ENV === "development") return configData;
  111. const homePath = path.dirname(process.execPath);
  112. const configPath = path.join(homePath, "config.json");
  113. if (fs.existsSync(configPath)) {
  114. configData = JSON.parse(fs.readFileSync(configPath, "utf8"));
  115. configData = getConfig(configData);
  116. } else {
  117. fs.writeFileSync(configPath, JSON.stringify(configData), "utf8");
  118. }
  119. return configData;
  120. }
  121. export {
  122. initPath,
  123. getHomeDir,
  124. getStoresDir,
  125. getExtraDir,
  126. getImagicTool,
  127. getImgDecodeTool,
  128. getInputDir,
  129. getStageDir,
  130. getOutputDir,
  131. getScanExePath,
  132. getTmpDir,
  133. makeDirSync,
  134. getDatabaseDir,
  135. initConfigData,
  136. };