更新依赖并增强应用管理命令功能
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { AssistantApp } from '@/module/assistant/index.ts';
|
||||
import { program, Command, assistantConfig } from '@/program.ts';
|
||||
import { AppDownload } from '@/services/app/index.ts';
|
||||
import { table } from 'table';
|
||||
|
||||
const appManagerCommand = new Command('app-manager').alias('am').description('Manage Assistant Apps 管理本地的应用模块');
|
||||
program.addCommand(appManagerCommand);
|
||||
@@ -8,11 +9,43 @@ program.addCommand(appManagerCommand);
|
||||
appManagerCommand
|
||||
.command('list')
|
||||
.description('List all installed apps')
|
||||
.action(async () => {
|
||||
.option('-s, --status <status>', '列出状态信息, 可选值: running, stopped, inactive')
|
||||
.option('-w, --wide', '显示更多信息')
|
||||
.action(async (opts) => {
|
||||
const manager = new AssistantApp(assistantConfig);
|
||||
await manager.loadConfig();
|
||||
const showInfos = manager.getAllAppShowInfo();
|
||||
console.log('Installed Apps:', showInfos);
|
||||
let showInfos = manager.getAllAppShowInfo();
|
||||
const isWide = opts.wide ?? false;
|
||||
let header = [];
|
||||
if (!isWide) {
|
||||
showInfos = showInfos.map((item) => {
|
||||
return { key: item.key, status: item.status };
|
||||
});
|
||||
header = ['Key', 'Status'];
|
||||
}
|
||||
if (opts.status) {
|
||||
const showList = showInfos.filter(info => info.status === opts.status);
|
||||
if (showList.length === 0) {
|
||||
console.log(`No apps with status: ${opts.status}`);
|
||||
return;
|
||||
}
|
||||
const teables = showList.map(item => Object.values(item));
|
||||
teables.unshift(header);
|
||||
console.log('App Start Info:\n')
|
||||
console.log(table(teables));
|
||||
return
|
||||
}
|
||||
if (showInfos.length === 0) {
|
||||
console.log('No installed apps found.');
|
||||
return;
|
||||
}
|
||||
header = Object.keys(showInfos[0]);;
|
||||
const teables = showInfos.map(item => Object.values(item));
|
||||
teables.unshift(header);
|
||||
|
||||
console.log('Installed Apps:\n')
|
||||
console.log(table(teables));
|
||||
|
||||
});
|
||||
|
||||
appManagerCommand
|
||||
|
||||
@@ -1,15 +1,69 @@
|
||||
import { program, Command, assistantConfig } from '@/program.ts';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import fs from 'node:fs';
|
||||
import { AssistantApp, checkFileExists } from '@/lib.ts';
|
||||
import { logger } from '@/module/logger.ts';
|
||||
import { LoadApp, StopApp } from '@/module/local-apps/src/modules/manager.ts';
|
||||
|
||||
const runScriptsCommand = new Command('run-scripts')
|
||||
.alias('run')
|
||||
.arguments('<cmd> [env]')
|
||||
.option('-l --local', '使用当前文件夹的package.json中的scripts', false)
|
||||
.description('运行脚本,在assistant.config.json中配置的脚本')
|
||||
.action(async (cmd, env) => {
|
||||
.action(async (cmd, env, opts) => {
|
||||
const useLocal = opts.local;
|
||||
const showScripts = cmd === 'show';
|
||||
const showScriptFunc = (scripts: any) => {
|
||||
console.log('可用的本地脚本:');
|
||||
let has = false;
|
||||
Object.keys(scripts).forEach((key) => {
|
||||
console.log(`- ${key}: ${scripts[key]}`);
|
||||
has = true;
|
||||
});
|
||||
if (!has) {
|
||||
console.log('当前未定义任何脚本。');
|
||||
}
|
||||
}
|
||||
if (useLocal) {
|
||||
const pkgPath = path.join(process.cwd(), 'package.json');
|
||||
if (checkFileExists(pkgPath) === false) {
|
||||
console.error('当前目录下未找到 package.json 文件。');
|
||||
return;
|
||||
}
|
||||
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
||||
const scripts = pkg.scripts || {};
|
||||
if (showScripts) {
|
||||
showScriptFunc(scripts);
|
||||
return;
|
||||
}
|
||||
const script = scripts[cmd];
|
||||
if (!script) {
|
||||
console.error(`Script "${cmd}" not found in local package.json.`);
|
||||
return;
|
||||
}
|
||||
const command = [script, ...(env ? [env] : [])].join(' ');
|
||||
const res = spawnSync(command, { shell: true, stdio: 'inherit', cwd: assistantConfig.configDir });
|
||||
console.log(`执行 "[${command}]"...`);
|
||||
if (res.error) {
|
||||
console.error(`执行失败 "${cmd}":`, res.error);
|
||||
return;
|
||||
}
|
||||
if (res.status !== 0) {
|
||||
console.error(`本地脚本 "${cmd}" 以代码 ${res.status} 退出`);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
assistantConfig.checkMounted();
|
||||
const configs = assistantConfig.getCacheAssistantConfig();
|
||||
const scripts = configs?.scripts || {};
|
||||
try {
|
||||
const script = scripts[cmd];
|
||||
if (showScripts) {
|
||||
showScriptFunc(scripts);
|
||||
return;
|
||||
}
|
||||
if (!script) {
|
||||
console.error(`Script "${cmd}" not found.`);
|
||||
return;
|
||||
@@ -32,3 +86,93 @@ const runScriptsCommand = new Command('run-scripts')
|
||||
}
|
||||
});
|
||||
program.addCommand(runScriptsCommand);
|
||||
|
||||
const createRandomApp = (opts: { app: any, package: any, pwd: string, status?: string }) => {
|
||||
const { app, package: packageJson, pwd } = opts;
|
||||
if (!app.status) {
|
||||
app.status = opts.status || 'running'
|
||||
}
|
||||
if (!app.key) {
|
||||
const randomSuffix = Math.random().toString(36).substring(2, 8);
|
||||
app.key = packageJson.basename || `${'unknown-app'}-${randomSuffix}`;
|
||||
}
|
||||
app.path = pwd;
|
||||
if (app.type === 'pm2-system-app' && !app.pm2Options) {
|
||||
app.pm2Options = {
|
||||
cwd: pwd,
|
||||
}
|
||||
}
|
||||
return app;
|
||||
}
|
||||
const start = new Command('start')
|
||||
.description('获取package.json中app参数并启动对应的app')
|
||||
.option('-s --save', '保存应用信息到assistant配置中', false)
|
||||
.action(async (opts) => {
|
||||
// assistantConfig.checkMounted();
|
||||
const pwd = process.cwd();
|
||||
const packageJsonPath = path.join(pwd, 'package.json');
|
||||
if (checkFileExists(packageJsonPath) === false) {
|
||||
logger.error('package.json 在当前目录未找到,请在包含 package.json 的目录下运行此命令。');
|
||||
return
|
||||
}
|
||||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
||||
const appKey = packageJson.app;
|
||||
if (!appKey) {
|
||||
logger.error('package.json 中未找到 app 字段,请确保在 package.json 中正确配置 app 字段。');
|
||||
return
|
||||
}
|
||||
const app = createRandomApp({ app: packageJson.app, package: packageJson, pwd });
|
||||
if (app.type !== 'system-app') {
|
||||
const load = await LoadApp(app, {});
|
||||
if (!load) {
|
||||
logger.error(`未能加载应用, 请确保应用名称正确且已安装。`, app.type);
|
||||
return
|
||||
}
|
||||
} else {
|
||||
LoadApp(app, {}).then(() => {
|
||||
logger.info(`系统应用已启动: ${app.key}`);
|
||||
}).catch((err) => {
|
||||
logger.error(`启动系统应用失败: ${app.key}`, err);
|
||||
});
|
||||
}
|
||||
|
||||
if (opts.save) {
|
||||
assistantConfig.checkMounted();
|
||||
const manager = new AssistantApp(assistantConfig, app);
|
||||
await manager.loadConfig();
|
||||
await manager.saveAppInfo(app);
|
||||
}
|
||||
});
|
||||
|
||||
program.addCommand(start);
|
||||
|
||||
const stop = new Command('stop')
|
||||
.description('获取package.json中app参数并停止对应的app')
|
||||
.option('-t --todo <todo>', '停止应用,在pm2中如果为stop则停止,如果为remove则删除,默认为stop', 'stop')
|
||||
.option('-s --save', '保存应用信息到assistant配置中', false)
|
||||
.action(async (opts) => {
|
||||
// assistantConfig.checkMounted();
|
||||
const pwd = process.cwd();
|
||||
const packageJsonPath = path.join(pwd, 'package.json');
|
||||
if (checkFileExists(packageJsonPath) === false) {
|
||||
logger.error('package.json 在当前目录未找到,请在包含 package.json 的目录下运行此命令。');
|
||||
return
|
||||
}
|
||||
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
|
||||
const appKey = packageJson.app;
|
||||
if (!appKey) {
|
||||
logger.error('package.json 中未找到 app 字段,请确保在 package.json 中正确配置 app 字段。');
|
||||
return
|
||||
}
|
||||
const app = createRandomApp({ app: packageJson.app, package: packageJson, pwd, status: 'stopped' });
|
||||
await StopApp(app, { todo: opts.todo });
|
||||
if (opts.save) {
|
||||
assistantConfig.checkMounted();
|
||||
const manager = new AssistantApp(assistantConfig, app);
|
||||
await manager.loadConfig();
|
||||
await manager.removeApp(app.key, { deleteFile: false });
|
||||
}
|
||||
});
|
||||
|
||||
program.addCommand(stop);
|
||||
|
||||
|
||||
@@ -40,7 +40,3 @@ if (DEV_SERVER) {
|
||||
});
|
||||
loadManager({ configFilename: 'b.json' });
|
||||
}
|
||||
|
||||
export const loadApp = (mainApp: App, appInfo?: any) => {
|
||||
//
|
||||
};
|
||||
|
||||
@@ -53,6 +53,7 @@ export type AppInfo = {
|
||||
path?: string; // 文件路径
|
||||
env?: Record<string, any>; // 环境变量
|
||||
engine?: string; // runtime, python node deno bun etc
|
||||
init?: boolean; // 是否需要初始化安装npm等依赖
|
||||
/**
|
||||
* pm2 选项, 仅仅当是AppType.Pm2SystemApp的时候生效
|
||||
* pm2 选项可以参考 https://pm2.keymetrics.io/docs/usage/application-declaration/
|
||||
@@ -125,71 +126,7 @@ export class Manager<T extends AppInfo = AppInfo> {
|
||||
async loadApp(app: T) {
|
||||
const mainApp = this.mainApp;
|
||||
this.apps.set(app.key, app);
|
||||
if (app.status !== 'running') {
|
||||
return;
|
||||
}
|
||||
if (!fileIsExist(app.path)) {
|
||||
console.error('app is not found');
|
||||
return;
|
||||
}
|
||||
const pathEntry = path.join(app.path, app.entry);
|
||||
if (!fileIsExist(pathEntry)) {
|
||||
console.error('file entry not found');
|
||||
return;
|
||||
}
|
||||
const entry = app.entry + `?timestamp=${app?.timestamp}`;
|
||||
// 注册路由
|
||||
if (app.type === AppType.MicroApp) {
|
||||
const childProcess = fork(app.entry, [], {
|
||||
stdio: 'inherit', // 共享主进程的标准输入输出
|
||||
cwd: app.path,
|
||||
env: {
|
||||
...process.env,
|
||||
...app.env,
|
||||
APP_KEY: app.key,
|
||||
APP_PATH: app.path,
|
||||
APP_ENTRY: entry
|
||||
}
|
||||
});
|
||||
app.process = childProcess;
|
||||
} else if (app.type === AppType.SystemApp) {
|
||||
const pathEntryAndTimestamp = path.join(app.path, entry);
|
||||
// Windows下需要使用file://协议,并将反斜杠转换为正斜杠
|
||||
const importPath = process.platform === 'win32'
|
||||
? 'file:///' + pathEntryAndTimestamp.replace(/\\/g, '/')
|
||||
: pathEntryAndTimestamp;
|
||||
const module = await import(importPath);
|
||||
if (module.loadApp && mainApp) {
|
||||
await module.loadApp?.(mainApp, app);
|
||||
}
|
||||
} else if (app.type === AppType.GatewayApp) {
|
||||
console.log('gateway app not support');
|
||||
} else if (app.type === AppType.Pm2SystemApp) {
|
||||
const pathEntry = path.join(app.path, app.entry);
|
||||
const pm2Manager = new Pm2Manager({
|
||||
appName: app.key,
|
||||
script: pathEntry,
|
||||
pm2Connect: this.#pm2Connect
|
||||
});
|
||||
|
||||
// const isInstall = await checkInstall(app);
|
||||
// if (!isInstall) {
|
||||
// console.log('install failed');
|
||||
// return;
|
||||
// }
|
||||
const pm2Options: StartOptions = app.pm2Options || {};
|
||||
if (app?.engine) {
|
||||
pm2Options.interpreter = pm2Options.interpreter || app?.engine;
|
||||
}
|
||||
if (!pm2Options.cwd) {
|
||||
pm2Options.cwd = path.join(app.path, '../..');
|
||||
}
|
||||
await pm2Manager.start(pm2Options);
|
||||
} else {
|
||||
console.error('app type not support', app.type);
|
||||
}
|
||||
console.log(`load ${app.type} success`, app.key);
|
||||
return true;
|
||||
return await LoadApp(app, { mainApp, pm2Connect: this.#pm2Connect });
|
||||
}
|
||||
/**
|
||||
* create new app info
|
||||
@@ -308,24 +245,7 @@ export class Manager<T extends AppInfo = AppInfo> {
|
||||
if (!app) {
|
||||
return;
|
||||
}
|
||||
if (app.status === 'stop' && app.type === AppType.SystemApp) {
|
||||
console.log(`app ${key} is stopped`);
|
||||
return;
|
||||
}
|
||||
app.status = 'stop';
|
||||
if (app.type === AppType.MicroApp) {
|
||||
if (app.process) {
|
||||
app.process.kill();
|
||||
}
|
||||
}
|
||||
if (app.type === AppType.Pm2SystemApp) {
|
||||
const pm2Manager = new Pm2Manager({
|
||||
appName: app.key,
|
||||
script: app.entry,
|
||||
pm2Connect: this.#pm2Connect
|
||||
});
|
||||
await pm2Manager.stop();
|
||||
}
|
||||
await StopApp(app, { pm2Connect: this.#pm2Connect, todo: 'stop' });
|
||||
await this.saveAppInfo(app);
|
||||
}
|
||||
async restart(key: string) {
|
||||
@@ -393,8 +313,9 @@ export class Manager<T extends AppInfo = AppInfo> {
|
||||
* @param key
|
||||
* @returns
|
||||
*/
|
||||
async removeApp(key: string) {
|
||||
async removeApp(key: string, opts?: { deleteFile?: boolean }) {
|
||||
const app = this.apps.get(key);
|
||||
const deleteFile = opts?.deleteFile ?? true;
|
||||
if (!app) {
|
||||
return false;
|
||||
}
|
||||
@@ -414,6 +335,9 @@ export class Manager<T extends AppInfo = AppInfo> {
|
||||
} catch (e) {
|
||||
console.log('delete pm2 process error', e);
|
||||
}
|
||||
if (!deleteFile) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
deleteFileAppInfo(key, this.appsPath);
|
||||
} catch (e) {
|
||||
@@ -485,7 +409,105 @@ export class Manager<T extends AppInfo = AppInfo> {
|
||||
}
|
||||
}
|
||||
}
|
||||
export const LoadApp = async (app: AppInfo, opts?: { mainApp?: any, pm2Connect?: any }) => {
|
||||
const mainApp = opts?.mainApp;
|
||||
const pm2Connect = opts?.pm2Connect;
|
||||
if (app.status !== 'running') {
|
||||
return false;
|
||||
}
|
||||
if (!fileIsExist(app.path)) {
|
||||
console.error('app is not found');
|
||||
return false;
|
||||
}
|
||||
const pathEntry = path.join(app.path, app.entry);
|
||||
if (!fileIsExist(pathEntry)) {
|
||||
console.error('file entry not found');
|
||||
return false;
|
||||
}
|
||||
const entry = app.entry + `?timestamp=${app?.timestamp}`;
|
||||
// 注册路由
|
||||
if (app.type === AppType.MicroApp) {
|
||||
const childProcess = fork(app.entry, [], {
|
||||
stdio: 'inherit', // 共享主进程的标准输入输出
|
||||
cwd: app.path,
|
||||
env: {
|
||||
...process.env,
|
||||
...app.env,
|
||||
APP_KEY: app.key,
|
||||
APP_PATH: app.path,
|
||||
APP_ENTRY: entry
|
||||
}
|
||||
});
|
||||
app.process = childProcess;
|
||||
} else if (app.type === AppType.SystemApp) {
|
||||
const pathEntryAndTimestamp = path.join(app.path, entry);
|
||||
// Windows下需要使用file://协议,并将反斜杠转换为正斜杠
|
||||
const importPath = process.platform === 'win32'
|
||||
? 'file:///' + pathEntryAndTimestamp.replace(/\\/g, '/')
|
||||
: pathEntryAndTimestamp;
|
||||
const module = await import(importPath);
|
||||
if (module.loadApp && mainApp) {
|
||||
await module.loadApp?.(mainApp, app);
|
||||
}
|
||||
} else if (app.type === AppType.GatewayApp) {
|
||||
console.log('gateway app not support');
|
||||
} else if (app.type === AppType.Pm2SystemApp) {
|
||||
const pathEntry = path.join(app.path, app.entry);
|
||||
console.log('pm2 system app start', pathEntry);
|
||||
const pm2Manager = new Pm2Manager({
|
||||
appName: app.key,
|
||||
script: pathEntry,
|
||||
pm2Connect: pm2Connect
|
||||
});
|
||||
if (app?.init) {
|
||||
const isInstall = await checkInstall(app);
|
||||
if (!isInstall) {
|
||||
console.log('install failed');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
const pm2Options: StartOptions = app.pm2Options || {};
|
||||
if (app?.engine) {
|
||||
pm2Options.interpreter = pm2Options.interpreter || app?.engine;
|
||||
}
|
||||
if (!pm2Options.cwd) {
|
||||
pm2Options.cwd = path.join(app.path, '../..');
|
||||
}
|
||||
console.log('pm2 start options', pm2Options);
|
||||
await pm2Manager.start(pm2Options);
|
||||
} else if (app.type === AppType.ScriptApp) {
|
||||
// console.log('script app 直接运行,不需要启动');
|
||||
return true;
|
||||
} else {
|
||||
console.error('app type not support', app.type);
|
||||
}
|
||||
console.log(`load ${app.type} success`, app.key);
|
||||
return true;
|
||||
}
|
||||
|
||||
export const StopApp = async (app: AppInfo, opts?: { pm2Connect?: Pm2Connect, todo?: 'stop' | 'remove' | 'restart' }) => {
|
||||
const key = app.key;
|
||||
const pm2Connect = opts?.pm2Connect;
|
||||
const todo = opts?.todo || 'stop';
|
||||
if (app.status === 'stop' && app.type === AppType.SystemApp) {
|
||||
console.log(`app ${key} is stopped`);
|
||||
return;
|
||||
}
|
||||
app.status = 'stop';
|
||||
if (app.type === AppType.MicroApp) {
|
||||
if (app.process) {
|
||||
app.process.kill();
|
||||
}
|
||||
}
|
||||
if (app.type === AppType.Pm2SystemApp) {
|
||||
const pm2Manager = new Pm2Manager({
|
||||
appName: app.key,
|
||||
script: app.entry,
|
||||
pm2Connect: pm2Connect
|
||||
});
|
||||
await pm2Manager[todo]?.();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 安装app通过key
|
||||
* @param key
|
||||
|
||||
@@ -108,6 +108,9 @@ export class Pm2Connect {
|
||||
}
|
||||
}
|
||||
type RunOptions = {
|
||||
/**
|
||||
* 是否在操作完成后退出连接
|
||||
*/
|
||||
needExit?: boolean;
|
||||
};
|
||||
export class Pm2Manager {
|
||||
@@ -226,6 +229,9 @@ export class Pm2Manager {
|
||||
this.pm2Connect.checkDisconnect(runOpts);
|
||||
}
|
||||
}
|
||||
async remove(runOpts?: RunOptions) {
|
||||
this.deleteProcess(runOpts);
|
||||
}
|
||||
async deleteProcess(runOpts?: RunOptions) {
|
||||
try {
|
||||
await this.pm2Connect.checkConnect();
|
||||
|
||||
Reference in New Issue
Block a user