This commit is contained in:
2026-01-17 20:19:07 +08:00
parent 2cb12644ea
commit 5395449751
5 changed files with 34 additions and 93 deletions

View File

@@ -6,87 +6,6 @@ import { AssistantApp, checkFileExists } from '@/lib.ts';
import { logger } from '@/module/logger.ts';
import { LoadApp, StopApp } from '@/module/local-apps/src/modules/manager.ts';
const runScriptsCommand = new Command('run-scripts')
.alias('run')
.arguments('<cmd> [env]')
.option('-l --local', '使用当前文件夹的package.json中的scripts', false)
.description('运行脚本在assistant.config.json中配置的脚本')
.action(async (cmd, env, opts) => {
const useLocal = opts.local;
const showScripts = cmd === 'show';
const showScriptFunc = (scripts: any) => {
console.log('可用的本地脚本:');
let has = false;
Object.keys(scripts).forEach((key) => {
console.log(`- ${key}: ${scripts[key]}`);
has = true;
});
if (!has) {
console.log('当前未定义任何脚本。');
}
}
if (useLocal) {
const pkgPath = path.join(process.cwd(), 'package.json');
if (checkFileExists(pkgPath) === false) {
console.error('当前目录下未找到 package.json 文件。');
return;
}
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
const scripts = pkg.scripts || {};
if (showScripts) {
showScriptFunc(scripts);
return;
}
const script = scripts[cmd];
if (!script) {
console.error(`Script "${cmd}" not found in local package.json.`);
return;
}
const command = [script, ...(env ? [env] : [])].join(' ');
const res = spawnSync(command, { shell: true, stdio: 'inherit', cwd: assistantConfig.configDir });
console.log(`执行 "[${command}]"...`);
if (res.error) {
console.error(`执行失败 "${cmd}":`, res.error);
return;
}
if (res.status !== 0) {
console.error(`本地脚本 "${cmd}" 以代码 ${res.status} 退出`);
return;
}
return;
}
assistantConfig.checkMounted();
const configs = assistantConfig.getCacheAssistantConfig();
const scripts = configs?.scripts || {};
try {
const script = scripts[cmd];
if (showScripts) {
showScriptFunc(scripts);
return;
}
if (!script) {
console.error(`Script "${cmd}" not found.`);
return;
}
// console.log(`Running script "${script}"...`);
const command = [script, ...(env ? [env] : [])].join(' ');
// console.log(`Command: ${command}`, env);
const res = spawnSync(command, { shell: true, stdio: 'inherit', cwd: assistantConfig.configDir });
if (res.error) {
console.error(`Error running script "${cmd}":`, res.error);
return;
}
if (res.status !== 0) {
console.error(`Script "${cmd}" exited with code ${res.status}`);
return;
}
console.log(`Script "${cmd}" run successfully.`);
} catch (error) {
console.error(`Failed to run script "${cmd}":`, error);
}
});
program.addCommand(runScriptsCommand);
const createRandomApp = (opts: { app: any, package: any, pwd: string, status?: string }) => {
const { app, package: packageJson, pwd } = opts;
if (!app.status) {

View File

@@ -143,19 +143,29 @@ export type AssistantConfigData = {
enabled?: boolean;
token?: string;
}
/**
* 自定义脚本, asst 启动时会执行这些脚本
*/
scripts?: {
[key: string]: string;
};
/**
* 认证和权限配置
* share: protected 需要认证代理访问(默认) public 公开访问, private 私有访问
* share 是对外共享 pages 目录下的页面
*/
auth?: AuthPermission;
storage?: AssistantStorage[];
};
type AssistantStorage = FileStorage | S3Storage;
type FileStorage = {
id: string;
path: string;
type: 'local'
}
type S3Storage = {
id: string;
bucket: string;
region: string;
accessKeyId: string;
secretAccessKey: string;
endpoint?: string;
type: 's3' | 'minio';
}
let assistantConfig: AssistantConfigData;
type AssistantConfigOptions = {
configDir?: string;

View File

@@ -23,7 +23,7 @@ export const defaultApiProxy = [
* @param paths ['/api/router', '/v1' ]
* @returns
*/
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1']) => {
export const createApiProxy = (api: string, paths: string[] = ['/api', '/v1']): ProxyInfo[] => {
const pathList = paths.map((item) => {
return {
path: item,

View File

@@ -36,10 +36,14 @@ export type ProxyInfo = {
*/
rootPath?: string;
s3?: {
bucket: string;
region: string;
accessKeyId: string;
secretAccessKey: string;
/**
* 如何id存在使用assistan-config的storage配置
*/
id?: string;
bucket?: string;
region?: string;
accessKeyId?: string;
secretAccessKey?: string;
endpoint?: string;
}
};

View File

@@ -123,10 +123,18 @@ export const proxyRoute = async (req: http.IncomingMessage, res: http.ServerResp
// res.setHeader('Access-Control-Allow-Origin', '*');
// res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
// res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
if (apiBackendProxy.s3?.id) {
const storage = _assistantConfig?.storage || []
const storageConfig = storage.find((item) => item.id === apiBackendProxy.s3?.id);
apiBackendProxy.s3 = {
...storageConfig,
...apiBackendProxy.s3,
}
}
return proxy(req, res, {
path: apiBackendProxy.path,
target: apiBackendProxy.target,
...apiBackendProxy
...apiBackendProxy,
});
}
logger.debug('proxyRoute handle by router', { url: req.url }, noAdmin);