utils.js 4.9 KB
Newer Older
M
muwoo 已提交
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 7
import {nativeImage} from 'electron';
import {APP_FINDER_PATH} from './constans';
M
muwoo 已提交
8
import {getlocalDataFile} from "../../../main/common/utils";
M
init  
muwoo 已提交
9 10 11 12 13 14 15 16 17 18

const store = new Store();

function getWindowHeight(searchList) {
  if (!searchList) return WINDOW_MAX_HEIGHT;
  if (!searchList.length) return WINDOW_MIN_HEIGHT;
  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;
}

function searchKeyValues(lists, value){
M
muwoo 已提交
19 20 21 22
  return lists.filter(item => {
    if (typeof item === 'string') return item.indexOf(value) >= 0;
    return item.type.indexOf(value) >= 0;
  })
M
init  
muwoo 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36
}

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

M
muwoo 已提交
37
const appPath = getlocalDataFile();
M
init  
muwoo 已提交
38

M
muwoo 已提交
39
async function downloadZip(downloadRepoUrl, name) {
M
muwoo 已提交
40
  try {
M
muwoo 已提交
41
    const plugin_path = appPath;
M
muwoo 已提交
42
    // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
M
muwoo 已提交
43 44 45 46
    // const temp_dest = `${plugin_path}/${name}`;
    await downloadFile(downloadRepoUrl, plugin_path,{extract: true});

    return `${plugin_path}/${name}`
M
muwoo 已提交
47 48
  } catch (e) {
    console.log(e);
M
muwoo 已提交
49 50 51
  }
}

M
init  
muwoo 已提交
52 53 54 55 56 57 58 59 60 61
const sysFile = {
  savePlugins(plugins) {
    store.set('user-plugins', plugins);
  },
  getUserPlugins() {
    try {
      return store.get('user-plugins').devPlugins;
    } catch (e) {
      return []
    }
M
muwoo 已提交
62 63 64
  },
  removeAllPlugins() {
    store.delete('user-plugins');
M
init  
muwoo 已提交
65 66 67
  }
}

M
muwoo 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81
function mergePlugins(plugins) {
  return [
    ...plugins,
    ...SYSTEM_PLUGINS.map(plugin => {
      return {
        ...plugin,
        status: true,
        sourceFile: '',
        type: 'system',
      }
    }),
  ]
}

M
muwoo 已提交
82
function find(p, target = 'plugin.json') {
M
muwoo 已提交
83 84 85 86 87 88 89
  try {
    let result;
    const fileList = fs.readdirSync(p);
    for (let i = 0; i < fileList.length; i++) {
      let thisPath = p + "/" + fileList[i];
      const data = fs.statSync(thisPath);

M
muwoo 已提交
90
      if (data.isFile() && fileList[i] === target) {
M
muwoo 已提交
91 92 93 94 95 96 97 98 99 100 101 102
        result = path.join(thisPath, '../');
        return result;
      }
      if (data.isDirectory()) {
        result = find(thisPath);
      }
    }
    return result;
  } catch (e) {
    console.log(e);
  }
}
M
muwoo 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
const fileLists = [];
// 默认搜索目录
APP_FINDER_PATH.forEach((searchPath) => {
  fs.readdir(searchPath, (err, files) => {
    try {
      for (let i = 0; i < files.length; i++) {
        const appName = files[i];
        const extname = path.extname(appName);
        const appSubStr = appName.split(extname)[0];
        if ((extname === '.app' || extname === '.prefPane') >= 0 ) {
          try {
            const path1 = path.join(searchPath, `${appName}/Contents/Resources/App.icns`);
            const path2 = path.join(searchPath, `${appName}/Contents/Resources/AppIcon.icns`);
            const path3 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr}.icns`);
            const path4 = path.join(searchPath, `${appName}/Contents/Resources/${appSubStr.replace(' ', '')}.icns`);
            let iconPath = path1;
            if (fs.existsSync(path1)) {
              iconPath = path1;
            } else if (fs.existsSync(path2)) {
              iconPath = path2;
            } else if (fs.existsSync(path3)) {
              iconPath = path3;
            } else if (fs.existsSync(path4)) {
              iconPath = path4;
            } else {
              // 性能最低的方式
              const resourceList = fs.readdirSync(path.join(searchPath, `${appName}/Contents/Resources`));
              const iconName = resourceList.filter(file => path.extname(file) === '.icns')[0];
              iconPath = path.join(searchPath, `${appName}/Contents/Resources/${iconName}`);
            }
            nativeImage.createThumbnailFromPath(iconPath, {width: 64, height: 64}).then(img => {
              fileLists.push({
                name: appSubStr,
                value: 'plugin',
                icon: img.toDataURL(),
                desc: path.join(searchPath, appName),
                type: 'app',
                action: `open ${path.join(searchPath, appName).replace(' ', '\\ ')}`
              })
            })
          } catch (e) {
          }

        }
      }
    } catch (e) {
      console.log(e);
    }
  });
});


function debounce(fn, delay) {
  let timer
  return function () {
    const context = this
    const args = arguments

    clearTimeout(timer)
    timer = setTimeout(function () {
      fn.apply(context, args)
    }, delay)
  }
}
M
muwoo 已提交
167

M
init  
muwoo 已提交
168 169 170 171
export {
  getWindowHeight,
  searchKeyValues,
  sysFile,
M
muwoo 已提交
172
  mergePlugins,
M
muwoo 已提交
173
  find,
M
muwoo 已提交
174
  downloadZip,
M
muwoo 已提交
175 176
  fileLists,
  debounce,
M
init  
muwoo 已提交
177
}