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; };