fix: install from app key

This commit is contained in:
xion 2024-12-04 21:55:17 +08:00
parent a5d0e66b3c
commit b2d968b70d
5 changed files with 53 additions and 46 deletions

View File

@ -154,6 +154,7 @@ router.get('/api/micro-app/download/:id', async (req, res) => {
return; return;
} }
res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(fileName)}"`); res.setHeader('Content-Disposition', `attachment; filename="${encodeURIComponent(fileName)}"`);
res.setHeader('app-key', file.data?.key || id);
res.writeHead(200, { 'Content-Type': 'application/octet-stream' }); res.writeHead(200, { 'Content-Type': 'application/octet-stream' });
try { try {
const stream = await minioClient.getObject(bucketName, objectName); const stream = await minioClient.getObject(bucketName, objectName);

View File

@ -1,6 +1,7 @@
import { app } from '@/app.ts'; import { app } from '@/app.ts';
import { MicroAppModel } from './models.ts'; import { MicroAppModel } from './models.ts';
import { appPathCheck, getAppPathKeys, installApp, installAppFromKey } from './module/install-app.ts'; import { appPathCheck, installApp } from './module/install-app.ts';
import { getAppPathKeys, installAppFromKey } from './module/manager.ts';
import { loadApp } from './module/load-app.ts'; import { loadApp } from './module/load-app.ts';
import { manager } from './manager-app.ts'; import { manager } from './manager-app.ts';

View File

@ -10,6 +10,7 @@ type MicroAppData = {
hash: string; hash: string;
name: string; name: string;
}; };
key?: string;
data?: any; data?: any;
collection?: any; // 上传的信息汇总 collection?: any; // 上传的信息汇总
}; };

View File

@ -6,7 +6,7 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import * as tar from 'tar'; import * as tar from 'tar';
import { appsPath } from '../lib/index.ts'; import { appsPath } from '../lib/index.ts';
import { installAppFromKey } from './manager.ts';
export type InstallAppOpts = { export type InstallAppOpts = {
path?: string; path?: string;
key?: string; key?: string;
@ -49,47 +49,3 @@ export const installApp = async (opts: InstallAppOpts) => {
}); });
return installAppFromKey(key); return installAppFromKey(key);
}; };
export const installAppFromKey = 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 readmeFile = path.join(directory, 'README.md');
let readmeDesc = '';
if (fileIsExist(readmeFile)) {
readmeDesc = fs.readFileSync(readmeFile, 'utf-8');
}
let showAppInfo = {
key,
status: 'inactive',
type: app?.type || 'system-app',
description: readmeDesc || '',
version,
//
entry: app?.entry || '',
path: directory,
origin: app,
};
app.key = key;
fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2));
return { pkg, showAppInfo };
};
export const getAppPathKeys = async () => {
let files = fs.readdirSync(appsPath);
files = files.filter((file) => {
const stat = fs.statSync(path.join(appsPath, file));
if (file === 'node_modules') return false;
return stat.isDirectory();
});
return files;
};

View File

@ -5,6 +5,9 @@ import { merge } from 'lodash-es';
import { deleteFileAppInfo } from './load-app.ts'; import { deleteFileAppInfo } from './load-app.ts';
import { fileIsExist } from '@kevisual/use-config'; import { fileIsExist } from '@kevisual/use-config';
import path from 'path'; import path from 'path';
import fs from 'fs';
import { appsPath } from '../lib/index.ts';
// 共享 // 共享
export const existDenpend = [ export const existDenpend = [
'sequelize', // commonjs 'sequelize', // commonjs
@ -249,3 +252,48 @@ export class Manager<T extends AppInfo = any> {
} }
} }
} }
export const installAppFromKey = 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 readmeFile = path.join(directory, 'README.md');
let readmeDesc = '';
if (fileIsExist(readmeFile)) {
readmeDesc = fs.readFileSync(readmeFile, 'utf-8');
}
let showAppInfo = {
key,
status: 'inactive',
type: app?.type || 'system-app',
description: readmeDesc || '',
version,
//
entry: app?.entry || '',
path: directory,
origin: app,
};
app.key = key;
fs.writeFileSync(pkgs, JSON.stringify(pkg, null, 2));
return { pkg, showAppInfo };
};
export const getAppPathKeys = async () => {
let files = fs.readdirSync(appsPath);
files = files.filter((file) => {
const stat = fs.statSync(path.join(appsPath, file));
if (file === 'node_modules') return false;
return stat.isDirectory();
});
return files;
};