feat: add dynamic app
This commit is contained in:
@@ -21,3 +21,5 @@ import './app-manager/index.ts';
|
||||
import './file/index.ts';
|
||||
|
||||
import './packages/index.ts';
|
||||
|
||||
import './micro-app/index.ts';
|
||||
|
||||
1
src/routes/micro-app/index.ts
Normal file
1
src/routes/micro-app/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
import './list.ts';
|
||||
91
src/routes/micro-app/list.ts
Normal file
91
src/routes/micro-app/list.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { app } from '@/app.ts';
|
||||
import { MicroAppModel } from './models.ts';
|
||||
import { appCheck, installApp } from './module/install-app.ts';
|
||||
import { loadApp } from './module/load-app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'micro-app',
|
||||
key: 'upload',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { files, collection } = ctx.query?.data;
|
||||
const { uid, username } = ctx.state.tokenUser;
|
||||
const file = files[0];
|
||||
console.log('File', files);
|
||||
const { path, name, hash, size } = file;
|
||||
const microApp = await MicroAppModel.create({
|
||||
title: name,
|
||||
description: '',
|
||||
type: 'micro-app',
|
||||
tags: [],
|
||||
data: {
|
||||
file: {
|
||||
path,
|
||||
size,
|
||||
name,
|
||||
hash,
|
||||
},
|
||||
collection,
|
||||
},
|
||||
uid,
|
||||
share: false,
|
||||
uname: username,
|
||||
});
|
||||
ctx.body = microApp;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
// curl http://localhost:4002/api/router?path=micro-app&key=deploy
|
||||
app
|
||||
.route({
|
||||
path: 'micro-app',
|
||||
key: 'deploy',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
// const { id, key} = ctx.query?.data;
|
||||
// const id = '10f03411-85fc-4d37-a4d3-e32b15566a6c';
|
||||
// const key = 'envision-cli';
|
||||
const id = '7c54a6de-9171-4093-926d-67a035042c6c';
|
||||
const key = 'mark';
|
||||
if (!id) {
|
||||
ctx.throw(400, 'Invalid id');
|
||||
}
|
||||
const microApp = await MicroAppModel.findByPk(id);
|
||||
const { file } = microApp.data || {};
|
||||
const path = file?.path;
|
||||
if (!path) {
|
||||
ctx.throw(404, 'Invalid path');
|
||||
}
|
||||
console.log('path', path);
|
||||
const check = await appCheck({ key });
|
||||
if (check) {
|
||||
ctx.throw(400, 'App already exists, please remove it first');
|
||||
}
|
||||
await installApp({ path, key });
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
// curl http://localhost:4002/api/router?path=micro-app&key=load
|
||||
app
|
||||
.route({
|
||||
path: 'micro-app',
|
||||
key: 'load',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
// const { key } = ctx.query?.data;
|
||||
const key = 'mark';
|
||||
try {
|
||||
const main = await loadApp(key);
|
||||
if (main?.loadApp) {
|
||||
await main.loadApp(app);
|
||||
ctx.body = 'success';
|
||||
return;
|
||||
}
|
||||
ctx.throw(400, 'Invalid app');
|
||||
} catch (e) {
|
||||
ctx.throw(400, e.message);
|
||||
}
|
||||
})
|
||||
.addTo(app);
|
||||
84
src/routes/micro-app/models.ts
Normal file
84
src/routes/micro-app/models.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { sequelize } from '@/modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
|
||||
export type MicroApp = Partial<InstanceType<typeof MicroAppModel>>;
|
||||
|
||||
type MicroAppData = {
|
||||
file?: {
|
||||
path: string;
|
||||
size: number;
|
||||
hash: string;
|
||||
};
|
||||
data?: any;
|
||||
collection?: any;
|
||||
};
|
||||
export class MicroAppModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
declare description: string;
|
||||
declare type: string;
|
||||
declare tags: string[];
|
||||
declare data: MicroAppData;
|
||||
declare uid: string;
|
||||
declare updatedAt: Date;
|
||||
declare createdAt: Date;
|
||||
declare source: string;
|
||||
declare share: boolean;
|
||||
declare uname: string;
|
||||
}
|
||||
|
||||
MicroAppModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
comment: 'id',
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
tags: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: [],
|
||||
},
|
||||
type: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
source: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: {},
|
||||
},
|
||||
share: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false,
|
||||
},
|
||||
uname: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: '',
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'micro_apps',
|
||||
// paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
MicroAppModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('MicroAppModel sync', e);
|
||||
});
|
||||
60
src/routes/micro-app/module/install-app.ts
Normal file
60
src/routes/micro-app/module/install-app.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { checkFileExistsSync } from '@/routes/page/module/cache-file.ts';
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import * as tar from 'tar';
|
||||
|
||||
const appsPath = useFileStore('apps', { needExists: true });
|
||||
|
||||
export type InstallAppOpts = {
|
||||
path?: string;
|
||||
key?: string;
|
||||
};
|
||||
export const appCheck = async (opts: InstallAppOpts) => {
|
||||
const { key } = opts;
|
||||
const directory = path.join(appsPath, key);
|
||||
if (checkFileExistsSync(directory)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
export const installApp = async (opts: InstallAppOpts) => {
|
||||
const { key } = opts;
|
||||
const fileStream = await minioClient.getObject(bucketName, opts.path);
|
||||
const pathName = opts.path.split('/').pop();
|
||||
const directory = path.join(appsPath, key);
|
||||
if (!checkFileExistsSync(directory)) {
|
||||
fs.mkdirSync(directory, { recursive: true });
|
||||
}
|
||||
const filePath = path.join(directory, pathName);
|
||||
|
||||
const writeStream = fs.createWriteStream(filePath);
|
||||
fileStream.pipe(writeStream);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
writeStream.on('finish', resolve);
|
||||
writeStream.on('error', reject);
|
||||
});
|
||||
// 解压 tgz文件
|
||||
const extractPath = path.join(directory);
|
||||
await tar.x({
|
||||
file: filePath,
|
||||
cwd: extractPath,
|
||||
});
|
||||
const pkgs = path.join(extractPath, 'package.json');
|
||||
if (!checkFileExistsSync(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');
|
||||
}
|
||||
app.key = key;
|
||||
fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2));
|
||||
// fs.unlinkSync(filePath);
|
||||
return { path: filePath };
|
||||
};
|
||||
29
src/routes/micro-app/module/load-app.ts
Normal file
29
src/routes/micro-app/module/load-app.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { checkFileExistsSync } from '@/routes/page/module/cache-file.ts';
|
||||
import { useFileStore } from '@abearxiong/use-file-store';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const appsPath = useFileStore('apps', { needExists: true });
|
||||
export const loadApp = async (key: string) => {
|
||||
const directory = path.join(appsPath, key);
|
||||
if (!checkFileExistsSync(directory)) {
|
||||
throw new Error('app not found');
|
||||
}
|
||||
const pkgs = path.join(directory, 'package.json');
|
||||
if (!checkFileExistsSync(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 (!checkFileExistsSync(mainEntry)) {
|
||||
throw new Error('Invalid main entry');
|
||||
}
|
||||
const main = await import(mainEntry);
|
||||
return main;
|
||||
};
|
||||
Reference in New Issue
Block a user