mira-app-server
Version:
Mira Server - standalone server application using mira-app-core
276 lines • 11.1 kB
JavaScript
;
/**
* Mira App Server CLI
*
* 既是服务器启动入口(start),也通过 mira-app-core/shared/sdk 暴露
* 完整的服务器操作能力(auth/libraries/files/tags/folders/plugins/devices/db/system)。
*
* 登录凭证持久化到 ~/.mira/credentials.json,支持多 profile 切换。
* 详细用法见各子命令的 --help。
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const MiraServer_1 = require("./MiraServer");
const commander_1 = require("commander");
const dotenv_1 = __importDefault(require("dotenv"));
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
// 命令模块
const doctor_1 = require("./cli/doctor");
const auth_1 = require("./cli/commands/auth");
const user_1 = require("./cli/commands/user");
const libraries_1 = require("./cli/commands/libraries");
const files_1 = require("./cli/commands/files");
const tags_1 = require("./cli/commands/tags");
const folders_1 = require("./cli/commands/folders");
const plugins_1 = require("./cli/commands/plugins");
const devices_1 = require("./cli/commands/devices");
const database_1 = require("./cli/commands/database");
const system_1 = require("./cli/commands/system");
const autostart_1 = require("./cli/commands/autostart");
const autostart_2 = require("./cli/autostart");
// MCP 服务(懒加载,避免在非 MCP 模式下加载 SDK)
const server_1 = require("./mcp/server");
// 加载环境变量
dotenv_1.default.config();
/**
* MCP 模式短路检测:
* 当命令行包含 --mcp 时,直接启动 MCP 服务(stdio),不进入 commander 命令流。
* 这样可保证 stdout 仅承载 JSON-RPC(MCP 协议要求),帮助信息也不会污染输出。
*/
async function maybeRunMcp() {
const argv = process.argv.slice(2);
if (!argv.includes('--mcp') && !argv.includes('-mcp')) {
return false;
}
// 简易解析 --server / --token / --debug(复用 commander 风格但独立处理,避免 commander 副作用)
const getOpt = (name) => {
const i = argv.indexOf(name);
return i >= 0 ? argv[i + 1] : undefined;
};
try {
await (0, server_1.startMcpServer)({
server: getOpt('--server') || getOpt('-s'),
token: getOpt('--token'),
debug: argv.includes('--debug') || process.env.MIRA_MCP_DEBUG === '1',
});
return true;
}
catch (error) {
process.stderr.write(`❌ MCP server failed to start: ${error}\n`);
process.exit(1);
}
}
// 从 package.json 读取真实版本号
// 编译产物位于 dist/,package.json 在其上一级目录
function getPackageVersion() {
try {
const pkgPath = path_1.default.resolve(__dirname, '..', 'package.json');
const pkg = JSON.parse(fs_1.default.readFileSync(pkgPath, 'utf-8'));
return pkg.version || '0.0.0';
}
catch {
return '0.0.0';
}
}
const VERSION = getPackageVersion();
commander_1.program
.name('mira-app-server')
.description('Mira Server - Media Library Management System')
.version(VERSION)
// 全局选项:所有命令可覆盖连接目标与凭证来源
.option('-s, --server <server>', '服务器地址(默认 http://localhost:8081)')
.option('--token <token>', '访问令牌(覆盖当前 profile)')
.option('--profile <name>', '使用指定 profile 的凭证')
.option('--json', '以 JSON 格式输出(便于脚本/agent 解析)');
// ============ start ============
commander_1.program
.command('start')
.description('Start the Mira server')
.option('-p, --http-port <number>', 'HTTP port number', '8081')
.option('-w, --ws-port <number>', 'WebSocket port number', '8018')
.option('-d, --data-path <path>', 'Data directory path')
.option('--env <path>', 'Environment file path')
.option('--autostart', '注册为系统开机自启项并由系统托管启动(macOS=LaunchAgent / Linux=systemd / Windows=任务计划)')
.action(async (options) => {
try {
// 如果指定了env文件,加载它
if (options.env) {
dotenv_1.default.config({ path: path_1.default.resolve(options.env) });
}
if (options.autostart) {
console.log('🚀 注册系统开机自启并启动 Mira Server...');
const target = (0, autostart_2.enableAutoStart)({
httpPort: parseInt(options.httpPort),
wsPort: parseInt(options.wsPort),
dataPath: options.dataPath,
});
console.log(`✅ 已注册开机自启并由系统托管:${target}`);
console.log(' 服务现已运行;下次开机/登录将自动启动。');
return; // 不再前台启动实例(由系统机制拉起,避免双实例端口冲突)
}
console.log('🚀 Starting Mira Server with CLI...');
const server = await MiraServer_1.MiraServer.createAndStart({
httpPort: parseInt(options.httpPort),
wsPort: parseInt(options.wsPort),
dataPath: options.dataPath,
});
console.log('✅ Mira Server started via CLI');
// 优雅关闭处理
process.on('SIGINT', async () => {
console.log('\n📴 Received SIGINT, gracefully shutting down...');
await server.stop();
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\n📴 Received SIGTERM, gracefully shutting down...');
await server.stop();
process.exit(0);
});
}
catch (error) {
console.error('❌ Failed to start server:', error);
process.exit(1);
}
});
// ============ stop ============
commander_1.program
.command('stop')
.description('停止系统托管的 Mira 服务(保留开机自启注册;前台实例请用 Ctrl+C)')
.action(() => {
try {
const status = (0, autostart_2.statusAutoStart)();
if (!status.registered) {
console.error('❌ 未注册开机自启,无系统托管实例可停止。');
console.error(' 前台启动的实例请用 Ctrl+C 退出;如需系统托管请用 `mira-app-server start --autostart`。');
process.exitCode = 1;
return;
}
(0, autostart_2.stopAutoStart)();
console.log('✅ 已停止 Mira 服务(开机自启注册已保留)。');
}
catch (e) {
console.error(`❌ 停止失败:${e?.message || e}`);
process.exitCode = 1;
}
});
// ============ restart ============
commander_1.program
.command('restart')
.description('重启系统托管的 Mira 服务')
.action(() => {
try {
const status = (0, autostart_2.statusAutoStart)();
if (!status.registered) {
console.error('❌ 未注册开机自启,无系统托管实例可重启。');
console.error(' 如需系统托管请用 `mira-app-server start --autostart`。');
process.exitCode = 1;
return;
}
(0, autostart_2.restartAutoStart)();
console.log('✅ 已重启 Mira 服务。');
}
catch (e) {
console.error(`❌ 重启失败:${e?.message || e}`);
process.exitCode = 1;
}
});
// ============ version ============
commander_1.program
.command('version')
.description('Show version information')
.action(() => {
console.log('Mira Server v' + VERSION);
console.log('Node.js', process.version);
console.log('Platform:', process.platform);
});
// ============ health(保留向后兼容) ============
commander_1.program
.command('health')
.description('Check server health')
.option('-p, --http-port <number>', 'Server port', '8081')
.action(async (options) => {
try {
const axios = await Promise.resolve().then(() => __importStar(require('axios')));
const response = await axios.default.get(`http://localhost:${options.httpPort}/health`);
console.log('✅ Server is healthy:', response.data);
}
catch (error) {
console.error('❌ Server health check failed:', error);
process.exit(1);
}
});
// ============ SDK 能力命令 ============
// 环境依赖自检(顶层 doctor 命令)
(0, doctor_1.registerDoctor)(commander_1.program);
// 认证与凭证
(0, auth_1.registerAuth)(commander_1.program);
// 用户
(0, user_1.registerUser)(commander_1.program);
// 素材库
(0, libraries_1.registerLibraries)(commander_1.program);
// 文件
(0, files_1.registerFiles)(commander_1.program);
// 标签
(0, tags_1.registerTags)(commander_1.program);
// 文件夹
(0, folders_1.registerFolders)(commander_1.program);
// 插件
(0, plugins_1.registerPlugins)(commander_1.program);
// 设备
(0, devices_1.registerDevices)(commander_1.program);
// 数据库
(0, database_1.registerDatabase)(commander_1.program);
// 系统
(0, system_1.registerSystem)(commander_1.program);
// 开机自启管理
(0, autostart_1.registerAutoStart)(commander_1.program);
// 主流程:先检测 MCP 模式,否则进入 commander 命令派发
(async () => {
if (await maybeRunMcp()) {
return; // MCP 模式已接管,进程由 stdio transport 保持运行
}
// 解析命令行参数
commander_1.program.parse(process.argv);
// 如果没有提供命令,显示帮助
if (!process.argv.slice(2).length) {
commander_1.program.outputHelp();
}
})();
//# sourceMappingURL=cli.js.map