utils.js 4.1 KB
Newer Older
1
import { WINDOW_MAX_HEIGHT, WINDOW_MIN_HEIGHT, PRE_ITEM_HEIGHT, SYSTEM_PLUGINS } from './constans';
M
init  
muwoo 已提交
2 3 4
import path from 'path';
import fs from 'fs';
import Store from 'electron-store';
M
muwoo 已提交
5
import downloadFile from 'download';
M
muwoo 已提交
6
import { ipcRenderer } from 'electron';
7
import { getlocalDataFile } from '../../../main/common/utils';
8
import shell from 'shelljs';
M
init  
muwoo 已提交
9

M
muwoo 已提交
10
const getApp = process.platform === 'win32' ? require('./win-app').getApp : require('./darwin-app').getApp;
11

M
init  
muwoo 已提交
12 13
const store = new Store();

M
muwoo 已提交
14 15 16
getApp.init();
const fileLists = getApp.fileLists;

M
init  
muwoo 已提交
17 18 19
function getWindowHeight(searchList) {
  if (!searchList) return WINDOW_MAX_HEIGHT;
  if (!searchList.length) return WINDOW_MIN_HEIGHT;
20 21 22
  return searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5 > WINDOW_MAX_HEIGHT
    ? WINDOW_MAX_HEIGHT
    : searchList.length * PRE_ITEM_HEIGHT + WINDOW_MIN_HEIGHT + 5;
M
init  
muwoo 已提交
23 24
}

25 26
function searchKeyValues(lists, value) {
  return lists.filter((item) => {
M
muwoo 已提交
27 28
    if (typeof item === 'string') return item.indexOf(value) >= 0;
    return item.type.indexOf(value) >= 0;
29
  });
M
init  
muwoo 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
}

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

M
muwoo 已提交
44
const appPath = getlocalDataFile();
M
init  
muwoo 已提交
45

M
muwoo 已提交
46
async function downloadZip(downloadRepoUrl, name) {
M
muwoo 已提交
47
  try {
M
muwoo 已提交
48
    const plugin_path = appPath;
M
muwoo 已提交
49
    // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
M
muwoo 已提交
50 51 52
    const temp_dest = `${plugin_path}/${name}`;
    // 下载模板
    if (await existOrNot(temp_dest)) {
53
      shell.rm('-rf', temp_dest);
M
muwoo 已提交
54 55
    }

56
    await downloadFile(downloadRepoUrl, plugin_path, { extract: true });
M
muwoo 已提交
57

M
muwoo 已提交
58
    return temp_dest;
M
muwoo 已提交
59 60
  } catch (e) {
    console.log(e);
M
muwoo 已提交
61 62 63
  }
}

M
init  
muwoo 已提交
64 65
const sysFile = {
  savePlugins(plugins) {
66 67 68
    ipcRenderer.send('optionPlugin', {
      plugins: plugins.filter((plugin) => {
        let hasOption = false;
69 70
        plugin.features.forEach((fe) => {
          fe.cmds.forEach((cmd) => {
71 72 73
            if (cmd.type) {
              hasOption = true;
            }
74
          });
75 76 77 78
        });
        return hasOption;
      })
    });
M
init  
muwoo 已提交
79 80 81 82
    store.set('user-plugins', plugins);
  },
  getUserPlugins() {
    try {
M
muwoo 已提交
83
      return store.get('user-plugins');
M
init  
muwoo 已提交
84
    } catch (e) {
85
      return [];
M
init  
muwoo 已提交
86
    }
M
muwoo 已提交
87 88 89
  },
  removeAllPlugins() {
    store.delete('user-plugins');
M
init  
muwoo 已提交
90
  }
91
};
M
init  
muwoo 已提交
92

M
muwoo 已提交
93
function mergePlugins(plugins) {
94
  const result = [
M
muwoo 已提交
95
    ...plugins,
96
    ...SYSTEM_PLUGINS.map((plugin) => {
M
muwoo 已提交
97 98 99 100
      return {
        ...plugin,
        status: true,
        sourceFile: '',
101 102
        type: 'system'
      };
103
    })
104
  ];
105

M
muwoo 已提交
106 107 108 109 110
  const target = [];

  result.forEach((item, i) => {
    let targetIndex = -1;
    target.forEach((tg, j) => {
111
      if (tg.tag === item.tag && tg.type === 'system') {
112
        targetIndex = j;
113 114
      }
    });
M
muwoo 已提交
115
    if (targetIndex === -1) {
116
      target.push(item);
M
muwoo 已提交
117
    }
118
  });
119 120 121 122 123 124 125 126 127 128 129 130 131 132
  ipcRenderer &&
    ipcRenderer.send('optionPlugin', {
      plugins: target.filter((plugin) => {
        let hasOption = false;
        plugin.features.forEach((fe) => {
          fe.cmds.forEach((cmd) => {
            if (cmd.type) {
              hasOption = true;
            }
          });
        });
        return hasOption;
      })
    });
M
muwoo 已提交
133 134 135 136
  ipcRenderer &&
  ipcRenderer.send('pluginInit', {
    plugins: target
  });
137
  return target;
M
muwoo 已提交
138 139
}

M
muwoo 已提交
140
function find(p, target = 'plugin.json') {
M
muwoo 已提交
141 142 143 144
  try {
    let result;
    const fileList = fs.readdirSync(p);
    for (let i = 0; i < fileList.length; i++) {
145
      let thisPath = p + '/' + fileList[i];
M
muwoo 已提交
146 147
      const data = fs.statSync(thisPath);

M
muwoo 已提交
148
      if (data.isFile() && fileList[i] === target) {
M
muwoo 已提交
149 150 151 152 153 154 155 156 157 158 159 160
        result = path.join(thisPath, '../');
        return result;
      }
      if (data.isDirectory()) {
        result = find(thisPath);
      }
    }
    return result;
  } catch (e) {
    console.log(e);
  }
}
M
muwoo 已提交
161 162

function debounce(fn, delay) {
163 164 165 166
  let timer;
  return function() {
    const context = this;
    const args = arguments;
M
muwoo 已提交
167

168 169 170 171 172
    clearTimeout(timer);
    timer = setTimeout(function() {
      fn.apply(context, args);
    }, delay);
  };
M
init  
muwoo 已提交
173
}
174 175

export { getWindowHeight, searchKeyValues, sysFile, mergePlugins, find, downloadZip, fileLists, debounce };