utils.js 4.9 KB
Newer Older
1 2 3
import {
  WINDOW_MAX_HEIGHT,
  WINDOW_MIN_HEIGHT,
璃白.'s avatar
璃白. 已提交
4
  WINDOW_MIN_HEIGHT_WITH_APP,
5
  PRE_ITEM_HEIGHT,
郭维嘉 已提交
6
  SYSTEM_PLUGINS
7 8 9 10 11 12 13 14 15 16 17 18 19
} from "./constans";
import path from "path";
import fs from "fs";
import Store from "electron-store";
import downloadFile from "download";
import { ipcRenderer } from "electron";
import { getLocalDataFile } from "../../../main/common/utils";
import shell from "shelljs";

const getApp =
  process.platform === "win32"
    ? require("./win-app").getApp
    : require("./darwin-app").getApp;
20

M
init  
muwoo 已提交
21 22
const store = new Store();

M
muwoo 已提交
23 24 25
getApp.init();
const fileLists = getApp.fileLists;

璃白.'s avatar
璃白. 已提交
26
function getWindowHeight(searchList, dingAppList = []) {
M
init  
muwoo 已提交
27
  if (!searchList) return WINDOW_MAX_HEIGHT;
璃白.'s avatar
璃白. 已提交
28 29
  if (!searchList.length)
    return dingAppList.length ? WINDOW_MIN_HEIGHT_WITH_APP : WINDOW_MIN_HEIGHT;
30 31
  return searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5 >
    WINDOW_MAX_HEIGHT
32 33
    ? WINDOW_MAX_HEIGHT
    : searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5;
M
init  
muwoo 已提交
34 35
}

36
function searchKeyValues(lists, value) {
郭维嘉 已提交
37
  return lists.filter(item => {
38 39
    if (typeof item === "string")
      return item.toLowerCase().indexOf(value.toLowerCase()) >= 0;
40
    return item.type.toLowerCase().indexOf(value.toLowerCase()) >= 0;
41
  });
M
init  
muwoo 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55
}

function existOrNot(path) {
  return new Promise((resolve, reject) => {
    fs.stat(path, async (err, stat) => {
      if (err) {
        resolve(false);
      } else {
        resolve(true);
      }
    });
  });
}

璃白.'s avatar
璃白. 已提交
56 57 58 59 60
function isMdFile(str) {
  if (!str) return false;
  return /\.md$/i.test(str);
}

61
const appPath = getLocalDataFile();
M
init  
muwoo 已提交
62

M
muwoo 已提交
63
async function downloadZip(downloadRepoUrl, name) {
M
muwoo 已提交
64
  try {
M
muwoo 已提交
65
    const plugin_path = appPath;
M
muwoo 已提交
66
    // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
M
muwoo 已提交
67 68 69
    const temp_dest = `${plugin_path}/${name}`;
    // 下载模板
    if (await existOrNot(temp_dest)) {
70
      shell.rm("-rf", temp_dest);
M
muwoo 已提交
71 72
    }

郭维嘉 已提交
73
    await downloadFile(downloadRepoUrl, temp_dest, { extract: true });
M
muwoo 已提交
74

M
muwoo 已提交
75
    return temp_dest;
M
muwoo 已提交
76 77
  } catch (e) {
    console.log(e);
M
muwoo 已提交
78 79
  }
}
璃白.'s avatar
璃白. 已提交
80 81
async function downloadZipFromGitCode(downloadRepoUrl, name) {
  try {
璃白.'s avatar
璃白. 已提交
82
    const plugin_path = `${appPath}/quicker-plugins`;
璃白.'s avatar
璃白. 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96
    // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
    const temp_dest = `${plugin_path}/${name}`;
    // 下载模板
    if (await existOrNot(temp_dest)) {
      shell.rm("-rf", temp_dest);
    }

    await downloadFile(downloadRepoUrl, temp_dest, { extract: true });

    return temp_dest;
  } catch (e) {
    console.log(e);
  }
}
M
muwoo 已提交
97

M
init  
muwoo 已提交
98 99
const sysFile = {
  savePlugins(plugins) {
100
    ipcRenderer.send("optionPlugin", {
郭维嘉 已提交
101
      plugins: plugins.filter(plugin => {
102
        let hasOption = false;
郭维嘉 已提交
103 104
        plugin.features.forEach(fe => {
          fe.cmds.forEach(cmd => {
105 106 107
            if (cmd.type) {
              hasOption = true;
            }
108
          });
109 110
        });
        return hasOption;
郭维嘉 已提交
111
      })
112
    });
113
    store.set("user-plugins", plugins);
M
init  
muwoo 已提交
114 115 116
  },
  getUserPlugins() {
    try {
117
      return store.get("user-plugins");
M
init  
muwoo 已提交
118
    } catch (e) {
119
      return [];
M
init  
muwoo 已提交
120
    }
M
muwoo 已提交
121 122
  },
  removeAllPlugins() {
123
    store.delete("user-plugins");
郭维嘉 已提交
124
  }
125
};
M
init  
muwoo 已提交
126

M
muwoo 已提交
127
function mergePlugins(plugins) {
128
  const result = [
M
muwoo 已提交
129
    ...plugins,
郭维嘉 已提交
130
    ...SYSTEM_PLUGINS.map(plugin => {
M
muwoo 已提交
131 132 133
      return {
        ...plugin,
        status: true,
134
        sourceFile: "",
郭维嘉 已提交
135
        type: "system"
136
      };
郭维嘉 已提交
137
    })
138
  ];
139

M
muwoo 已提交
140 141 142 143 144
  const target = [];

  result.forEach((item, i) => {
    let targetIndex = -1;
    target.forEach((tg, j) => {
145
      if (tg.tag === item.tag && tg.type === "system") {
146
        targetIndex = j;
147 148
      }
    });
M
muwoo 已提交
149
    if (targetIndex === -1) {
150
      target.push(item);
M
muwoo 已提交
151
    }
152
  });
153
  ipcRenderer &&
154
    ipcRenderer.send("optionPlugin", {
郭维嘉 已提交
155
      plugins: target.filter(plugin => {
156
        let hasOption = false;
郭维嘉 已提交
157 158
        plugin.features.forEach(fe => {
          fe.cmds.forEach(cmd => {
159 160 161 162 163 164
            if (cmd.type) {
              hasOption = true;
            }
          });
        });
        return hasOption;
郭维嘉 已提交
165
      })
166
    });
M
muwoo 已提交
167
  ipcRenderer &&
168
    ipcRenderer.send("pluginInit", {
郭维嘉 已提交
169
      plugins: target
170
    });
171
  return target;
M
muwoo 已提交
172 173
}

174
function find(p, target = "plugin.json") {
M
muwoo 已提交
175 176 177 178
  try {
    let result;
    const fileList = fs.readdirSync(p);
    for (let i = 0; i < fileList.length; i++) {
179
      let thisPath = p + "/" + fileList[i];
M
muwoo 已提交
180 181
      const data = fs.statSync(thisPath);

M
muwoo 已提交
182
      if (data.isFile() && fileList[i] === target) {
183
        result = path.join(thisPath, "../");
M
muwoo 已提交
184 185 186 187 188 189 190 191 192 193 194
        return result;
      }
      if (data.isDirectory()) {
        result = find(thisPath);
      }
    }
    return result;
  } catch (e) {
    console.log(e);
  }
}
M
muwoo 已提交
195 196

function debounce(fn, delay) {
197
  let timer;
郭维嘉 已提交
198
  return function () {
199 200
    const context = this;
    const args = arguments;
M
muwoo 已提交
201

202
    clearTimeout(timer);
郭维嘉 已提交
203
    timer = setTimeout(function () {
204 205 206
      fn.apply(context, args);
    }, delay);
  };
M
init  
muwoo 已提交
207
}
208

209 210 211 212 213 214 215
export {
  getWindowHeight,
  searchKeyValues,
  sysFile,
  mergePlugins,
  find,
  downloadZip,
璃白.'s avatar
璃白. 已提交
216
  downloadZipFromGitCode,
217
  fileLists,
璃白.'s avatar
璃白. 已提交
218 219
  debounce,
  isMdFile
220
};