Feat: Optimize the format

This commit is contained in:
anmengweilai 2024-01-16 15:06:24 +08:00
parent 6d1cc467ad
commit c103a397e8
10 changed files with 125 additions and 68 deletions

View File

@ -1,13 +1,12 @@
import {DefAlasConfig} from '@alas/common';
import {ALAS_INSTR_FILE} from '@alas/common';
import {ALAS_INSTR_FILE, DefAlasConfig} from '@alas/common';
import {validateConfigFile} from '@/utils/validate';
import {join} from 'path';
import path, {join} from 'path';
import {logger} from '@/core/Logger/customLogger';
import yaml from 'yaml';
import fs from 'fs';
import path from 'path';
import {getScriptRootPath} from '@/utils/getScriptRootPath';
import {is} from '@electron-toolkit/utils';
// function getAlasPath() {
// let file;
@ -48,38 +47,89 @@ import {getScriptRootPath} from '@/utils/getScriptRootPath';
// return path.join(alasPath, './Alas.exe');
// }
export const alasPath = getScriptRootPath();
// export const alasPath = getScriptRootPath();
try {
validateConfigFile(join(alasPath, '/config'));
} catch (e) {
logger.error(String(e as any));
}
// try {
// validateConfigFile(join(alasPath, '/config'));
// } catch (e) {
// logger.error(String(e as any));
// }
const file = fs.readFileSync(path.join(alasPath, './config/deploy.yaml'), 'utf8');
const config = yaml.parse(file) as DefAlasConfig;
const PythonExecutable = config.Deploy.Python.PythonExecutable;
const WebuiPort = config.Deploy.Webui.WebuiPort.toString();
const Theme = config.Deploy.Webui.Theme;
// const file = fs.readFileSync(path.join(alasPath, './config/deploy.yaml'), 'utf8');
// const config = yaml.parse(file) as DefAlasConfig;
// const PythonExecutable = config.Deploy.Python.PythonExecutable;
// const WebuiPort = config.Deploy.Webui.WebuiPort.toString();
// const Theme = config.Deploy.Webui.Theme;
export const ThemeObj: {[k in string]: 'light' | 'dark'} = {
default: 'light',
light: 'light',
dark: 'dark',
system: 'light',
};
// export const ThemeObj: {[k in string]: 'light' | 'dark'} = {
// default: 'light',
// light: 'light',
// dark: 'dark',
// system: 'light',
// };
export const pythonPath = path.isAbsolute(PythonExecutable)
? PythonExecutable
: path.join(alasPath, PythonExecutable);
export const installerPath = ALAS_INSTR_FILE;
export const installerArgs = import.meta.env.DEV ? ['--print-test'] : [];
export const webuiUrl = `http://127.0.0.1:${WebuiPort}`;
export const webuiPath = 'gui.py';
export const webuiArgs = ['--port', WebuiPort, '--electron'];
export const dpiScaling =
Boolean(config.Deploy.Webui.DpiScaling) || config.Deploy.Webui.DpiScaling === undefined;
// export const pythonPath = path.isAbsolute(PythonExecutable)
// ? PythonExecutable
// : path.join(alasPath, PythonExecutable);
// export const installerPath = ALAS_INSTR_FILE;
// export const installerArgs = import.meta.env.DEV ? ['--print-test'] : [];
// export const webuiUrl = `http://127.0.0.1:${WebuiPort}`;
// export const webuiPath = 'gui.py';
// export const webuiArgs = ['--port', WebuiPort, '--electron'];
// export const dpiScaling =
// Boolean(config.Deploy.Webui.DpiScaling) || config.Deploy.Webui.DpiScaling === undefined;
export const webuiTheme = ThemeObj[Theme] || 'light';
// export const webuiTheme = ThemeObj[Theme] || 'light';
//
// export const noSandbox = config.Deploy.Webui.NoSandbox;
export const noSandbox = config.Deploy.Webui.NoSandbox;
let configInfo = (() => {
const alasPath = getScriptRootPath();
try {
validateConfigFile(join(alasPath, '/config'));
} catch (e) {
logger.error(String(e as any));
}
const file = fs.readFileSync(path.join(alasPath, './config/deploy.yaml'), 'utf8');
const config = yaml.parse(file) as DefAlasConfig;
const PythonExecutable = config.Deploy.Python.PythonExecutable;
const WebuiPort = config.Deploy.Webui.WebuiPort.toString();
const Theme = config.Deploy.Webui.Theme;
const ThemeObj: {[k in string]: 'light' | 'dark'} = {
default: 'light',
light: 'light',
dark: 'dark',
system: 'light',
};
const pythonPath = path.isAbsolute(PythonExecutable)
? PythonExecutable
: path.join(alasPath, PythonExecutable);
const installerPath = ALAS_INSTR_FILE;
const installerArgs = is.dev ? ['--print-test'] : [];
const webuiUrl = `http://127.0.0.1:${WebuiPort}`;
const webuiPath = 'gui.py';
const webuiArgs = ['--port', WebuiPort, '--electron'];
const dpiScaling =
Boolean(config.Deploy.Webui.DpiScaling) || config.Deploy.Webui.DpiScaling === undefined;
const webuiTheme = ThemeObj[Theme] || 'light';
const noSandbox = config.Deploy.Webui.NoSandbox;
return {
alasPath,
installerPath,
pythonPath,
installerArgs,
webuiUrl,
webuiPath,
webuiArgs,
dpiScaling,
webuiTheme,
noSandbox,
};
})();
export default configInfo;

View File

@ -11,8 +11,10 @@ import {getAlasConfig} from '@/utils/alasConfig';
import type {AlasConfig} from '@alas/common';
import {isDev} from '@alas/common';
import * as browserItems from '../browserItems';
import configInfo from '@/config';
const {dpiScaling} = configInfo;
// eslint-disable-next-line
const importAll = (r: any) => Object.values(r).map((v: any) => v.default);
export type ServiceMap = Map<string, any>;
@ -44,13 +46,16 @@ export class App extends EventEmitter {
config: Partial<AlasConfig> = {};
/**
* alas
*
*/
scriptManager: Map<string, PyShell> = new Map();
constructor() {
super();
// 日志系统
this.logger = new Logger();
const services: TServiceModule[] = importAll(import.meta.globEager('../services/*Service.ts'));
services.forEach(service => this.addService(service));
@ -76,13 +81,11 @@ export class App extends EventEmitter {
// 启动窗口管理器
this.browserManager = new BrowserManager(this);
// 日志系统
this.logger = new Logger();
}
onActivate = () => {
this.browserManager.browsers.get('index')!.show();
const [key] = this.browserManager.browsers.keys();
this.browserManager.browsers.get(key)!.show();
};
beforeQuit = () => {
@ -94,11 +97,8 @@ export class App extends EventEmitter {
this.browserManager.browsers.forEach(browser => {
browser?.destroy();
});
/**
* alas服务
*/
};
/**
* app
*/
@ -128,7 +128,6 @@ export class App extends EventEmitter {
/**
*
*/
initBrowsers() {
Object.values(browserItems).forEach(item => {
@ -185,7 +184,7 @@ export class App extends EventEmitter {
app.commandLine.appendSwitch('no-sandbox');
// No DPI scaling
if (!this.config.dpiScaling) {
if (!dpiScaling) {
app.commandLine.appendSwitch('high-dpi-support', '1');
app.commandLine.appendSwitch('force-device-scale-factor', '1');
}

View File

@ -2,7 +2,15 @@ import type {App} from '@/core/App';
import type {BrowserWindowsIdentifier, MainEvents} from '@alas/common';
import {isDev, isMacOS} from '@alas/common';
import type {BrowserWindowConstructorOptions} from 'electron';
import {BrowserWindow, Menu, Tray, app, globalShortcut, nativeImage, protocol} from 'electron';
import {
BrowserWindow,
Menu,
Tray,
globalShortcut,
app as electronApp,
nativeImage,
protocol,
} from 'electron';
// import installer /* ,{VUEJS3_DEVTOOLS} */ from 'electron-devtools-installer';
import EventEmitter from 'events';
import {join} from 'path';
@ -95,7 +103,7 @@ export default class Browser extends EventEmitter {
*
*/
loadTray = () => {
const {browserWindow} = this;
const {browserWindow, app} = this;
Menu.setApplicationMenu(null);
const icon = nativeImage.createFromPath(join(__dirname, './icon.png'));
const dockerIcon = icon.resize({width: 16, height: 16});
@ -103,23 +111,20 @@ export default class Browser extends EventEmitter {
const contextMenu = Menu.buildFromTemplate([
{
label: 'Show',
click: function () {
click: () => {
browserWindow?.show();
},
},
{
label: 'Hide',
click: function () {
click: () => {
browserWindow?.hide();
},
},
{
label: 'Exit',
click: function () {
/**
* alasService
*/
app.quit();
click: () => {
app.destroy();
process.exit(0);
},
},
@ -149,7 +154,7 @@ export default class Browser extends EventEmitter {
// 生产环境直接结束
if (!(isDev || process.env.DEBUG === '1')) return;
app.whenReady().then(() => {
electronApp.whenReady().then(() => {
/**
* vue devtools VUEJS3_DEVTOOLS id改换会出现没法正确安装的情况,id
*/
@ -235,7 +240,6 @@ export default class Browser extends EventEmitter {
// https://www.electronjs.org/docs/tutorial/context-isolation
contextIsolation: true,
// devTools: isDev,
// preload: '../preload/index.js',
preload: join(__dirname, '../preload/index.js'),
},
});

View File

@ -1,6 +1,7 @@
import {logAfter, logBefore} from '@/core/Logger';
/**
*
* service
*/
export class ServiceStorage {

View File

@ -1,5 +1,6 @@
import './security-restrictions';
import {App} from './core/App';
import {logger} from '@/core/Logger/customLogger';
new App().bootstrap().catch(e => {
console.error(e);
logger.error(e);
});

View File

@ -1,8 +1,10 @@
import {alasPath, pythonPath} from '@/config';
import configInfo from '@/config';
import {logger} from '@/core/Logger/customLogger';
import {Options, PythonShell} from 'python-shell';
import treeKill from 'tree-kill';
const {alasPath, pythonPath} = configInfo;
export class PyShell extends PythonShell {
constructor(script: string, args: Array<string> = []) {
const options: Options = {

View File

@ -1,8 +1,10 @@
import {installerArgs, installerPath} from '@/config';
import configInfo from '@/config';
import {PyShell} from '@/pyshell';
import {ServiceModule, event} from '@/services/index';
import {ALAS_RELAUNCH_ARGV} from '@alas/common';
const {installerArgs, installerPath} = configInfo;
export default class ScriptService extends ServiceModule {
@event('/script/start-alas-server')
startScriptServer() {

View File

@ -2,16 +2,16 @@ import {ThemeObj, ALAS_CONFIG_YAML} from '@alas/common';
import type {Dirent} from 'fs';
import type {AlasConfig, DefAlasConfig} from '@alas/common';
import {checkIsFirst} from '@/utils/checkIsFirst';
import {webuiArgs, webuiPath} from '@/config';
import configInfo from '@/config';
import path from 'path';
import yaml from 'yaml';
import fs from 'fs';
import {getScriptRootPath} from '@/utils/getScriptRootPath';
const {webuiArgs, webuiPath, alasPath} = configInfo;
let alasConfig: AlasConfig | null = null;
export async function getAlasConfig() {
if (alasConfig === null) {
const alasPath = getScriptRootPath('/config/deploy.template.yaml');
const file = fs.readFileSync(path.join(alasPath, `./config/${ALAS_CONFIG_YAML}`), 'utf8');
const config = yaml.parse(file) as DefAlasConfig;
const WebuiPort = config.Deploy.Webui.WebuiPort.toString();
@ -40,7 +40,6 @@ interface fileInfoItem {
lastModifyTime: Date;
}
export function getAlasConfigDirFiles() {
const alasPath = getScriptRootPath('/config/deploy.template.yaml');
const configPath = path.join(alasPath, './config');
const files: Dirent[] = fs.readdirSync(configPath, {withFileTypes: true});
const filesInfoList: fileInfoItem[] = files.map((file: Dirent) => {

View File

@ -5,11 +5,8 @@
* @return {function}
*/
export const createLogProxy =
(logLevel: string, mainLogger: any) =>
(
// eslint-disable-next-line @typescript-eslint/ban-types
fn: Function,
) =>
(logLevel: string, mainLogger: any): Function =>
(fn: Function) =>
(...args: any) => {
fn(...args);
mainLogger[logLevel](...args);

View File

@ -12,7 +12,9 @@ export function modifyYaml(filePath: string, keyObj: {[k in string]: never}) {
const keysMap = new Map(Object.entries(keyObj));
visit(doc, {
Pair: (_node, pair: Pair) => {
// @ts-ignore
if (keysMap.has(pair?.key?.value)) {
// @ts-ignore
pair.value.value = keysMap.get(pair.key.value);
}
},