feat: add run script
This commit is contained in:
34
assistant/src/command/run-scripts/index.ts
Normal file
34
assistant/src/command/run-scripts/index.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { program, Command, assistantConfig } from '@/program.ts';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
const runScriptsCommand = new Command('run-scripts')
|
||||
.alias('run')
|
||||
.arguments('<cmd> [env]')
|
||||
.description('运行脚本,在assistant.config.json中配置的脚本')
|
||||
.action(async (cmd, env) => {
|
||||
assistantConfig.checkMounted();
|
||||
const configs = assistantConfig.getCacheAssistantConfig();
|
||||
const scripts = configs?.scripts || {};
|
||||
try {
|
||||
const script = scripts[cmd];
|
||||
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);
|
||||
Reference in New Issue
Block a user