75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
const path = require('path');
|
|
|
|
// 动态获取 scripts 目录路径
|
|
function getScriptsPath(scriptName) {
|
|
// 开发环境
|
|
const devPath = path.join(__dirname, '../../scripts', scriptName);
|
|
// 生产环境(打包后)
|
|
const prodPath = path.join(process.resourcesPath, 'scripts', scriptName);
|
|
|
|
try {
|
|
// 先尝试开发环境路径
|
|
require.resolve(devPath);
|
|
return devPath;
|
|
} catch (e) {
|
|
// 如果开发环境路径不存在,使用生产环境路径
|
|
return prodPath;
|
|
}
|
|
}
|
|
|
|
// 延迟加载 MySQLManager
|
|
let MySQLManager = null;
|
|
function getMySQLManager() {
|
|
if (!MySQLManager) {
|
|
MySQLManager = require(getScriptsPath('start-mysql'));
|
|
}
|
|
return MySQLManager;
|
|
}
|
|
|
|
class MySQLController {
|
|
|
|
/**
|
|
* 启动MySQL服务
|
|
*/
|
|
async start() {
|
|
try {
|
|
const MySQLManagerClass = getMySQLManager();
|
|
const mysqlManager = new MySQLManagerClass();
|
|
await mysqlManager.start();
|
|
return { success: true, message: 'MySQL started successfully' };
|
|
} catch (error) {
|
|
return { success: false, message: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 停止MySQL服务
|
|
*/
|
|
async stop() {
|
|
try {
|
|
const MySQLManagerClass = getMySQLManager();
|
|
const mysqlManager = new MySQLManagerClass();
|
|
await mysqlManager.stop();
|
|
return { success: true, message: 'MySQL stopped successfully' };
|
|
} catch (error) {
|
|
return { success: false, message: error.message };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取MySQL连接配置
|
|
*/
|
|
async getConnectionConfig() {
|
|
try {
|
|
const MySQLManagerClass = getMySQLManager();
|
|
const mysqlManager = new MySQLManagerClass();
|
|
const config = mysqlManager.getConnectionConfig();
|
|
return { success: true, data: config };
|
|
} catch (error) {
|
|
return { success: false, message: error.message };
|
|
}
|
|
}
|
|
}
|
|
|
|
MySQLController.toString = () => '[class MySQLController]';
|
|
module.exports = MySQLController; |