feat: add dynamic app

This commit is contained in:
2024-11-22 02:13:12 +08:00
parent d71a574613
commit 40f42ca89b
12 changed files with 358 additions and 23 deletions

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