Add support for CJS environment loader in main entry

Checks for the NAPCAT_NODE_CJS_ENV_LOADER_PATH environment variable and loads the specified CommonJS environment loader if present. Logs success or failure and exits on error. This allows for custom environment setup before continuing with the main process.
This commit is contained in:
手瓜一十雪 2026-01-22 11:58:13 +08:00
parent 6268923f01
commit 711a060dd9

View File

@ -8,6 +8,7 @@ import { webUiRuntimePort } from '@/napcat-webui-backend/index';
import { createProcessManager, type IProcessManager, type IWorkerProcess } from './process-api'; import { createProcessManager, type IProcessManager, type IWorkerProcess } from './process-api';
import path from 'path'; import path from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { createRequire } from 'module';
// ES 模块中获取 __dirname // ES 模块中获取 __dirname
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
@ -323,6 +324,20 @@ async function startWorkerProcess (): Promise<void> {
* *
*/ */
async function main (): Promise<void> { async function main (): Promise<void> {
// 检查是否启用了 CJS 环境加载器
const cjsLoaderPath = process.env['NAPCAT_NODE_CJS_ENV_LOADER_PATH'];
if (cjsLoaderPath) {
try {
logger.log(`[NapCat] [Process] 使用 CJS 环境加载器: ${cjsLoaderPath}`);
const require = createRequire(import.meta.url);
require(cjsLoaderPath);
return;
} catch (e) {
logger.logError('[NapCat] [Process] CJS 环境加载器加载失败:', e);
process.exit(1);
}
}
// 单进程模式:直接启动核心 // 单进程模式:直接启动核心
if (ENV.isMultiProcessDisabled) { if (ENV.isMultiProcessDisabled) {
await NCoreInitShell(); await NCoreInitShell();