关于login重构
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
import { minioClient } from '@/app.ts';
|
||||
import { bucketName } from '@/modules/minio.ts';
|
||||
import { fileIsExist } from '@kevisual/use-config';
|
||||
import { spawn, spawnSync } from 'child_process';
|
||||
import { getFileStat, getMinioList, MinioFile } from '@/routes/file/index.ts';
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import * as tar from 'tar';
|
||||
import { appsPath } from '../lib/index.ts';
|
||||
import { installAppFromKey } from './manager.ts';
|
||||
export type InstallAppOpts = {
|
||||
path?: string;
|
||||
key?: string;
|
||||
needInstallDeps?: boolean;
|
||||
// minio中
|
||||
appKey?: string;
|
||||
version?: string;
|
||||
username?: string;
|
||||
};
|
||||
/**
|
||||
* 检测路径是否存在
|
||||
@@ -26,28 +31,37 @@ export const appPathCheck = async (opts: InstallAppOpts) => {
|
||||
return false;
|
||||
};
|
||||
export const installApp = async (opts: InstallAppOpts) => {
|
||||
const { key, needInstallDeps } = opts;
|
||||
const fileStream = await minioClient.getObject(bucketName, opts.path);
|
||||
const pathName = opts.path.split('/').pop();
|
||||
const { key, needInstallDeps, appKey, version, username } = opts;
|
||||
const prefix = `${username}/${appKey}/${version}`;
|
||||
const pkgPrefix = prefix + '/package.json';
|
||||
const stat = await getFileStat(pkgPrefix);
|
||||
if (!stat) {
|
||||
throw new Error('App not found');
|
||||
}
|
||||
const fileList = await getMinioList({
|
||||
prefix,
|
||||
recursive: true,
|
||||
});
|
||||
for (const file of fileList) {
|
||||
const { name } = file as MinioFile;
|
||||
const outputPath = path.join(appsPath, key, name.replace(prefix, ''));
|
||||
const dir = path.dirname(outputPath);
|
||||
if (!fileIsExist(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
const fileStream = await minioClient.getObject(bucketName, `${name}`);
|
||||
const writeStream = fs.createWriteStream(outputPath);
|
||||
fileStream.pipe(writeStream);
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
writeStream.on('finish', () => resolve(true));
|
||||
writeStream.on('error', reject);
|
||||
});
|
||||
}
|
||||
const directory = path.join(appsPath, key);
|
||||
if (!fileIsExist(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(true));
|
||||
writeStream.on('error', reject);
|
||||
});
|
||||
// 解压 tgz文件
|
||||
const extractPath = path.join(directory);
|
||||
await tar.x({
|
||||
file: filePath,
|
||||
cwd: extractPath,
|
||||
});
|
||||
if (needInstallDeps) {
|
||||
try {
|
||||
installDeps({ appPath: directory, isProduction: true, sync: true });
|
||||
@@ -57,7 +71,6 @@ export const installApp = async (opts: InstallAppOpts) => {
|
||||
}
|
||||
return installAppFromKey(key);
|
||||
};
|
||||
import { spawn, spawnSync } from 'child_process';
|
||||
|
||||
export const checkPnpm = () => {
|
||||
try {
|
||||
@@ -76,13 +89,16 @@ type InstallDepsOptions = {
|
||||
export const installDeps = async (opts: InstallDepsOptions) => {
|
||||
const { appPath } = opts;
|
||||
const isProduction = opts.isProduction ?? true;
|
||||
const isPnpm = checkPnpm();
|
||||
const params = ['i'];
|
||||
if (isProduction) {
|
||||
if (isProduction && isPnpm) {
|
||||
params.push('--production');
|
||||
} else {
|
||||
params.push('--omit=dev');
|
||||
}
|
||||
console.log('installDeps', appPath, params);
|
||||
const syncSpawn = opts.sync ? spawnSync : spawn;
|
||||
if (checkPnpm()) {
|
||||
if (isPnpm) {
|
||||
syncSpawn('pnpm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
|
||||
} else {
|
||||
syncSpawn('npm', params, { cwd: appPath, stdio: 'inherit', env: process.env });
|
||||
|
||||
Reference in New Issue
Block a user