ref: 优化window读取系统应用,增加自动更新提示

This commit is contained in:
muwoo 2021-09-08 20:46:38 +08:00
parent 424c40a99c
commit 5118fa6ca4
3 changed files with 75 additions and 86 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "rubick2", "name": "rubick2",
"version": "0.0.3-beta.13", "version": "0.0.3-beta.14",
"author": "muwoo <2424880409@qq.com>", "author": "muwoo <2424880409@qq.com>",
"description": "An electron-vue project", "description": "An electron-vue project",
"license": null, "license": null,

View File

@ -5,7 +5,7 @@ import pkg from '../../../package.json';
const os = require('os'); const os = require('os');
const version = pkg.version; const version = pkg.version;
const releaseUrl = 'http://rubick-server.qa.91jkys.com/release/query'; const releaseUrl = 'http://118.195.176.247:8080/release/query';
export async function autoUpdate() { export async function autoUpdate() {
let res; let res;

View File

@ -1,102 +1,91 @@
import fs from 'fs'; import fs from 'fs';
import path from "path"; import path from 'path';
import os from 'os'; import os from 'os';
import child from 'child_process'; import translate from './translate';
import iconv from 'iconv-lite'; import {shell} from 'electron';
import translate from "./translate";
const fii = require('file-icon-info');
const filePath = path.resolve('C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs');
const fileLists = []; const fileLists = [];
const isZhRegex = /[\u4e00-\u9fa5]/; const isZhRegex = /[\u4e00-\u9fa5]/;
const getico = apps =>{ const icondir = path.join(os.tmpdir(), 'ProcessIcon');
const fii = require('file-icon-info'); const exists = fs.existsSync(icondir);
const icondir = path.join(os.tmpdir(), 'ProcessIcon'); if (!exists) { fs.mkdirSync(icondir) }
const exists = fs.existsSync(icondir);
if (!exists) { fs.mkdirSync(icondir) }
apps.forEach((app, i) => { const getico = app =>{
fii.getIcon(app.desc, data => { fii.getIcon(app.desc, data => {
let iconpath = path.join(icondir, `${app.LegalName}.png`) let iconpath = path.join(icondir, `${app.name}.png`)
fs.exists(iconpath, exists => { fs.exists(iconpath, exists => {
if (!exists) { if (!exists) {
fs.writeFile(iconpath, data, "base64", err => { fs.writeFile(iconpath, data, 'base64', err => {
if (err) { console.log(err); } if (err) { console.log(err); }
}) })
} }
});
}); });
}); });
} }
const powershell = (cmd, callback) => { function fileDisplay(filePath){
const ps = child.spawn('powershell', ['-NoProfile', '-Command', cmd], { encoding: 'buffer' }) //根据文件路径读取文件,返回文件列表
let chunks = []; fs.readdir(filePath,function(err,files){
let err_chunks = []; if(err){
ps.stdout.on('data', chunk => { console.warn(err)
chunks.push(iconv.decode(chunk, 'cp936')) }else{
}) files.forEach(function(filename){
ps.stderr.on('data', err_chunk => { const filedir = path.join(filePath, filename);
err_chunks.push(iconv.decode(err_chunk, 'cp936')) fs.stat(filedir,function(eror,stats){
}) if(eror){
ps.on('close', code => { console.warn('获取文件stats失败');
let stdout = chunks.join(""); } else {
let stderr = err_chunks.join(""); const isFile = stats.isFile(); // 是文件
callback(stdout, stderr) const isDir = stats.isDirectory(); // 是文件夹
}) if(isFile){
} const appName = filename.split('.')[0];
const keyWords = [appName];
const appDetail = shell.readShortcutLink(filedir);
if (!appDetail.target) return;
const getWinAppList = () => { if (isZhRegex.test(appName)) {
let filterValues = "Select-Object DisplayName,DisplayIcon,UninstallString,DisplayVersion,InstallDate,Publisher,InstallLocation" const py = translate(appName);
let localMatcine = `Get-ItemProperty HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`; const pinyinArr = py.split(',');
let currentUser = `Get-ItemProperty HKCU:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`; const zh_firstLatter = pinyinArr.map(py => py[0]);
let Wow6432Node = `Get-ItemProperty HKLM:\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | ${filterValues}`; // 拼音
let x64 = process.arch === 'x64' ? `;${Wow6432Node}` : ''; keyWords.push(pinyinArr.join(''));
powershell(`${localMatcine};${currentUser}${x64}`, (stdout, stderr) => { // 缩写
let apps = stdout.trim().replace(/\r\n[ ]{10,}/g,"").split('\r\n\r\n'); keyWords.push(zh_firstLatter.join(''));
for (const app of apps) { } else {
const dict = {} const firstLatter = appName.split(' ').map(name => name[0]).join('');
let lines = app.split('\r\n') keyWords.push(firstLatter);
for (var line of lines) { }
if (line) {
const key = line.split(/\s+:\s*/)[0];
const value = line.split(/\s+:\s*/)[1];
dict[key] = value;
}
}
if (dict.DisplayName && dict.DisplayIcon && dict.DisplayIcon.indexOf('.exe') >= 0) {
dict.LegalName = dict.DisplayName.replace(/[\\\/\:\*\?\"\<\>\|]/g, "");
dict.Icon = path.join(os.tmpdir(), 'ProcessIcon', `${encodeURIComponent(dict.LegalName)}.png`);
const firstLatter = dict.DisplayName.split(' ').map(name => name[0]).join('');
const appPath = dict.DisplayIcon.split(',')[0].replace(/"/g, '');
const keyWords = [dict.DisplayName, firstLatter];
if (isZhRegex.test(dict.DisplayName)) {
const py = translate(dict.DisplayName);
const pinyinArr = py.split(',');
const zh_firstLatter = pinyinArr.map(py => py[0]);
// 拼音
keyWords.push(pinyinArr.join(''));
// 缩写
keyWords.push(zh_firstLatter.join(''));
}
fileLists.push({ const icon = path.join(os.tmpdir(), 'ProcessIcon', `${encodeURIComponent(appName)}.png`);
...dict,
value: 'plugin', const appInfo = {
icon: dict.Icon, value: 'plugin',
desc: appPath, desc: appDetail.target,
type: 'app', type: 'app',
action: `start "dummyclient" "${appPath}"`, icon,
keyWords: keyWords, action: `start "dummyclient" "${appDetail.target}"`,
name: dict.DisplayName, keyWords: keyWords,
names: JSON.parse(JSON.stringify(keyWords)), name: appName,
}); names: JSON.parse(JSON.stringify(keyWords)),
} }
fileLists.push(appInfo);
getico(appInfo);
}
if(isDir) {
fileDisplay(filedir); // 递归,如果是文件夹,就继续遍历该文件夹下面的文件
}
}
})
});
} }
getico(fileLists);
}); });
} }
export const getApp = { export const getApp = {
init: getWinAppList, init: () => fileDisplay(filePath),
fileLists, fileLists
}; }