Skip to content

Commit

Permalink
重构部分Util;网站按创建时间排序,支持分页
Browse files Browse the repository at this point in the history
  • Loading branch information
xianyunleo committed Dec 1, 2023
1 parent 9c99ce7 commit 1f240fa
Show file tree
Hide file tree
Showing 34 changed files with 460 additions and 491 deletions.
66 changes: 31 additions & 35 deletions src/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
InitFiles_DIR_NAME,
TEMP_DIR_NAME
} from '@/main/utils/constant'
import Directory from '@/main/utils/Directory'
import DirUtil from '@/main/utils/DirUtil'
import FileUtil from '@/main/utils/FileUtil'
import Path from '@/main/utils/Path'
import child_process from 'child_process'
import fs from 'fs'
import Software from "@/main/core/software/Software";
import GetPath from "@/shared/utils/GetPath";
import LocalInstall from "@/main/core/software/LocalInstall";
import FsUtil from '@/main/utils/FsUtil'

const app = electronRequire('app');

Expand Down Expand Up @@ -112,8 +112,8 @@ export default class App {
return path.join(this.getDir(), `extra/${process.platform}`);
}

static initFileExists() {
return FileUtil.Exists(this.getInitFilePath());
static async initFileExists() {
return await FileUtil.Exists(this.getInitFilePath());
}

static async init() {
Expand All @@ -123,34 +123,34 @@ export default class App {

let initFile = this.getInitFilePath();

if (!FileUtil.Exists(initFile)) {
if (!await FileUtil.Exists(initFile)) {
return;
}

const softwareDirExists = Software.DirExists();
const softwareDirExists = await Software.DirExists();

if (isMacOS && !isDev) {
if (!Directory.Exists(MAC_USER_CORE_DIR)) {
Directory.CreateDirectory(MAC_USER_CORE_DIR);
if (!await DirUtil.Exists(MAC_USER_CORE_DIR)) {
await DirUtil.Create(MAC_USER_CORE_DIR);
}
await this.updateMacCoreSubDir(['Library']);
}

this.moveInitFiles(["downloads", "www"]);
this.createCoreSubDir(["software", "database", "bin",`${TEMP_DIR_NAME}/php`]);
await this.moveInitFiles(["downloads", "www"]);
await this.createCoreSubDir(["software", "database", "bin",`${TEMP_DIR_NAME}/php`]);

if (!softwareDirExists) { //目录不存在说明是第一次安装,不是覆盖安装
const files = Directory.GetFiles(GetPath.getDownloadsDir());
const files = await DirUtil.GetFiles(GetPath.getDownloadsDir());
await LocalInstall.installMultiple(files)
}

FileUtil.Delete(initFile);
await FileUtil.Delete(initFile);
}

static deleteInitFile(){
static async deleteInitFile() {
let initFile = this.getInitFilePath();
if (FileUtil.Exists(initFile)) {
FileUtil.Delete(initFile);
if (await FileUtil.Exists(initFile)) {
await FileUtil.Delete(initFile);
}
}

Expand All @@ -169,27 +169,27 @@ export default class App {
let corePath = this.getCoreDir();
for (const dir of dirs) {
let source = Path.Join(corePath, dir);
if (!Directory.Exists(source)) {
if (!await DirUtil.Exists(source)) {
continue;
}
let target = Path.Join(MAC_USER_CORE_DIR, dir);
if (!Directory.Exists(target)) {
Directory.CreateDirectory(target);
if (!await DirUtil.Exists(target)) {
await DirUtil.Create(target);
}
child_process.execSync(`rsync -a ${source}/* ${target}`);
await Directory.Delete(source);
await DirUtil.Delete(source);
}
}

/**
* 创建目录,如果目录不存在的情况下
* @param dirs
*/
static createCoreSubDir(dirs) {
static async createCoreSubDir(dirs) {
for (const dir of dirs) {
let p = path.join(this.getUserCoreDir(), dir);
if (!Directory.Exists(p)) {
Directory.CreateDirectory(p);
if (!await DirUtil.Exists(p)) {
await DirUtil.Create(p);
}
}
}
Expand All @@ -198,24 +198,20 @@ export default class App {
* 将initFiles目录下的文件(文件夹)移动到用户操作的核心目录
* @param files
*/
static moveInitFiles(files = []) {
let initFilesPath = Path.Join(this.getCoreDir(),InitFiles_DIR_NAME);
static async moveInitFiles(files = []) {
let initFilesPath = Path.Join(this.getCoreDir(), InitFiles_DIR_NAME);
for (const file of files) {
let source = Path.Join(initFilesPath, file);
if(!fs.existsSync(source)){
continue;
}
let target = Path.Join(this.getUserCoreDir(), file);
if (!fs.existsSync(target)) {
fs.renameSync(source, target);
const source = Path.Join(initFilesPath, file);
const target = Path.Join(this.getUserCoreDir(), file);

if (await FsUtil.Exists(target)) {
FsUtil.Remove(source, { force: true, recursive: true }) //不捕捉错误
} else {
let options = {force: true, recursive: true};
fs.rm(source, options, err => {
console.log(`Error moveInitFiles fs.rm ${source}\r${err}`);
});
await FsUtil.Rename(source, target)
}
}
}

static exit() {
app.exit();
}
Expand Down
21 changes: 5 additions & 16 deletions src/main/core/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ import TcpProcess from "@/main/utils/TcpProcess";
import { isWindows } from '@/main/utils/utils'

export default class Database {

/**
*
* @param version {string}
* @returns {Promise<void>}
*/
static async initMySQL(version) {
//mysql user的密码在mysql data目录
await Database.initMySQLData(version);
await Database.resetMySQLPassword(version);
}

/**
*
* @param version {string}
Expand Down Expand Up @@ -52,14 +40,15 @@ export default class Database {
}
let mysqlPath = GetPath.getMysqlDir(version);
let resetPwdPath = path.join(mysqlPath, 'reset-pwd.txt');
FileUtil.WriteAllText(resetPwdPath, resetCommand);
await FileUtil.WriteAll(resetPwdPath, resetCommand);

let confFilePath = this.getMySQLConfFilePath(version);
let portMatch = FileUtil.ReadAllText(confFilePath).match(/\[mysqld].*?port\s*=\s*(\d+)/s);
let confText = await FileUtil.ReadAll(confFilePath)
let portMatch = confText.match(/\[mysqld].*?port\s*=\s*(\d+)/s)
let port = portMatch ? portMatch[1] : 3306;

let oldPid = await TcpProcess.getPidByPort(port);
if(oldPid){
if (oldPid) {
await ProcessExtend.kill(oldPid);
}

Expand All @@ -79,7 +68,7 @@ export default class Database {
}
await sleep(100);
await ProcessExtend.kill(childProcess.pid);
FileUtil.Delete(resetPwdPath);
await FileUtil.Delete(resetPwdPath);
}

static getMySQLConfFilePath(version) {
Expand Down
20 changes: 10 additions & 10 deletions src/main/core/Env/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ export default class Env {
* @param targetPath
* @param binName
*/
static createBinFile(targetPath, binName) {
static async createBinFile(targetPath, binName) {
let binDirPath = GetPath.getBinDir();
let path = Path.Join(binDirPath, this.getBinFileName(binName));
this.deleteBinFile(binName);
await this.deleteBinFile(binName);
if (isWindows) {
let text;
if (binName === 'composer') {
text = `@echo off\r\nphp "${targetPath}" %*`;
} else {
text = `@echo off\r\n"${targetPath}" %*`
}
FileUtil.WriteAllText(path, text);
await FileUtil.WriteAll(path, text);
} else {
if (binName === 'php') {
this.createOtherBinFile(targetPath, 'phpize', 'phpize');
Expand All @@ -38,22 +38,22 @@ export default class Env {
FileUtil.CreateSymbolicLink(path, targetOtherFilePath);
}

static deleteBinFile(binName) {
static async deleteBinFile(binName) {
let path = Path.Join(GetPath.getBinDir(), this.getBinFileName(binName));
if (FileUtil.Exists(path)) {
FileUtil.Delete(path);
if (await FileUtil.Exists(path)) {
await FileUtil.Delete(path);
}
if (!isWindows) {
if (binName === 'php') {
this.deleteOtherBinFile('phpize');
await this.deleteOtherBinFile('phpize');
}
}
}

static deleteOtherBinFile(otherBinName) {
static async deleteOtherBinFile(otherBinName) {
let path = Path.Join(GetPath.getBinDir(), this.getBinFileName(otherBinName));
if (FileUtil.Exists(path)) {
FileUtil.Delete(path);
if (await FileUtil.Exists(path)) {
await FileUtil.Delete(path);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/core/Env/EnvMacOS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export default class EnvMacOS {
let userName = OS.getUserName();
let text;

if (!FileUtil.Exists(envFilePath)) {
FileUtil.WriteAllText(envFilePath, '')
if (!await FileUtil.Exists(envFilePath)) {
await FileUtil.WriteAll(envFilePath, '')
}

if (!await FsUtil.CanReadWrite(envFilePath)) {
//envFile正常是可以编辑的,考虑到own变成root的情况
await Command.sudoExec(`chown ${userName}:staff ${envFilePath}`)
}

text = FileUtil.ReadAllText(envFilePath);
text = await FileUtil.ReadAll(envFilePath);

if (enable) {
let binPath = GetPath.getBinDir();
Expand All @@ -39,11 +39,11 @@ export default class EnvMacOS {
if (text.slice(-1) !== '\n') {
appendText = `\n${appendText}`
}
await FileUtil.AppendAllText(envFilePath, appendText);
await FileUtil.Append(envFilePath, appendText);
} else {
let regx = new RegExp(`export\\s+PATH.+${APP_NAME}.+`, 'g');
text = text.replaceAll(regx, '');
FileUtil.WriteAllText(envFilePath, text);
await FileUtil.WriteAll(envFilePath, text);
}
}

Expand Down
Loading

0 comments on commit 1f240fa

Please sign in to comment.