2024-12-03 23:22:39 +08:00

56 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fileIsExist } from '@kevisual/use-config';
import { useFileStore } from '@abearxiong/use-file-store';
import fs from 'fs';
import path from 'path';
export const appsPath = useFileStore('apps', { needExists: true });
export const loadFileAppInfo = async (key: string) => {
const directory = path.join(appsPath, key);
if (!fileIsExist(directory)) {
throw new Error('app not found');
}
const pkgs = path.join(directory, 'package.json');
if (!fileIsExist(pkgs)) {
throw new Error('Invalid package.json');
}
const json = fs.readFileSync(pkgs, 'utf-8');
const pkg = JSON.parse(json);
const { name, version, app } = pkg;
if (!name || !version || !app) {
throw new Error('Invalid package.json');
}
const mainEntry = path.join(directory, app.entry);
if (!fileIsExist(mainEntry)) {
throw new Error('Invalid main entry');
}
return { mainEntry, app };
};
export const deleteFileAppInfo = async (key: string) => {
const directory = path.join(appsPath, key);
if (!fileIsExist(directory)) {
return;
}
fs.rmSync(directory, { recursive: true });
};
/**
* @deprecated
* @param key
* @returns
*/
export const loadApp = async (key: string) => {
const { mainEntry, app } = await loadFileAppInfo(key);
// 1. 查询数据库获取app信息查看是否运行中
// 2. 如果运行中,直接返回
// 3. 如果不在运行中加载app
// 3.1 查看app的类型如果是 system-app直接加载
// 3.2 如果是 micro-app查找相关的依赖如果依赖不存在先加载依赖
// 3.3 使用fork加载app
// 4. 记录app的运行状态程序重新启动时重新加载
const main = await import(mainEntry);
return main;
};