utils.js 4.0 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 5 6
import download from 'download-git-repo';
import path from 'path';
import fs from 'fs';
import process from 'child_process';
import Store from 'electron-store';
M
muwoo 已提交
7
import downloadFile from 'download';
M
init  
muwoo 已提交
8 9 10 11 12 13 14 15 16 17

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 已提交
18 19 20 21
  return lists.filter(item => {
    if (typeof item === 'string') return item.indexOf(value) >= 0;
    return item.type.indexOf(value) >= 0;
  })
M
init  
muwoo 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
}

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

function mkdirFolder(name) {
  return new Promise((resolve, reject) => {
    process.exec(`mkdir ${name}`, async function (error, stdout, stderr) {
      if (error) {
        reject(false);
      } else {
        resolve(true);
      }
    })
  });
}

M
muwoo 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
async function downloadZip(downloadRepoUrl, name) {
  const plugin_path = path.join(__static, './plugins');
  const targetUrl = downloadRepoUrl ? downloadRepoUrl : `https://github.com/clouDr-f2e/${name}/archive/refs/heads/master.zip`;
  if (!(await existOrNot(plugin_path))) {
    await mkdirFolder(plugin_path);
  }
  // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
  const temp_dest = `${plugin_path}/${name}-master`;
  // 下载模板
  if (await existOrNot(temp_dest)) {
    await process.execSync(`rm -rf ${temp_dest}`);
  }
  await downloadFile(targetUrl, `${__static}/plugins`,{extract: true})
}

M
muwoo 已提交
63 64
function downloadFunc(downloadRepoUrl, name) {
  const targetGit = downloadRepoUrl ? downloadRepoUrl : `github:clouDr-f2e/${name}`;
M
init  
muwoo 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78
  const plugin_path = path.join(__static, './plugins');

  return new Promise(async (resolve, reject) => {
    try {
      if (!(await existOrNot(plugin_path))) {
        await mkdirFolder(plugin_path);
      }
      // 基础模版所在目录,如果是初始化,则是模板名称,否则是项目名称
      const temp_dest = `${plugin_path}/${name}`;
      // 下载模板
      if (await existOrNot(temp_dest)) {
        await process.execSync(`rm -rf ${temp_dest}`);
      }

M
muwoo 已提交
79
      download(targetGit, temp_dest, {clone: true}, function (err) {
M
init  
muwoo 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        console.log(err ? 'Error' : 'Success')
        if (err) {
          console.log(err);
          reject('请求模板下载失败');
        } else {
          resolve('请求模板下载成功');
        }
      })
    } catch (e) {
      console.log(e);
    }

  });
}

const sysFile = {
  savePlugins(plugins) {
    store.set('user-plugins', plugins);
  },
  getUserPlugins() {
    try {
      return store.get('user-plugins').devPlugins;
    } catch (e) {
      return []
    }
M
muwoo 已提交
105 106 107
  },
  removeAllPlugins() {
    store.delete('user-plugins');
M
init  
muwoo 已提交
108 109
  }
}
M
muwoo 已提交
110
sysFile.removeAllPlugins()
M
init  
muwoo 已提交
111

M
muwoo 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125
function mergePlugins(plugins) {
  return [
    ...plugins,
    ...SYSTEM_PLUGINS.map(plugin => {
      return {
        ...plugin,
        status: true,
        sourceFile: '',
        type: 'system',
      }
    }),
  ]
}

M
muwoo 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
function find(p) {
  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);

      if (data.isFile() && fileList[i] === 'plugin.json') {
        result = path.join(thisPath, '../');
        return result;
      }
      if (data.isDirectory()) {
        result = find(thisPath);
      }
    }
    return result;
  } catch (e) {
    console.log(e);
  }

}

M
init  
muwoo 已提交
149 150 151 152 153
export {
  getWindowHeight,
  searchKeyValues,
  downloadFunc,
  sysFile,
M
muwoo 已提交
154
  mergePlugins,
M
muwoo 已提交
155
  find,
M
muwoo 已提交
156
  downloadZip,
M
init  
muwoo 已提交
157
}