Files
cli/assistant/src/command/asst-server/index.ts

47 lines
1.5 KiB
TypeScript

import { program, Command } from '@/program.ts';
import { spawnSync } from 'node:child_process';
const command = new Command('server')
.description('启动服务')
.option('-d, --daemon', '是否以守护进程方式运行')
.option('-n, --name <name>', '服务名称', 'assistant-server')
.option('-p, --port <port>', '服务端口')
.option('-s, --start', '是否启动服务')
.option('-e, --interpreter <interpreter>', '指定使用的解释器', 'bun')
.action((options) => {
const { port } = options;
const [_interpreter, execPath] = process.argv;
const shellCommands = [];
if (options.daemon) {
shellCommands.push('-d');
}
if (options.name) {
shellCommands.push(`-n ${options.name}`);
}
if (options.start) {
shellCommands.push('-s');
}
if (port) {
shellCommands.push(`-p ${port}`);
}
if (options.interpreter) {
shellCommands.push(`-e ${options.interpreter}`);
}
const basename = _interpreter.split('/').pop();
if (basename.includes('bun')) {
console.log(`Assistant server shell command: bun src/run-server.ts server ${shellCommands.join(' ')}`);
const child = spawnSync(_interpreter, ['src/run-server.ts', ...shellCommands], {
stdio: 'inherit',
shell: true,
});
} else {
console.log(`Assistant server shell command: asst-server ${shellCommands.join(' ')}`);
const child = spawnSync('asst-server', shellCommands, {
stdio: 'inherit',
shell: true,
});
}
});
program.addCommand(command);