diff --git a/script/napcat-utf8.bat b/script/napcat-utf8.bat index ddee7f54..9b15ce8a 100644 --- a/script/napcat-utf8.bat +++ b/script/napcat-utf8.bat @@ -15,4 +15,4 @@ for %%a in ("!RetString!") do ( set "QQPath=!pathWithoutUninstall!QQ.exe" set ELECTRON_RUN_AS_NODE=1 echo !QQPath! -"!QQPath!" ./napcat.cjs %* +"!QQPath!" ./napcat.mjs %* diff --git a/script/napcat-utf8.ps1 b/script/napcat-utf8.ps1 index 7d2955f6..7fc71c3f 100644 --- a/script/napcat-utf8.ps1 +++ b/script/napcat-utf8.ps1 @@ -37,7 +37,7 @@ if (!(Test-Path $QQpath)) { throw "provided QQ path is invalid: $QQpath" } -$Bootfile = Join-Path $PSScriptRoot "napcat.cjs" +$Bootfile = Join-Path $PSScriptRoot "napcat.mjs" $env:ELECTRON_RUN_AS_NODE = 1 $commandInfo = Get-Command $QQpath -ErrorAction Stop Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{& chcp 65001;& '$($commandInfo.Path)' $Bootfile $params}" \ No newline at end of file diff --git a/script/napcat.bat b/script/napcat.bat index 77306064..0b96b596 100644 --- a/script/napcat.bat +++ b/script/napcat.bat @@ -14,4 +14,4 @@ for %%a in ("!RetString!") do ( set "QQPath=!pathWithoutUninstall!QQ.exe" set ELECTRON_RUN_AS_NODE=1 echo !QQPath! -"!QQPath!" ./napcat.cjs %* +"!QQPath!" ./napcat.mjs %* diff --git a/script/napcat.ps1 b/script/napcat.ps1 index 527de0ac..e38aca7b 100644 --- a/script/napcat.ps1 +++ b/script/napcat.ps1 @@ -37,7 +37,7 @@ if (!(Test-Path $QQpath)) { throw "provided QQ path is invalid: $QQpath" } -$Bootfile = Join-Path $PSScriptRoot "napcat.cjs" +$Bootfile = Join-Path $PSScriptRoot "napcat.mjs" $env:ELECTRON_RUN_AS_NODE = 1 $commandInfo = Get-Command $QQpath -ErrorAction Stop Start-Process powershell -ArgumentList "-noexit", "-noprofile", "-command &{& '$($commandInfo.Path)' $Bootfile $params}" \ No newline at end of file diff --git a/script/napcat.sh b/script/napcat.sh index 16ff7e2f..36198acb 100644 --- a/script/napcat.sh +++ b/script/napcat.sh @@ -18,4 +18,4 @@ if ! [ -x /opt/QQ/qq ]; then exit 1 fi -/opt/QQ/qq "${SCRIPT_DIR}/napcat.cjs" "$@" +/opt/QQ/qq "${SCRIPT_DIR}/napcat.mjs" "$@" diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index a11d221e..24415c6e 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -1,6 +1,12 @@ import path from 'node:path'; import fs from 'node:fs'; import { log, logDebug, logError } from '@/common/utils/log'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); const configDir = path.resolve(__dirname, 'config'); fs.mkdirSync(configDir, { recursive: true }); diff --git a/src/common/utils/QQBasicInfo.ts b/src/common/utils/QQBasicInfo.ts index f7655ec5..324cf610 100644 --- a/src/common/utils/QQBasicInfo.ts +++ b/src/common/utils/QQBasicInfo.ts @@ -55,7 +55,7 @@ if (fs.existsSync(configVersionInfoPath)) { export const qqVersionConfigInfo: QQVersionConfigInfo = _qqVersionConfigInfo; -export const qqPkgInfo: QQPkgInfo = require(pkgInfoPath); +export const qqPkgInfo: QQPkgInfo = JSON.parse(fs.readFileSync(pkgInfoPath).toString()); // platform_type: 3, // app_type: 4, // app_version: '9.9.9-23159', diff --git a/src/common/utils/cpmodule.ts b/src/common/utils/cpmodule.ts index f0eaa59b..744dcaa7 100644 --- a/src/common/utils/cpmodule.ts +++ b/src/common/utils/cpmodule.ts @@ -1,7 +1,12 @@ import * as os from 'os'; import path from 'node:path'; import fs from 'fs'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); export function getModuleWithArchName(moduleName: string) { const systemPlatform = os.platform(); const cpuArch = os.arch(); diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index 9a099be1..d87a3a4b 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -2,6 +2,12 @@ import crypto from 'node:crypto'; import path from 'node:path'; import fs from 'fs/promises'; import { log, logDebug } from './log'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); export function sleep(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } diff --git a/src/common/utils/log.ts b/src/common/utils/log.ts index c832f153..8408def4 100644 --- a/src/common/utils/log.ts +++ b/src/common/utils/log.ts @@ -2,6 +2,12 @@ import log4js, { Configuration } from 'log4js'; import { truncateString } from '@/common/utils/helper'; import path from 'node:path'; import { SelfInfo } from '@/core'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); export enum LogLevel { DEBUG = 'debug', diff --git a/src/common/utils/reboot.ts b/src/common/utils/reboot.ts index 02b714e5..db5a9258 100644 --- a/src/common/utils/reboot.ts +++ b/src/common/utils/reboot.ts @@ -1,6 +1,13 @@ import { resolve } from "node:path"; import { spawn } from "node:child_process"; import { pid, ppid, exit } from 'node:process'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + export async function rebootWithQuickLogin(uin: string) { let batScript = resolve(__dirname, './napcat.bat'); let batUtf8Script = resolve(__dirname, './napcat-utf8.bat'); diff --git a/src/index.ts b/src/index.ts index b4ff8671..cf878d47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { napCatCore } from '@/core'; import { program } from 'commander'; import qrcode from 'qrcode-terminal'; import fs from 'fs/promises'; +import fsSync from 'fs'; import path from 'node:path'; import { checkVersion } from '@/common/utils/version'; import { log, logDebug, logError, LogLevel, setLogLevel } from '@/common/utils/log'; @@ -9,6 +10,13 @@ import { NapCatOnebot11 } from '@/onebot11/main'; import { InitWebUi } from './webui/index'; import { WebUiDataRuntime } from './webui/src/helper/Data'; import { UpdateConfig } from './common/utils/helper'; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + program .option('-q, --qq ', 'QQ号') .parse(process.argv); @@ -19,10 +27,8 @@ UpdateConfig().catch(logError); InitWebUi(); const cmdOptions = program.opts(); // console.log(process.argv); - - -checkVersion().then((remoteVersion: string) => { - const localVersion = require('./package.json').version; +checkVersion().then(async (remoteVersion: string) => { + const localVersion = JSON.parse(fsSync.readFileSync(path.join(__dirname, 'package.json')).toString()); const localVersionList = localVersion.split('.'); const remoteVersionList = remoteVersion.split('.'); log('[NapCat] 当前版本:', localVersion); diff --git a/src/webui/index.ts b/src/webui/index.ts index 0ec5ea4a..9264c890 100644 --- a/src/webui/index.ts +++ b/src/webui/index.ts @@ -5,6 +5,13 @@ import { resolve } from 'node:path'; import { ALLRouter } from './src/router'; import { WebUiConfig } from './src/helper/config'; const app = express(); +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + /** * 初始化并启动WebUI服务。 * 该函数配置了Express服务器以支持JSON解析和静态文件服务,并监听6099端口。 diff --git a/src/webui/src/api/LogConsole.ts b/src/webui/src/api/LogConsole.ts index cd6b21cb..423b111c 100644 --- a/src/webui/src/api/LogConsole.ts +++ b/src/webui/src/api/LogConsole.ts @@ -2,6 +2,12 @@ import { RequestHandler } from "express"; import { resolve } from "path"; import { readdir, stat } from "fs/promises"; import { existsSync } from "fs"; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); export const GetLogFileListHandler: RequestHandler = async (req, res) => { try { let LogsPath = resolve(__dirname, "./logs/"); diff --git a/src/webui/src/api/OB11Config.ts b/src/webui/src/api/OB11Config.ts index 30606a98..e9614c6b 100644 --- a/src/webui/src/api/OB11Config.ts +++ b/src/webui/src/api/OB11Config.ts @@ -3,6 +3,13 @@ import { WebUiDataRuntime } from "../helper/Data"; import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; import { OB11Config } from "@/webui/ui/components/WebUiApiOB11Config"; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + const isEmpty = (data: any) => data === undefined || data === null || data === ""; export const OB11GetConfigHandler: RequestHandler = async (req, res) => { diff --git a/src/webui/src/helper/config.ts b/src/webui/src/helper/config.ts index 463507c4..93758a9f 100644 --- a/src/webui/src/helper/config.ts +++ b/src/webui/src/helper/config.ts @@ -1,6 +1,12 @@ import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; -const net = require('net'); +import * as net from "node:net"; +import { dirname } from "node:path" +import { fileURLToPath } from "node:url" + + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); // 限制尝试端口的次数,避免死循环 const MAX_PORT_TRY = 100; diff --git a/vite.config.ts b/vite.config.ts index 160faf86..262b5878 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -83,8 +83,8 @@ const baseConfig = (mode: string = 'development') => defineConfig({ minify: false, lib: { entry: 'src/index.ts', - formats: ['cjs'], - fileName: () => 'napcat.cjs', + formats: ['es'], + fileName: () => 'napcat.mjs', }, rollupOptions: { // external: [ /node:*/ ],