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) {