import { objAssign } from "./utils"; const path = window.nodeRequire("path"); const fs = window.nodeRequire("fs"); const electron = window.nodeRequire("electron"); const isDevelopment = process.env.NODE_ENV !== "production"; const appPath = electron.remote.app.getAppPath(); const homePath = isDevelopment ? path.join(appPath, "../") : path.join(appPath, "../../"); const storePath = path.join(homePath, "stores"); const extraPath = path.join(homePath, "extra"); function initPath() { const paths = [ storePath, getInputDir(), getStageDir(), getStoresDir("out"), getOutputDir("origin"), getTmpDir(), ]; // console.log(paths); paths.forEach((path) => { if (!fs.existsSync(path)) fs.mkdirSync(path); }); } initPath(); // base function getHomeDir(name) { return path.join(homePath, name); } function getStoresDir(name) { return path.join(storePath, name); } function getExtraDir(name) { return path.join(extraPath, name); } function getImagicTool() { return path.join(getExtraDir("ImageMagick"), "/"); } function getImgDecodeTool() { return path.join(getExtraDir("zxingA"), "zxing.exe"); } // stores function getInputDir() { return getStoresDir("in"); } function getStageDir() { return getStoresDir("stage"); } function getOutputDir(type) { return path.join(getStoresDir("out"), type); } function getTmpDir() { return getStoresDir("tmp"); } // extra function getDatabaseDir() { return getExtraDir("database"); } function getScanExePath() { return path.join(extraPath, "scan", "scan.exe"); } /** * * @param {String} pathContent 目录路径 */ function makeDirSync(pathContent) { let mkPathList = []; let curPath = pathContent; while (!fs.existsSync(curPath)) { mkPathList.unshift(curPath); curPath = path.dirname(curPath); } mkPathList.forEach((path) => { fs.mkdirSync(path); }); } // function formatNum(num, [min, max]) { // return !num || num > max || num < min ? max : num; // } const configSets = [ { field: "input", validate(val) { return !!val; }, default: getInputDir(), }, { field: "delayMode", validate(val) { return [0, 1].includes(val); }, default: 0, }, ]; function getConfig(datas) { const configData = {}; configSets.forEach((config) => { if (config.validate(datas[config.field])) { configData[config.field] = datas[config.field]; } else { configData[config.field] = config.default; } }); return configData; } function initConfigData(data) { let configData = getConfig(data); if (process.env.NODE_ENV === "development") return configData; const homePath = path.dirname(process.execPath); const configPath = path.join(homePath, "config.json"); if (fs.existsSync(configPath)) { configData = JSON.parse(fs.readFileSync(configPath, "utf8")); configData = getConfig(configData); } else { fs.writeFileSync(configPath, JSON.stringify(configData), "utf8"); } return configData; } export { initPath, getHomeDir, getStoresDir, getExtraDir, getImagicTool, getImgDecodeTool, getInputDir, getStageDir, getOutputDir, getScanExePath, getTmpDir, makeDirSync, getDatabaseDir, initConfigData, };