This commit is contained in:
xion 2025-02-18 17:25:08 +08:00
parent 0144f94edd
commit d9fe68d3fe
4 changed files with 114 additions and 20 deletions

View File

@ -18,7 +18,8 @@
"pub": "npm run build && npm run deploy && npm run reload", "pub": "npm run build && npm run deploy && npm run reload",
"deploy:nova": "rsync -avz --delete ./dist/ --exclude='app.config.json5' nova:~/apps/codeflow/backend", "deploy:nova": "rsync -avz --delete ./dist/ --exclude='app.config.json5' nova:~/apps/codeflow/backend",
"apps:build": "rollup -c rollup.apps.config.mjs", "apps:build": "rollup -c rollup.apps.config.mjs",
"apps:watch": "rollup -c rollup.apps.config.mjs -w" "apps:watch": "rollup -c rollup.apps.config.mjs -w",
"start": "pm2 start dist/app.mjs --name codeflow"
}, },
"keywords": [], "keywords": [],
"types": "types/index.d.ts", "types": "types/index.d.ts",

View File

@ -0,0 +1,35 @@
import { useConfig } from '@kevisual/use-config';
import childProcess from 'child_process';
const config = useConfig<{
appName: string;
}>();
export const selfRestart = async () => {
const appName = config.appName || 'codeflow';
// 检测 pm2 是否安装和是否有 appName 这个应用
try {
const res = childProcess.execSync(`pm2 list`);
const list = res.toString();
if (!list.includes(appName)) {
console.error(`pm2 list not found ${appName}`);
console.log('error', list);
return;
}
} catch (e) {
console.error('pm2 not found');
return;
}
// 执行 pm2 restart appName 命令
return new Promise((resolve, reject) => {
const res = childProcess.exec(`pm2 restart ${appName}`);
res.stdout.on('data', (data) => {
console.log('self-restart:', data);
resolve(data);
});
res.stderr.on('data', (data) => {
console.error('self-restart error:', data);
reject(data);
});
});
};

View File

@ -2,6 +2,7 @@ import { app } from '@/app.ts';
import { MicroAppUploadModel } from './models.ts'; import { MicroAppUploadModel } from './models.ts';
import { appPathCheck, installApp } from './module/install-app.ts'; import { appPathCheck, installApp } from './module/install-app.ts';
import { manager } from './manager-app.ts'; import { manager } from './manager-app.ts';
import { selfRestart } from '@/modules/self-restart.ts';
// 应用上传到 应用管理 的平台 // 应用上传到 应用管理 的平台
app app
@ -21,7 +22,36 @@ app
if (collection?.tags) { if (collection?.tags) {
tags.push(...collection.tags); tags.push(...collection.tags);
} }
const microApp = await MicroAppUploadModel.create({ if (!name) {
ctx.throw(400, 'Invalid file');
}
let microApp = await MicroAppUploadModel.findOne({
where: { title: name, uid },
});
if (microApp) {
await MicroAppUploadModel.update(
{
type: 'micro-app',
tags,
data: {
...microApp.data,
file: {
path,
size,
name,
hash,
},
collection,
},
},
{ where: { title: name, uid } },
);
microApp = await MicroAppUploadModel.findOne({
where: { title: name, uid },
});
console.log('Update micro app', microApp.id);
} else {
microApp = await MicroAppUploadModel.create({
title: name, title: name,
description: collection?.readme || '', description: collection?.readme || '',
type: 'micro-app', type: 'micro-app',
@ -39,6 +69,8 @@ app
share: false, share: false,
uname: username, uname: username,
}); });
}
ctx.body = microApp; ctx.body = microApp;
}) })
.addTo(app); .addTo(app);
@ -55,7 +87,7 @@ app
description: 'Deploy micro app in server', description: 'Deploy micro app in server',
}) })
.define(async (ctx) => { .define(async (ctx) => {
const { id, key } = ctx.query?.data; const { id, key, force } = ctx.query?.data;
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c'; // const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
// const key = 'envision-cli'; // const key = 'envision-cli';
// const id = '7c54a6de-9171-4093-926d-67a035042c6c'; // const id = '7c54a6de-9171-4093-926d-67a035042c6c';
@ -72,7 +104,18 @@ app
console.log('path', path); console.log('path', path);
const check = await appPathCheck({ key }); const check = await appPathCheck({ key });
if (check) { if (check) {
if (!force) {
ctx.throw(400, 'App already exists, please remove it first'); ctx.throw(400, 'App already exists, please remove it first');
} else {
const app = manager.getAppShowInfo(key);
const appType = app?.type;
await manager.removeApp(key);
if (appType === 'system-app') {
// 如果是system-app(主进程运行),就重载
selfRestart();
}
}
} }
const installAppData = await installApp({ path, key }); const installAppData = await installApp({ path, key });
await manager.add(installAppData.showAppInfo); await manager.add(installAppData.showAppInfo);

View File

@ -17,3 +17,18 @@ export { manager };
// console.log('app2 context', global.context); // console.log('app2 context', global.context);
// console.log('app equal', app === ManagerApp); // console.log('app equal', app === ManagerApp);
loadManager(); loadManager();
// middleware: ['auth-admin']
/*
path: 'local-apps',
key: 'detect',
path: 'local-apps',
key: 'list',
path: 'local-apps',
key: 'updateStatus',
path: 'local-apps',
key: 'delete',
*/